0

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?

2 Answers2

0

Try the following when binding the command to your button within the creation of the button:

command=labmda: ButA()

Otherwise your function is called as soon as the button is created.

ArK
  • 17
  • 1
  • 5
  • Thank you so much! This worked! If you don't mind me asking another question, I want to set a series of conditionals based on the value of decision, to replace the onscreen text, any idea how I could do that? Sorry for all the questions. Edit: Figured out one of my questions, so I deleted it. – CasualHawk Mar 02 '20 at 14:13
  • I assume the text you display is a label or a text widget. The decision that is made is expressed by the press of a button, right? Then you could bind a function that deletes the label/text widget to the button and draws a new one or you could read into StringVar that is changed when the button is pressed. Also I would appreciate an upvote :) – ArK Mar 02 '20 at 14:56
  • Thanks, I'll try that. So I should be looking to make a function that creates a new text box, and I should die that to the button? Is there a way I could keep the conditional format while still doing that? I'm just wondering as it's difficult to organize the branching pathes with that method. Also I'd love to but I have less than 15 repuation so stack overflow won't let me, so sorry :( – CasualHawk Mar 02 '20 at 15:41
  • I don't fully understand what your goal is, maybe open a new question for that if it is generally applicable. – ArK Mar 02 '20 at 15:47
  • Oh alright I'll do that, thanks! – CasualHawk Mar 02 '20 at 16:02
  • Hey I just wanted to say I posted a new question that clarified what I meant: https://stackoverflow.com/questions/60493250/im-wondering-how-to-make-logic-feed-into-a-gui-to-print-certain-things-to-the-s – CasualHawk Mar 02 '20 at 16:48
0

You do not need the parenthesis, just use command=ButA instead of command=ButA(). Otherwise, it will call the function instantly, at the definition of the button.

opt1 = Button(root, text='A', command=ButA, bg='black', bd=5, font=('Times', 20), activebackground='blue',
                  activeforeground='black', fg='black')
# [...]
opt2 = Button(root, text='B', bg='black', bd=5, font=('Times', 20), activebackground='blue',
                activeforeground='black', fg='black', command=ButB)
Phoenixo
  • 2,071
  • 1
  • 6
  • 13