0

I have this code for a game using a Tkinter-based GUI:

startGame()

win=Tkinter.Tk()
win.title("TREASURE QUEST!!")
Textlabel=Tkinter.Label\
  (win,text = text1,font=('Times New Roman',12),wraplength=600,)
Textlabel.pack()
Row2=Tkinter.Frame(win)
Btn1=Tkinter.Button\
  (Row2, text=button1Name, command= button1(),font=('Times New Roman',12))
Btn2=Tkinter.Button\
  (Row2, text=button2Name, command= button2(), font=('Times New Roman',12))
Btn1.pack(side='left')
Btn2.pack(side='left')
Row2.pack()

win.mainloop()

def startGame():
    global text1,button1,button1Name,button2,button2Name
    ##adds name
    ##button that calls StoryCard
    
    text1="Welcome to Treasure Quest!!" 
    button1=introCard
    button1Name="Play"
    
    button2=kill
    button2Name="Quit"

When the button is pushed that calls the function however, the screen does not redraw and call up the new variables and just displays the old text. Why? How do I fix it?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Logan Henry
  • 63
  • 1
  • 10
  • It'd be useful to know what `button1()` and `button2()` do exactly (just show the code of them, too). – TidB Oct 24 '14 at 19:24
  • 1
    You are *calling* the functions, and assigning their *return values* to the buttons. Remove the parentheses! – jonrsharpe Oct 24 '14 at 19:25
  • possible duplicate of [Why is tkinter-Button parameter "command" executed when declared?](http://stackoverflow.com/questions/8269096/why-is-tkinter-button-parameter-command-executed-when-declared) – jonrsharpe Oct 24 '14 at 19:25
  • I did! button1 and button2 are the variables I'm referring to. In the function startGame it calls them (the names they are set to above are the titles of another function. As weird a layout as this one is it does seem to work. – Logan Henry Oct 24 '14 at 19:28
  • @jonrsharpe Thanks! that did fix my buttons I set a print statement in the function I was calling so I could see if it went and it did. The buttons still redefine when the variable changes though. – Logan Henry Oct 24 '14 at 19:31
  • 2
    Generally, you change the text of a button with `mybutton.config(text="whatever")`, not by redefining the string that it points to. – Kevin Oct 24 '14 at 19:39
  • Does this answer your question? [Why is my Button's command executed immediately when I create the Button, and not when I click it?](https://stackoverflow.com/questions/5767228/why-is-my-buttons-command-executed-immediately-when-i-create-the-button-and-no) – Karl Knechtel Sep 05 '22 at 07:37

0 Answers0