所以我在制作更换模具时遇到了麻烦
出于某种原因,我的功能(dice_roller)在我按下按钮之前运行, 这对我没有意义。
只有在被告知的情况下才执行功能吗?
import tkinter,random
from tkinter import messagebox
window=tkinter.Tk()
window.title("Aidan's Dice Roller")
canvas = tkinter.Canvas(window, width=900, height=900)
canvas.pack()
canvas.create_rectangle(0,0,900,900, fill='black')
messagebox.showwarning("Dice Roller", "Do you want to proceed?")
dice1 = tkinter.PhotoImage(file="C:\\Users\\2019200\\Desktop\\Dice\\dice1.gif")
dice2 = tkinter.PhotoImage(file="C:\\Users\\2019200\\Desktop\\Dice\\dice2.gif")
dice3 = tkinter.PhotoImage(file="C:\\Users\\2019200\\Desktop\\Dice\\dice3.gif")
dice4 = tkinter.PhotoImage(file="C:\\Users\\2019200\\Desktop\\Dice\\dice4.gif")
dice5 = tkinter.PhotoImage(file="C:\\Users\\2019200\\Desktop\\Dice\\dice5.gif")
dice6 = tkinter.PhotoImage(file="C:\\Users\\2019200\\Desktop\\Dice\\dice6.gif")
dice1 = tkinter.PhotoImage(file="C:\\Users\\2019200\\Desktop\\Dice\\dice1.gif")
canvas.create_image(450, 350, image=dice1)
while True:
x=random.randint(1,6)
print(x)
def dice_roller():
global x
if x==1:
canvas.create_image(450, 350, image=dice1)
elif x==2:
canvas.create_image(450, 350, image=dice2)
elif x==3:
canvas.create_image(450, 350, image=dice3)
elif x==4:
canvas.create_image(450, 350, image=dice4)
elif x==5:
canvas.create_image(450, 350, image=dice5)
elif x==6:
canvas.create_image(450, 350, image=dice6)
window.update()
dice_button = tkinter.Button(window, text="hit button for die roll",height=10, width=80, command=dice_roller())
dice_button.place(x=160, y=600)
window.mainloop()
答案 0 :(得分:1)
问题来自这条线:
dice_button = tkinter.Button(window, text="hit button for die roll",height=10, width=80, command=dice_roller())
您正在调用dice_roller()
函数,而不是将其分配给tkinter.Button
这里是固定线路:
dice_button = tkinter.Button(window, text="hit button for die roll",height=10, width=80, command=dice_roller) #Notice the removed brackets
希望有所帮助