Consider below example:
import tkinter as tk
root = tk.Tk()
root.title(\"root\")
other_window = tk.Tk()
other_window.title(\"other_window\")
root.mainloop
Tk()
initializes the hidden tcl interpreter so that the code can run, as Tkinter is just a wrapper around tcl/tk. It also automatically creates a new window. Toplevel()
just creates a new window, and wont work if Tk()
hasn't been instantiated, as it requires the tcl interpreter that Tk()
initializes. You cannot create any Tkinter widgets without instantiating Tk()
, and Toplevel is merely a widget. In the question, you use Tk()
to create a second window. You should instead create another file, because initializing the tcl interpreter multiple times can get confusing, as @Bryan Oakley explains so well. Then you should do:
from os import startfile
startfile(nameOfTheOtherFile)
, because, as Toplevel()
is just a widget, it closes when the Tk()
window is closed. Having the other window in a separate file makes it less confusing.