0

I dive into tkinter at the moment and many code examples on the internet cover only very simple examples and don't teach best-practices.

For better maintainability and clearness, I'd like to use OOP for a GUI, which makes sense from my point of view. However, I'd live your advice about how to structure it because I'm a beginner in general. I've already browsed other questions, but they could not answer my specific general question I have.

My idea here:

Would like to create a menubar for the GUI and create a new file menu.py that only deals with that menu. Here you can find two examples:

1. Example: Logically, the menubar consists of other menus. So the menu contains menus. But I am not sure if it's good to use nested classes?

import tkinter as tk

class Menu(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.parent = parent
        self.menubar = Menubar(self)

    class Menubar(tk.Menu):
        def __init__(self, parent, *args, **kwargs):
            super().__init__(parent, *args, **kwargs)
            self.parent = parent

            self.add_cascade(label="File", menu=self.file_menu)
            self.add_cascade(label="Edit", menu=self.edit_menu)

            self.file_menu = FileMenu(self)
            self.edit_menu = EditMenu(self)

        class FileMenu(tk.Menu):
            def __init__(self, parent, *args, **kwargs):
                super().__init__(parent, *args, **kwargs)
                self.parent = parent

        class EditMenu(tk.Menu):
            def __init__(self, parent, *args, **kwargs):
                super().__init__(parent, *args, **kwargs)
                self.parent = parent

2. Example: Here you can find a more procedural example. But the weakness here from my point of view is that hierarchical the Menubar as well as the file_menu and the edit_menu are on the same level. But logically the Menubar consists of the file_menu and edit_menu. So, it's not really modular. On the other side, it's perhaps(?) more easy to read than the first example.

import tkinter as tk

class Menu(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.parent = parent
        self.menubar = tk.Menu(self)

        self.create_file_menu(self.menubar)
        self.create_edit_menu(self.menubar)

    def create_file_menu(self, parent):
        self.file_menu = tk.Menu(parent)
        self.menubar.add_cascade(label="File", menu=self.file_menu)

    def create_edit_menu(self, parent):
        self.edit_menu = tk.Menu(parent)
        self.menubar.add_cascade(label="Edit", menu=self.edit_menu)
Aliquis
  • 2,091
  • 4
  • 21
  • 40
  • are you aware that you are declaring a class inside of another class? – WhatsThePoint Feb 20 '17 at 09:12
  • Yes, I wrote this in my text. – Aliquis Feb 20 '17 at 09:24
  • 1
    sorry didnt see that part, but it would be better to seperate them and call them up when you need them – WhatsThePoint Feb 20 '17 at 09:28
  • 1
    I think this would make a better fit on [code Review SE](https://codereview.stackexchange.com/) – TidB Feb 20 '17 at 09:51
  • @TidB Code Review doesn't deal in examples, so I have my doubts about that. – Mast Feb 20 '17 at 09:54
  • 1
    This is probably a too broad question for SO. I'd agree with @WhatsThePoint, however. In this case I see no use of nesting the classes—you'll probably just into annoying problems later if you want to reuse something. – pingul Feb 20 '17 at 10:47
  • Possible duplicate of [Best way to structure a tkinter application](http://stackoverflow.com/questions/17466561/best-way-to-structure-a-tkinter-application) – WhatsThePoint Feb 20 '17 at 11:15

1 Answers1

0

Nesting a class doesn't reduce nor increase execution efficiency. It may alter maintenance and understanding efficiency. Python's nested classes aren't like Java's where the nested class where the inner class can reference instances of the outer class. They're effectively two separate classes.

Thus your example 1 is better than example 2.

See also:

Kardi Teknomo
  • 1,375
  • 16
  • 24