I would like the text to appear and be updated in each window, instead of only in one. I have noticed that the window that works is always the first that is being called, but that does not help me solve the issue.
Another thing I noticed is that the program accepts entering new values into the windows that display a value in the first place, but any attempt to change de
value by entering a value in the second window fails.
Here is a simplified version of my code:
from Tkinter import *
root = Tk()
root2 = Tk()
de= IntVar()
de.set(0)
def previous():
de.set(de.get()-1)
def Next():
de.set(de.get()+1)
def go_to(event) :
de.set(de.get())
button4 =Button( root2, text='Next', command=Next )
button4.grid(row=26 ,column=9, columnspan=2, rowspan=1,padx=0, pady=0, sticky=W+E+N+S)
button5 =Button( root2, text='Previous', command=previous )
button5.grid(row=26, column=6, columnspan=2, rowspan=1,padx=0, pady=0, sticky=W+E+N+S)
label1=Label(root2, text = 'Go to2')
entry1 = Entry(root2,textvariable=de,bd=1,width=3)
entry1.bind("<Return>", go_to)
label1.grid(row=25, column=8, columnspan=1, rowspan=1,padx=0, pady=0)
entry1.grid(row=26, column=8, columnspan=1, rowspan=1,padx=0, pady=0)
button3 =Button( root, text='Next', command=Next )
button3.grid(row=26 ,column=9, columnspan=2, rowspan=1,padx=0, pady=0, sticky=W+E+N+S)
button2 =Button( root, text='Previous', command=previous )
button2.grid(row=26, column=6, columnspan=2, rowspan=1,padx=0, pady=0, sticky=W+E+N+S)
label=Label(root, text = 'Go to1')
entry = Entry(root,textvariable=de,bd=1,width=3)
entry.bind("<Return>", go_to)
label.grid(row=25, column=8, columnspan=1, rowspan=1,padx=0, pady=0)
entry.grid(row=26, column=8, columnspan=1, rowspan=1,padx=0, pady=0)
root2.mainloop()
root.mainloop()
The root cause of the problem is that Tkinter isn't designed to have two root windows. Doing so has some unexpected side effects such as what you're seeing. In effect, you can think of the two root windows as two separate processes or threads which cannot share information. Your IntVar
belongs to the first window, but you're trying to use it in the second.
The fix is to never create more than one instance of Tk
. If you need more windows, create instances of Toplevel
. Doing that, you can share the same IntVar
among as many windows as you want.