Mar-16-2022, 03:05 AM
(This post was last modified: Mar-16-2022, 03:06 AM by deanhystad.)
The problem is not importing tkinter, it is calling Tk() and mainloop(). When you call Tk() it not only creates a top level window, but initializes tkinter. Doing this more than once leads to unpredictable results. mainloop() runs until the application is shut down. You could only run one mainloop(). Which of the many root windows would you choose to be THE root.
Your second experiment should work. You create a root window using Tk() and additional windows using TopLevel.
Your second experiment should work. You create a root window using Tk() and additional windows using TopLevel.
import tkinter as tk class MainWindow(tk.Tk): def __init__(self, *args, title="Main Window", **kwargs): super().__init__(*args, **kwargs) self.title(title) tk.Label(self, font=(None, 20), text= \ "This is the main window.\n" "It needs to be created first.") \ .pack(padx=20, pady=20) tk.Button(text="Push me to make a window", command=lambda: SecondWindow()) \ .pack(padx=20, pady=20) tk.Button(text="Push me to quit", command=self.quit) \ .pack(padx=20, pady=20) class SecondWindow(tk.Toplevel): def __init__(self, *args, title="Other Window", **kwargs): super().__init__(*args, **kwargs) self.title(title) tk.Label(self, font=(None, 20), text= \ "This is a toplevel window.\n" "Create this after the main window.") \ .pack(padx=20, pady=20) MainWindow().mainloop()