Reputation: 310
So I'm trying to make an automatic TicTacToe GUI that will play you back once you move. The code you see is very layman as the actual code is very long and generally out of context. I want the code itself to work so that when the button is clicked, the value of A1 will change to 1 and it will check if the value is 1 so it can print, "This is a test" if so.
I've already tried global, I don't know if I'm doing it wrong or not. Is there any other way of doing this withought assigning a different Variable?
from tkinter import *
root = Tk()
A1 = 0
def btn_change():
global A1
A1 += 1
Button1 = Button(root, text=" ", command=btn_change)
Button1.pack()
if A1 == 1:
print("This is a test.")
root.mainloop()
When I run the code, the window looks fine, but when I press the button on the screen, it doesn't display "This is a test." In other words, it's just blank.
Upvotes: 0
Views: 902
Reputation: 22493
Consider the below code:
if A1 == 1:
print("This is a test.")
It runs exactly once before the mainloop starts. You will never reach the point where A1 reaches 1 and print the result.
What you need is a check for the variable A1 whenever you want, which can be done by creating another button:
Button(root, text="get result",command=lambda: print("This is a test.") if A1==1 else "").pack()
Upvotes: 1