0

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.

miyesven
  • 13
  • 6
  • 1
    `command=self.close_window` is a *reference to a function* that will eventually be called when the button is clicked. `command=self.close_window(height)` is *the result of calling the function, RIGHT NOW* - what gets executed on a click is the return value of the function (`None` in this case, so nothing happens). – jasonharper Mar 01 '19 at 05:17
  • *"replaced by height, then it does not work."*: `height` is **local** to `def __init__(...` and destroyed on end of `def __init__(...`. Read [Why is Button parameter “command” executed when declared?](https://stackoverflow.com/questions/5767228/why-is-button-parameter-command-executed-when-declared) – stovfl Mar 01 '19 at 09:36
  • ah i see. @stovfl. so on init, the height is 0, so its just passed as height = 0 into the close_windows function? thats why its not able to enter the 'if' statement – miyesven Mar 01 '19 at 17:51
  • @miyesven: The condition in `def close_window(...` which is `if len(height.get())>0:` evaluates to `False` as `len(...` returns `0`. The click later, fails as *jasonharper* commented. – stovfl Mar 01 '19 at 17:59

0 Answers0