更新Tkinter中Menubutton Checkbuttons的标签

时间:2018-05-08 12:43:22

标签: python-2.7 tkinter

我想在单击一个复选按钮时使用更新功能更新menubutton中的复选框的标签文本。

到目前为止,我已经通过删除整个menubutton并重新创建它,但它不能完美地工作并增加了不必要的复杂性。以下是我到目前为止的情况:

from Tkinter import *

INGREDIENTS = ['cheese','ham','pickle','mustard','lettuce']


def print_ingredients(*args):
   values = [(ingredient, var.get()) for ingredient, var in data.items()]
   print values

results = []

def update():

    values = [(ingredient, var.get()) for ingredient, var in data.items()]

    for value in values:
        if value[1] == 1:
            results.append(value[0])
    print results

    for value in values:
        mb.menu.delete(0)

    for ingredient in INGREDIENTS:


        if ingredient in results:
            on_val = 0
            off_val = 1
            click = "Clicked!"
        else:
            on_val = 1
            off_val = 0
            click = ""

        var = IntVar()
        mb.menu.add_checkbutton(label=ingredient + " " + click, variable=var, onvalue = on_val, offvalue = off_val, command = update)
        data[ingredient] = var # add IntVar to the dictionary


data = {} # dictionary to store all the IntVars

top = Tk()

mb=  Menubutton ( top, text="Ingredients", relief=RAISED )
mb.menu  =  Menu ( mb, tearoff = 0 )
mb["menu"]  =  mb.menu

for ingredient in INGREDIENTS:
    var = IntVar()
    mb.menu.add_checkbutton(label=ingredient, variable=var, command = update)
    data[ingredient] = var # add IntVar to the dictionary

btn = Button(top, text="Print", command=print_ingredients)
btn.pack()

mb.pack()

top.mainloop()

有没有办法更新menubutton中的检查按钮的标签文本?

1 个答案:

答案 0 :(得分:1)

您可以trace附加到检查按钮的变量。如果在成分之后命名变量并将它们存储在dict中,则可以在跟踪的回调中获取成分和变量,并更改右侧索引处的条目:

from Tkinter import *

INGREDIENTS = ['cheese','ham','pickle','mustard','lettuce']

def update(var_name, *args):
    # Beacause we defined names for the BooleanVars, the first argument is the name of the changed Var
    # We named the Vars after the ingredients
    ingredient = var_name
    # Get the actual var from the dict
    var = data[var_name]
    # Get the index of the clicked ingredient
    i = INGREDIENTS.index(ingredient)
    # Check wether the checkbutton is clicked on or not
    if var.get() == True:
        # If checked, change label to include 'Clicked'
        mb.menu.entryconfigure(i, label = ingredient + ' Clicked!')
    else:
        # If unchecked, change label to just ingredient
        mb.menu.entryconfigure(i, label = ingredient)

data = {} # dictionary to store all the IntVars

top = Tk()

mb=  Menubutton ( top, text="Ingredients", relief=RAISED )
mb.menu  =  Menu ( mb, tearoff = 0 )
mb["menu"]  =  mb.menu

for ingredient in INGREDIENTS:
    # Create a Boolean variable with the name of the ingredient
    var = BooleanVar(name = ingredient)
    # Trace changes to the variable
    var.trace("w", update)
    # Create Checkbutton without command
    mb.menu.add_checkbutton(label=ingredient, variable=var)
    # Add variable to the dictionary
    data[ingredient] = var

mb.pack()

top.mainloop()