0

I'm trying to make an example for some high school students using tkinter with multiple forms, and accessing form data in different functions. I'm trying to keep the example simple, but having a small problem. The sv3 & sv4 variables are not getting the values from the second form. Any suggestions or ideas?

from tkinter import *
root = Tk()

sv1 = StringVar()
sv2 = StringVar()
sv3 = StringVar()
sv4 = StringVar()

#first function - this DOES NOT take text from entry widgets and displays, but should
def callback2():
    test2 = sv3.get()
    print(test2)
    print (sv4.get())
    print("show second form entry widgets values")
    return True

#first function - this takes text from entry widgets and displays
def callback():
    test = sv1.get()
    print(test)
    print (sv2.get())
    print("show first form entry widgets values")
    new_form()
    return True

#new form
def new_form():
    newfrm = Tk()
    entry3 = Entry(newfrm, textvariable=sv3).pack()
    entry4 = Entry(newfrm, textvariable=sv4).pack()
    button = Button(newfrm, text="Click Me", command=callback2).pack()
    newfrm.mainloop()

#initial form
def main_form():
    entry1 = Entry(root, textvariable=sv1).pack()
    entry2 = Entry(root, textvariable=sv2).pack()
    button = Button(root, text="Click Me", command=callback).pack()
    root.mainloop()

main_form()
martineau
  • 119,623
  • 25
  • 170
  • 301
Jamie
  • 1
  • 1
  • 4
    This is what happens when you call `Tk()` more than once - those Vars are tied to a particular instance of `Tk`, and simply won't work with widgets in a different instance. Use `Toplevel()` instead to create additional windows. – jasonharper Oct 05 '21 at 21:13
  • 1
    More on what @jasonharper said: [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) Here's some documentation on [`Toplevel`](https://www.tutorialspoint.com/python/tk_toplevel.htm) widgets. – martineau Oct 05 '21 at 21:17

1 Answers1

0

Here how to avoid using multiple Tk instances and manually transfers the values from the first two Entrys to the second pair. See lines with #### comments.

from tkinter import *
root = Tk()

sv1 = StringVar()
sv2 = StringVar()
sv3 = StringVar()
sv4 = StringVar()

#first function - this now take text from entry widgets and displays as it should.
def callback2():
    test2 = sv3.get()
    print(test2)
    print (sv4.get())
    print("show second form entry widgets values")
    return True

#first function - this takes text from entry widgets and displays
def callback():
    test = sv1.get()
    print(test)
    print (sv2.get())
    print("show first form entry widgets values")
    new_form()
    return True

#new form
def new_form():
####    newfrm = Tk()
    newfrm = Toplevel()  #### Instead.
    sv3.set(sv1.get())  #### Trasfer value.
    sv4.set(sv2.get())  #### Trasfer value.
    entry3 = Entry(newfrm, textvariable=sv3).pack()
    entry4 = Entry(newfrm, textvariable=sv4).pack()
    button = Button(newfrm, text="Click Me", command=callback2).pack()
    newfrm.mainloop()

#initial form
def main_form():
    entry1 = Entry(root, textvariable=sv1).pack()
    entry2 = Entry(root, textvariable=sv2).pack()
    button = Button(root, text="Click Me", command=callback).pack()
    root.mainloop()

main_form()

martineau
  • 119,623
  • 25
  • 170
  • 301