簡體   English   中英

更新選項菜單中的選項 Tkinter

[英]Updating options in optionmenu Tkinter

我目前正在寫一個小的愛好項目,我在使用下拉菜單時遇到了關於我的列表“骰子”的問題,它只顯示列表的第一次迭代(單個 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”按鈕定義中創建下拉菜單,但這並不是真正想要做的。 提前致謝。

編輯:固定拼寫。

這是你想要的嗎? 我將optionmenu移動到roll() function

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])
    dd1= OptionMenu(root,prpp,dice[0])
    dd2= OptionMenu(root,prpp,dice[1])
    dd3= OptionMenu(root,prpp,dice[2])
    dd4= OptionMenu(root,prpp,dice[3])
    dd5= OptionMenu(root,prpp,dice[4])
    dd6= OptionMenu(root,prpp,dice[5])

    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)

    

#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')
 
#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)
 

root.mainloop()

之前的結果:

在此處輸入圖像描述

之后的結果:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM