简体   繁体   中英

why is my function not getting executed in tkinter

Here is the code( a bit too long). i tried debbuging by printing the values and it works alright. But it just dosent execute in tkinter. I have defined all functions, checked if there is a typo error

# BUTTON CLICK
def button_click(choice):
    global player_img_address
    global computer_img_address
    global your_score
    global computer_score
    global round
    your_choice = choice
    computer_choice = computer_select()
    win_check = check_win(your_choice,computer_choice)
    score_manage(win_check)
    img_add(your_choice,computer_choice)

# GAME SETUP
rps_game = Tk()
rps_game.title('ROCK PAPER SCISSOR GAME')
rps_game.configure(bg= 'grey', width= 500, height= 400)

Label(rps_game,text='ROCK PAPER SCISSOR', font = 'normal 20 bold', fg = 'blue' , bg = 'grey').place(relx = .22 , rely = 0)
Label(rps_game, text = f'Round {round}', font = 'normal 18 bold', bg = 'grey', fg = 'blue').place(relx = .40, rely = .1 )

# PLAYER VS COMPUTER label
pl = Label(text = 'Player        ', bg ='grey', font = 'normal 15 bold').place(relx = .18, rely = .3)
comp = Label(text = 'Computer', bg = 'grey', font = 'normal 15 bold').place(relx = .68, rely = .3)
vs = Label(text = '   Vs         ', bg = 'grey', font = 'normal 15 bold').place(relx = .44, rely = .3)

#SCORE
player_score_label = Label(text = your_score, bg = 'grey', font = 'normal 15 bold').place(relx = .23, rely = .4)
computer_score_label = Label(text = computer_score, bg = 'grey', font = 'normal 15 bold').place(relx = .75, rely = .4)

# IMAGE
p_img = Image.open(player_img_address)
p_img = p_img.resize((50,50), Image.ANTIALIAS)
player_img = ImageTk.PhotoImage(p_img)
Label(image= player_img, bg ='grey').place(relx = .19, rely = .5)
c_img = Image.open(computer_img_address)
c_img = c_img.resize((50,50), Image.ANTIALIAS)
computer_img = ImageTk.PhotoImage(c_img)
Label(image= computer_img, bg = 'grey').place(relx = .72, rely = .5)

# ROCK PAPER SCISSOR BUTTONS
r_bt = Button(rps_game,text= 'ROCK', width = 6, height = 2, bg = 'blue', fg = 'black',command = button_click('rock')).place(relx = .19, rely = .7)
p_bt = Button(rps_game,text= 'PAPER', width = 6, height = 2, bg = 'blue', fg = 'black',command = button_click('paper')).place(relx = .45, rely = .7)
s_bt = Button(rps_game,text= 'SCISSOR', width = 6, height = 2, bg = 'blue', fg = 'black',command = button_click('scissor')).place(relx = .72, rely = .7)

rps_game.mainloop()

When you create the buttons, you are calling the function button_click instead of just passing a reference to it. Since that function doesn't return anything, you are effectively setting the command to None .

Since to want to pass parameters to the commands, consider using functools.partial :

rock = functools.partial(button_click, choice="rock")
paper = functools.partial(button_click, choice="paper")
scissors = functools.partial(button_click, choice="scissors")

r_bt = Button(rps_game,text= 'ROCK', width=6, height=2, bg='blue', fg='black', command=rock)
r_bt.place(relx = .19, rely = .7)
p_bt = Button(rps_game,text='PAPER', width=6, height=2, bg='blue', fg='black', command=paper)
p_bt.place(relx = .45, rely = .7)
s_bt = Button(rps_game,text='SCISSOR', width=6, height=2, bg='blue', fg='black',command=scissors)
s_bt.place(relx = .72, rely = .7)

Note how I'm passing references to the functions rock , paper and scissors to the Button constructors.

Edit:

Updated because the place method does not return a value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM