Why does the function always return 0? (function + tkinter + global variables)

25 views Asked by At

Here is a fragment of my code:

from tkinter import*
root = Tk()
root.title("Example")
root.geometry("1000x250")
root['bg']="black"
def ask2(question,var1,var2):
    ans = 0
    def button1():
        global ans
        bt1.destroy()
        bt2.destroy()
        lb.destroy()
        root.quit()
        ans = 0
    def button2():
        global ans
        bt1.destroy()
        bt2.destroy()
        lb.destroy()
        root.quit()
        ans = 1
    lb = Label(root,text=question,
                bg="black",
                fg="white",
                font=("Determination (RUS BY LYAJKA)",28))
    bt1 = Button(text=var1,
                 justify=CENTER,
                 relief=SOLID,
                 bg="black",
                 fg="white",
                 font=("Determination (RUS BY LYAJKA)",20),
                 command=button1,
                 disabledforeground="yellow")
    bt2 = Button(text=var2,
                 justify=CENTER,
                 relief=SOLID,
                 bg="black",
                 fg="white",
                 font=("Determination (RUS BY LYAJKA)",20),
                 command=button2)
    lb.pack(side=TOP)
    bt1.pack(side=LEFT)
    bt2.pack(side=RIGHT)
    root.mainloop()
    if ans == 0:
        return(0)
    else:
        return(2)    
print(ask2("Question","Answer 1", "Answer 2"))

This function always returns 0. Why?

It's my output (I used the first and the second buttons): enter image description here I thought that first the program would output 0 and then 2...

0

There are 0 answers