1

i am having issue with the tkinter StrinVar() and i dont have much knowledge in tkinter and would like some help, thanks.

What i have done is i created two window in which each has a Entry widget and Button widget if you click button of one window then that window gets withdraw and another window pop ups and if you click button in that pop up window then it withdraws and a original window gets pop up.Here is the code:-

from tkinter import *
def x():
    a1.set("")
    a.withdraw()
    b.deiconify()
def y():
    b1.set("")
    b.withdraw()
    a.deiconify()
a=Tk()
a.withdraw()
a1=StringVar()
b1=StringVar()
Entry(a,textvariable=a1).pack()
Button(a,text="button1",command=x).pack()
a.withdraw()
b=Tk()
Entry(b,textvariable=b1).pack()
Button(b,text="button2",command=y).pack()
mainloop()

if you write something in entry widget and press button2 then second window pop ups then again if you write something in entry widget and press button1 the entry field is not getting updated in previous first window.

i found that someone suggested that there should not be two instanses of Tk() a tkinter must have only one instanse of Tk() for multiple window use Toplevel(). So, i changed b=Tk() to b=Toplevel() which works fine and StringVar() was updating value

Now, my question is that 1)What is the reason/logic behind not updating StingVar() with two instanses of Tk()2) is there any way to make StringVar() to update value with having two instanses of Tk()

Sanket
  • 65
  • 1
  • 5
  • `Tk()` creates an instance of the embedded Tcl/Tk environment that contains the actual implementation of the GUI; the window you get from it is just a side-effect. Tk vars are tied to the environment in which they are created, and won't work with widgets from a different environment. – jasonharper Jan 06 '19 at 07:01

1 Answers1

2

" 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.

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.

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. Quite simply, that is how tkinter and the underlying Tcl/Tk interpreter was designed to be used. " --- From Bryan Oakley at Why are multiple instances of Tk discouraged?

#1 So When you create a=Tk() and then b=Tk() StringVar a1, b1 is available in b=Tk() but not in a=Tk(). that's why value is not updating in a=Tk().

#2 As stated above "StringVar created in one interpreter is not visible in another". So is there no way to make StringVar() to update value with having two instanses of Tk().

Partho63
  • 3,117
  • 2
  • 21
  • 39
  • When i run my program, 1) window having button2 appears. 2) In this window i typed for eg "ABCD" in the Entry widget. 3)After typing when i press button2 a new window having button1 appears 4)Now, if i press button1 then previous window appears but "ABCD" which was typed earlier remains as it is even though i have written b1.set("") (not getting cleared/updated) this is the problem i have faced but i have solved it by modifying to b=Toplevel() and i didn't understood my two doubts which i have mentioned above – Sanket Jan 06 '19 at 11:15
  • 1
    @sanket sorry for the previous answer. I have updated my answer. Let me know if it helped you or not. :) – Partho63 Jan 06 '19 at 12:11