我的函数运行时无需通过tkinter按钮告知
因此,我在制作更换模具时遇到了麻烦 由于某种原因,我的功能(骰子滚轮)在我按下按钮之前运行, 这对我来说毫无意义 函数不是只有在被告知时才执行吗我的函数运行时无需通过tkinter按钮告知,tkinter,tkinter-canvas,Tkinter,Tkinter Canvas,因此,我在制作更换模具时遇到了麻烦 由于某种原因,我的功能(骰子滚轮)在我按下按钮之前运行, 这对我来说毫无意义 函数不是只有在被告知时才执行吗 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()
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()
问题来自生产线:
dice_button = tkinter.Button(window, text="hit button for die roll",height=10, width=80, command=dice_roller())
您正在调用dice\u 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
希望这有助于