0

I would like to have a window with a start value "0" for example. Then when I open another window, I would like to be able to enter a Value there and after clicking on a button (here OK) I would like this Value to be displayed instead of the "0" (or any other previous value). That last part unfortunately does not work (value stays zero after clicking on OK). What is it, that I am doing wrong?

Also, could I have placed the a inside of the clsApp? I did not do it because I thought that the mainloop would always set it to the start value. (Admittedly I did not search for this one, so I would not mind if you would tell me to googel it.)

import tkinter as tk

a=0

class clsApp(object):

    def __init__(self):
        self.root=tk.Tk()
        self.root.title("MainWindow")
        ##Labels##
        self.rootLabel=tk.Label(self.root, text="WindowAppExperiment", padx=100)
        self.aLabel=tk.Label(self.root, text=a, padx=100)
        ##Buttons##
        self.exit=tk.Button(self.root, text="Quit", fg="red", command=self.root.quit)
        self.newWindow=tk.Button(self.root, text ="Edit", command=lambda:self.clsNewWindow(self.root).run())

    def grid (self):
        self.rootLabel.pack()
        self.aLabel.pack()
        self.exit.pack(side="left")
        self.newWindow.pack(side="left")

    def run(self):
        self.grid()
        self.root.mainloop()
        self.root.destroy()

    class clsNewWindow(object):

        def __init__(self, Parent):
            self.parent=Parent
            self.top=tk.Toplevel()
            self.top.title("SubWindow")
            ##Labels##
            self.textLabel=tk.Label(self.top, text ="Enter Value", padx=100)
            ##Entyfields##
            self.E1=tk.Entry(self.top)
            ##Buttons##
            self.Cancel=tk.Button(self.top, text ="Cancel", command=self.top.quit)
            self.OK=tk.Button(self.top, text ="OK", command=lambda:self.getValue(self.E1))

        def grid(self):
            self.textLabel.pack()
            self.E1.pack()
            self.Cancel.pack()
            self.OK.pack()

        def getValue(self, entField):
            global a
            print(entField.get()) #test
            a=entField.get()
            print(a) #test
            self.parent.update()

        def run(self):
            self.grid()
            self.top.mainloop()
            self.top.destroy()


clsApp().run()
Bubibob
  • 191
  • 1
  • 13

1 Answers1

0

If you put a in there with text=a, it'll look at it to get the value just that once, and then never again... unless you tell it to. You can either trace the variable or simply update the variable in the main window whenever you change it in the child window:

def getValue(self, entField):
    global a
    print(entField.get()) #test
    a=entField.get()
    self.parent.alabel.config(text=a) # this right here
    print(a) #test
    self.parent.update()
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97