Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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
Python 掷两个骰子v&#x;钮扣_Python_Python 3.x_Tkinter - Fatal编程技术网

Python 掷两个骰子v&#x;钮扣

Python 掷两个骰子v&#x;钮扣,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我正在尝试创建两个骰子,它们可以一起滚动,或者如果标记为keep则分别滚动。我尝试了以下代码。掷骰子按钮掷骰子。使用botton1和botton2,每个模具都可以标记为保留,因此如果按下滚动骰子,则不会滚动 import tkinter import random # toplevel widget of Tk which represents mostly the main window of an application root = tkinter.Tk() root.geometry(

我正在尝试创建两个骰子,它们可以一起滚动,或者如果标记为
keep
则分别滚动。我尝试了以下代码。
掷骰子按钮掷骰子。使用botton1和botton2,每个模具都可以标记为
保留
,因此如果按下
滚动骰子
,则不会滚动

import tkinter
import random

# toplevel widget of Tk which represents mostly the main window of an application
root = tkinter.Tk()
root.geometry('1200x600')
root.title('Roll Dice')

# label to display dice
label = tkinter.Label(root, text='', font=('Helvetica', 260))

# function activated by button
def switch1():
    button1["state"] = "disabled"
    
def switch2():
    button2["state"] = "disabled"

def roll_dice(var1, var2):
    # unicode character strings for dice
    dice = ['\u2680', '\u2681', '\u2682', '\u2683', '\u2684', '\u2685']

    
    if button1["state"] == "active" and button2["state"] == "active":
        var1 = f'{random.choice(dice)}'
        var2 = f'{random.choice(dice)}'
        label.configure(text= var1 + var2)
        label.pack()
    elif button1["state"] == "active":
        var2 = f'{random.choice(dice)}'
        label.configure(text= var1+var2)
        label.pack()
    elif button2["state"] == "active":
        var1 = f'{random.choice(dice)}'
        label.configure(text= var1 + var2)
        label.pack()

    
# button
dice = ['\u2680', '\u2681', '\u2682', '\u2683', '\u2684', '\u2685']
var1, var2 = [f'{random.choice(dice)}', f'{random.choice(dice)}']


button1 = tkinter.Button(root, text='keep', foreground='green', command=switch1, state = "active")
button2 = tkinter.Button(root, text='keep', foreground='green', command=switch2, state = "active")
button_roll = tkinter.Button(root, text='roll dice', foreground='green', command=lambda: roll_dice(var1, var2))
   
# pack a widget in the parent widget
button_roll.pack()
button1.pack()
button2.pack()

# call the mainloop of Tk
# keeps window open
root.mainloop()

当我运行代码时,标记为
keep
的骰子会再滚动一次。在那之后,当我掷骰子时,骰子不会改变。如何使它不再滚动一次?

command=roll_dice(var1,var2))
是错误的,因为您正在调用函数,而不是将其作为回调传递。使用lambda,比如
command=lambda:roll_dice(var1,var2)
,或者为回调定义一个单独的函数。

请发布完整的错误回溯。远离主题的评论:单数是die,复数是dice。谢谢,这很有效。但在激活
keep
后,骰子在变为非活动状态之前仍会改变一次