Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的函数运行时无需通过tkinter按钮告知_Tkinter_Tkinter Canvas - Fatal编程技术网

我的函数运行时无需通过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
希望这有助于