Search code examples
pythontkintertk-toolkittoplevel

How should I make a class that can be used as my main app window, but also can be used as a secondary window


I know that I can subclass a tk.Frame (or ttk.Frame) and add that to a TopLevel to make secondary windows, but I'm not sure how I should use that as the main window. I know that creating an instance of a Frame class and calling .mainloop() on it seems to work for using it as the main window, but I feel like that's bad practice...

What do other people do when they are making GUI layouts that they want to have available to main windows and secondary windows?


Solution

  • Create a subclass of a Frame, and then put it either in the root window or a toplevel. In either case, you still call mainloop only once on the root window.

    The only care you have to take is that you have to be careful about letting the user close the root window, because it will cause all of the other windows to be destroyed.

    If you're creating a program that can have multiple windows, you might want to consider hiding the root window and always putting your window in a Toplevel. Of course, when you do that you need to make sure you destroy the root window whenever the last toplevel window is destroyed, or your program will continue to run but the user will have no way to access it.