Why are multiple instances of Tk discouraged?

后端 未结 3 2141
心在旅途
心在旅途 2020-11-21 09:55

Consider below example:

import tkinter as tk

root = tk.Tk()
root.title(\"root\")

other_window = tk.Tk()
other_window.title(\"other_window\")

root.mainloop         


        
3条回答
  •  我在风中等你
    2020-11-21 10:28

    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.

提交回复
热议问题