Why Are Multiple Instances of Tk Discouraged

Why are multiple instances of Tk discouraged?

Why is it considered bad to have multiple instances of Tk?

Tkinter is just a python wrapper around an embedded Tcl interpreter that imports the Tk library. When you create a root window, you create an instance of a Tcl interpreter.

Each Tcl interpreter is an isolated sandbox. An object in one sandbox cannot interact with objects in another. The most common manifestation of that is that a StringVar created in one interpreter is not visible in another. The same goes for widgets -- you can't create widgets in one interpreter that has as a parent widget in another interpreter. Images are a third case: images created in one cannot be used in another.

From a technical standpoint, there's no reason why you can't have two instances of Tk at the same time. The recommendation against it is because there's rarely an actual need to have two or more distinct Tcl interpreters, and it causes problems that are hard for beginners to grasp.

Is the second snippet considered a bit better, or does it suffer from the same conditions the first code does?

It's impossible to say whether the second example in the question is better or not without knowing what you're trying to achieve. It probably is not any better since, again, there's rarely ever a time when you actually need two instances.

The best solution 99.9% of the time is to create exactly one instance of Tk that you use for the life of your program. If you need a second or subsequent window, create instances of Toplevel. Quite simply, that is how tkinter and the underlying Tcl/Tk interpreter was designed to be used.

why does tkinter Entry widget returns empty sting while using multiple windows but seems to work in a single window?

Your issue is you are using two instances of Tk(). Doing so means you have two seperate instances of Tk() running that essentially can't interact with each other (a great explanation of this can be found here). Hence why you aren't able to store the text from the Entry box in StringVar and it only returns an empty string. Instead, use Toplevel(). It does what you are wanting to do while only using one Tk(). Working example using Toplevel:

import tkinter as tk

class Sample():
def __init__(self,root):
self.root = root
self.root.geometry("200x200")
S_Button = tk.Button(root,text="Open entry Box",command=self.add)
S_Button.pack()


def add(self):
self.second = tk.Toplevel(self.root)
self.second.geometry("500x200")

# Widgets, StringVar
self.SRN = tk.StringVar()
S_entry = tk.Entry(self.second,textvariable=self.SRN)
S_Button2 = tk.Button(self.second,text="Store in Database",command=self.data)

# Packing widgets
S_entry.pack()
S_Button2.pack()



def data(self):
self.second.destroy()
print(self.SRN.get())


if __name__ == "__main__":
root = tk.Tk()
app = Sample(root)
root.mainloop()

Don't use from tkinter import *, it doesn't follow PEP8. Instead use import tkinter as tk or similar. Don't use button = tk.Button().pack(). Seperate the button creation from the packing, this makes it much easier to order your widgets properly when there are more of them.

How Do I Use Multiple Windows In Python Tkinter

You can use tk.Toplevel() to create new window in tkinter.

More information is available here

Example

new_win=Toplevel()

Note: if you destroy the main Tk() all of the Toplevel() attached to that main window will also be destroyed.



Related Topics



Leave a reply



Submit