Joshua Nixon
Joshua Nixon

Reputation: 1417

python tkinter code layout

Up until now i would create a different module for each interface, splash screen, login then main interface and pass each module the main_window.

class MainWindow(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

I then pass this to each module. I am thinking there is a better way and a more organised way of creating tkinter GUI's. Do i create all of the screens in one module and call them or create separate modules?

EDIT An explaination on Tk and Toplevel would also be nice as i don't full understand them, i normally uses Tk()

I'm relatively new to uses class as objects, tk.Tk. Any advice or code layout examples would be nice.

Upvotes: 0

Views: 227

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385830

Widgets exist in a tree-like hierarchy with a single root. The root window is a Toplevel, though with a few other special behaviors because it is the root window. Both the single root window and all instances of Toplevel are independent windows floating on the screen.

For a tkinter application to work correctly you must have one root window, but you may have as many instances of Toplevel that you want. If you do not explicitly create a root window, one will be created the first time you try to create some other widget. You shouldn't write tkinter code that depends on this behavior. Explicit is better than implicit.

It's perfectly reasonable -- and often preferable -- to define each interface in a separate module, and pass the instance of the root window into the constructor of each other window.

Upvotes: 1

Related Questions