1

I'm trying to understand the functionality of .wait_variable() method. How does it actually work? Because it doesn't work the way I wanted it to.

Here is the try-out I've done:

import tkinter as tk

def func1():
    print(1)
    root.wait_variable(var)
    print(2)

def func2():
    print(3)
    global var
    var = True
    print(4)
    
root = tk.Tk()

var = False
button1 = tk.Button(root, text="Button 1", command=func1)
button1.pack()
button2 = tk.Button(root, text="Button 2", command=func2)
button2.pack()

root.mainloop()

Here's the output when I press the Button 1 and Button 2 in order:

1
3
4

Intended Output:

1
3
4
2

How can I achieve this?

1
  • 2
    It needs to be a tkinter.StringVar or IntVar or something like this to work. Also be aware of this Commented Feb 1, 2023 at 15:01

1 Answer 1

1

As explained by @Thingamabobs, the wait_variableexpect the variable to be a tkinter.BooleanVar, a tkinter.IntVar, a tkinter.DoubleVar or a tkinter.StringVar. As a result, the corrected code would be:

import tkinter as tk


def func1():
    print(1)
    root.wait_variable(var)
    print(2)


def func2():
    print(3)
    global var  # not required
    var.set(True)
    print(4)


root = tk.Tk()

var = tk.BooleanVar()
button1 = tk.Button(root, text="Button 1", command=func1)
button1.pack()
button2 = tk.Button(root, text="Button 2", command=func2)
button2.pack()

root.mainloop()

Then, when you press the Button 1 and Button 2 in order, the output is then

1
3
4
2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.