I am making a choose your own adventure game, and I'm trying to create a system where there is a global boolean that is toggled between True & False to dictate where the player goes next. Additionally, I'm going to use conditionals to dictate the logic and flow of events, and in order to make the logic wait for the GUI, I also added a 'count' variable that is incremented by 1 every time a button is clicked and made it so that the logic only runs if the count is greater than 0. However, when I made the two functions and set them as commands for the button, they kept executing before I actually pressed the button. Here is the code:
def ButA() :
global count
count += 1
Decision = True
def ButB() :
global count
count += 1
Decision = False
# GUI:
if Player == 0 :
root = tkinter.Tk()
canvas = tkinter.Canvas(root, width=960, height=720)
image = ImageTk.PhotoImage(Image.open('TitleScreen.png'))
canvas.create_image(0, 0, anchor=tkinter.NW, image=image)
textbox = canvas.create_text(485, 375, font=("Times", 25, 'bold'),
text='Welcome, press A to proceed, press B to quit [a/b]', fill='white',
justify='center')
opt1 = Button(root, text='A', command=ButA(), bg='black', bd=5, font=('Times', 20), activebackground='blue',
activeforeground='black', fg='black')
opt1.place(x=435, y=465)
opt2 = Button(root, text='B', bg='black', bd=5, font=('Times', 20), activebackground='blue',
activeforeground='black', fg='black', command=ButB())
opt2.place(x=485, y=465)
The value of the Decision is set to further up, and the value of count is set to 0. Whenever I run the script I see the number 2 appear in the middle of the screen which shows me that for some reason instead of reacting when the button is pressed, whenever I run the script it executes both functions once then just gives me the result. What am I doing wrong?