【发布时间】:2023-01-25 01:21:09
【问题描述】:
我目前正在写一个小的爱好项目,我在使用下拉菜单时遇到了关于我的列表“骰子”的问题,它只显示列表的第一次迭代(单个 0),但它应该在下拉菜单中更新每次按下“掷骰子”按钮后的菜单。我怎么做?
from random import randint
from tkinter import *
root = Tk()
root.title('Hobbyprojekt')
count = -1
global dice
dice = [0]
prpp= IntVar()
diceshow=Label()
#defining funtions for buttons
def roll():
global count
global diceshow
global dice
count +=1
print(count)
if count >= 1:
dice=[]
for x in range (0,7) :
dice.append(randint(1,10))
#calculating the viable dice options
for x in range (0,2) :
dice.remove(min(dice))
if count >= 1:
diceshow.destroy()
print("destroyed")
diceshow=Label(root, text=dice)
diceshow.grid(row=0,column=1)
print(dice)
print(dice[1])
print(dice[2])
print(dice[3])
#setting up the test gui
button1 = Button(root, text='Roll the dice', command=roll)
label1= Label(text='choice1')
label2= Label(text='choice2')
label3= Label(text='choice3')
label4= Label(text='choice4')
label5= Label(text='choice5')
label6= Label(text='choice6')
dd1= OptionMenu(root,prpp,*dice)
dd2= OptionMenu(root,prpp,*dice)
dd3= OptionMenu(root,prpp,*dice)
dd4= OptionMenu(root,prpp,*dice)
dd5= OptionMenu(root,prpp,*dice)
dd6= OptionMenu(root,prpp,*dice)
#setting layout
button1.grid(row=0,column=0)
label1.grid(row=1,column=0)
label2.grid(row=2,column=0)
label3.grid(row=3,column=0)
label4.grid(row=4,column=0)
label5.grid(row=5,column=0)
label6.grid(row=6,column=0)
dd1.grid(row=1, column=1)
dd2.grid(row=2,column=1)
dd3.grid(row=3,column=1)
dd4.grid(row=4,column=1)
dd5.grid(row=5,column=1)
dd6.grid(row=6,column=1)
root.mainloop()
因此,由于我对 python 还很陌生,所以我真的不知道该怎么做。我唯一能想到的是在“diceroll”按钮定义中创建下拉菜单,但这并不是真正想要做的。提前致谢。
编辑:固定拼写。
【问题讨论】:
-
我不明白六个下拉菜单的用途,因为它们使用相同的变量
prpp(这意味着更改其中一个也会更改其他)并且只有一个选项0。此外,还不清楚单击按钮时您想在这些下拉菜单上更新什么。 -
你是对的,他们是不同的选择,我没有改变这个迭代中的变量名。我想更新的是:如果我在第一个下拉菜单中选择了一个选项,那么在其他下拉菜单中不应选择选择的选项......但据我所知,下拉菜单实际上没有更新功能。所以我需要解决每次选择一个选项时破坏原始下拉列表的问题。
标签: python tkinter updating optionmenu