How Does Creating Two Instances Of Tk Work With One Mainloop?
How does adding master = Tk() into the __init__ of a subclass of tkinter.Frame produce two windows (app and app2) when only app.mainloop() is called? from tkinter import Frame,Bu
Solution 1:
You cannot create two instances of the class Tk
, and it's somewhat unusual to instantiate it within the __init__
of another class. Your code should work, but I've never seen it done that way.
You need to create an instance of Tk
before creating any other widgets. Since your main app is a subclass of Frame
, you're partially creating the instance of a Frame
before initializing Tkinter which is simply not the way it should be done. It might work, but the behavior is undefined.
Instead, it's generally better to create your application as a subclass of Tk:
from Tkinter import tk
classApplication(tk.Tk):
...
app =Application(...)
app.mainloop()
OR, create an instance of Tk
at the global scope, and pass it as an argument to your other widgets:
from Tkinter import tk
classApplication(tkFrame):
...
root = tk.Tk()
myframe = Application(root)
root.mainloop()
If you need more than one window, create additional windows with the Toplevel
class.
Post a Comment for "How Does Creating Two Instances Of Tk Work With One Mainloop?"