0

I have a question regarding Tkinter in Python. I was thinking of the smartest/most commonly used way of creating GUIs with many/multiple Entries, Lables, Buttons, etc.

In my first sketches I just put them before the window.mainloop(), which at some point becomes confusing and unreadable. I then used methods for each Label, Button,... like this:

def someInput():
    someInput = Entry(controlFrame)
    someInput.grid(column = 1, row = 1)
    ...

Now I was thinking, if it makes sense to put them into a class like this:

class allInputs():
   def inputOne(*args):
       ...
   def inputTwo(*args):
       ...

What is your preferred way of doing this and why?

Thanks in advance.

X_841
  • 191
  • 1
  • 14
  • 1
    check this answer - [best way to structure a tkinter application](https://stackoverflow.com/questions/17466561/best-way-to-structure-a-tkinter-application) – FrainBr33z3 Dec 19 '19 at 10:07

1 Answers1

1

My typical framework will look something like this

class App(tk.Frame):
    def __init__(self,master=None,**kw):
        tk.Frame.__init__(self,master=master,**kw)
        self.btnOne = tk.Button(self,text="Hello")
        self.btnOne.grid()
        self.ow = OtherWidget(self)

class OtherWidget(tk.Frame):
    ....

if __name__ == '__main__':
    root = tk.Tk()
    App(root).grid()
    root.mainloop()

The main application is a class (a sub-class of a tk Frame) and each "sub-component grouping" of widgets is its own class.

For example if I have a button which populates a text box, then they will be part of the same class. A textbox and it's scrollbar will be another class.

Basically I will group together widgets by their function and put them in the same class, all of which will be grouped in to the application class.

This makes sense to me as any data shared between widgets exist within the same class 'namespace' and they can access each others data without having to deal with globals.

scotty3785
  • 6,763
  • 1
  • 25
  • 35