When passing variables between functions in the same class in tkinter, it does not work if I just pass in the variable. The parameter seems to only work in the new function if it is an instance variable.
My code is a simple window that closes when the button is pressed if the entry field is not empty.
class simpleGUI:
def __init__(self, root):
height = StringVar()
self.root = root
self.frame = ttk.Frame(self.root, padding='30 30 30 30')
self.frame.grid(row=0, column=0, sticky=(N, W, E, S))
self.root.title("Enter your parameters")
ttk.Label(self.frame, text="Height").grid(row=1, column=1, sticky=(W, E))
# widgets
self.height = ttk.Entry(self.frame, width=7, textvariable=height)
self.height.grid(row=2, column=1, sticky=(W, E))
self.height.focus()
self.button = ttk.Button(self.frame, text="OK", command=self.close_window)
self.button.grid(row=3, column=1, sticky=(W, E))
def close_window(self):
if len(self.height.get())>0:
self.root.destroy()
This only works if the height widget is saved in self.height. If self.height is replaced by height, then it does not work. For example, if the bottom half of the code is replaced by the following:
# widgets
height = ttk.Entry(self.frame, width=7, textvariable=Height)
height.grid(row=2, column=1, sticky=(W, E))
height.focus()
self.button = ttk.Button(self.frame, text="OK", command=self.close_window(height))
self.button.grid(row=3, column=1, sticky=(W, E))
def close_window(self, height):
if len(height.get())>0:
self.root.destroy()
I dont get any errors in the console It just doesn't close the window anymore and I'm trying to understand why. Advice from more experienced folks will be appreciated.