Home > Enterprise >  Can you have two instances of tkinter in Python?
Can you have two instances of tkinter in Python?

Time:08-24

Is it possible to have two instances of tkinter ?

import tkinter as tk
import tkinter as sk

root = tk.Tk()
root2 = sk.Tk()

....some window with tk

....some window with sk
root.mainloop()
root2.mainloop()

then have a Toplevel() in both instances.

CodePudding user response:

You can, but the way it works likely won't be like what you expect. Importing it twice isn't the problem (but neither is it the solution). No matter how you import it or how often you import it, creating more than one instance of Tk is the problem. Each instance is backed by a separate internal interpreter. Images and variables and widgets created in one won't exist in the other.

If you need more than one window, it's usually best if second and subsequent windows are instances of Toplevel.

  • Related