如何根据Optionmenu选择通过函数修改全局变量

时间:2017-12-19 15:09:02

标签: python tkinter configuration optionmenu

我是python的新手,我正在尝试使用tkinter optionmenu创建产品配置器。我需要使用多个optionmenus,所选的每个值都应该为下一个optionmenu创建下一组数据。我已经研究了我能想到的所有事情,但没有用。

注意:第二个选项菜单位于注释中,因为它不适用于当前配置。此外,总共需要4个选项,所有选项都基于之前选择的值。

编辑代码:

from tkinter import *


def openForm():
    print('Open Form')
    return


def openParts():
    print('Open Parts')
    return


capChoice = ['No Lift Selected']


def capFilter(liftSelection):
    global capChoice
    if liftSelection == 'Arm':
        capChoice = ['50kg', '100kg', '200kg', '300kg']
    elif liftSelection == 'Arm (Food Grade)':
        capChoice = ['75kg']
    elif liftSelection == 'Rail':
        capChoice = ['125kg', '300kg']
    elif liftSelection == 'Drive':
        capChoice = ['125kg', '300kg']
    print(capChoice)


lengthChoice = ['No Capacity Selected']


def lengthFilter(lengthSelection):
    global lengthChoice
    if lengthSelection == '50kg' and capChoice == 'Arm':
        lengthChoice = ['3m']
    elif lengthSelection == '75kg':
        lengthChoice = ['4.2m']
    elif lengthSelection == '100kg' and capChoice == 'Arm':
        lengthChoice = ['3m', '4m', '5m']
    elif lengthSelection == '125kg':
        lengthChoice = ['N/A']
    elif lengthSelection == '200kg' and capChoice == 'Arm':
        lengthChoice = ['3m', '4m', '5m']
    elif lengthSelection == '300kg' and capChoice == 'Arm':
        lengthChoice = ['3m', '4m']
    elif lengthSelection == '300kg' and capChoice == 'Rail':
        lengthChoice = ['N/A']
    elif lengthSelection == '300kg' and capFilter() == 'Drive':
        lengthChoice = ['N/A']


app = Tk()
app.title('QL Form')
app.geometry('560x460+200+200')

menubar = Menu(app)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label='Form', command=openForm)
filemenu.add_command(label='Req Parts', command=openParts)
filemenu.add_separator()
filemenu.add_command(label='Quit', command=app.quit)
menubar.add_cascade(label='Page', menu=filemenu)

app.config(menu=menubar)

liftType = StringVar()
liftType.set('Lift Type')
files = ['Arm', 'Arm (Food Grade)', 'Rail', 'Drive']
liftDropDown = OptionMenu(app, liftType, *files, command=capFilter)
liftDropDown.pack()

liftCap = StringVar()
liftCap.set('Capacity')
capDropDown = OptionMenu(app, liftCap, *capChoice, command=lengthFilter)
capDropDown.pack()

liftLength = StringVar()
liftLength.set('Length')
capDropDown = OptionMenu(app, liftLength, *lengthChoice, command=lengthFilter)
capDropDown.pack()


app.mainloop()

之前的代码:

from tkinter import *


def openForm():
    print('Open Form')
    return


def openParts():
    print('Open Parts')
    return


def capFilter(selection):
    global capChoice
    if selection == 'Arm':
        capChoice = ['50kg', '100kg', '200kg', '300kg']
    elif selection == 'Arm (Food Grade)':
        capChoice = ['75kg']
    elif selection == 'Rail':
        capChoice = ['125kg', '300kg']
    elif selection == 'Drive':
        capChoice = ['125kg', '300kg']
    print(capChoice)
    return capChoice



def lengthFilter(selection):
    if selection == '50kg' and capFilter() == 'Arm':
        lengthChoice = ['3m']
    elif selection == '75kg':
        lengthChoice = ['4.2m']
    elif selection == '100kg' and capFilter() == 'Arm':
        lengthChoice = ['3m', '4m', '5m']
    elif selection == '125kg':
        lengthChoice = ['N/A']
    elif selection == '200kg' and capFilter() == 'Arm':
        lengthChoice = ['3m', '4m', '5m']
    elif selection == '300kg' and capFilter() == 'Arm':
        lengthChoice = ['3m', '4m']
    elif selection == '300kg' and capFilter() == 'Rail':
        lengthChoice = ['N/A']
    elif selection == '300kg' and capFilter() == 'Drive':
        lengthChoice = ['N/A']
    return lengthChoice


app = Tk()
app.title('QL Form')
app.geometry('560x460+200+200')

menubar = Menu(app)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label='Form', command=openForm)
filemenu.add_command(label='Req Parts', command=openParts)
filemenu.add_separator()
filemenu.add_command(label='Quit', command=app.quit)
menubar.add_cascade(label='Page', menu=filemenu)

app.config(menu=menubar)

liftType = StringVar()
liftType.set('Lift Type')
files = ['Arm', 'Arm (Food Grade)', 'Rail', 'Drive']
liftDropDown = OptionMenu(app, liftType, *files, command=capFilter)
liftDropDown.pack()


'''
liftCap = StringVar()
liftCap.set('Capacity')
capDropDown = OptionMenu(app, liftCap, *capChoice, command=lengthFilter)
capDropDown.pack()
'''

app.mainloop()

1 个答案:

答案 0 :(得分:0)

cap_choice在开始时复制cap_choice的值,但之后它不会使用它 - 所以如果你甚至更改cap_filter中的值,它将无效。

cap_dropdown内,您必须直接在import tkinter as tk def cap_filter(selection): options = { 'Arm': ['50kg', '100kg', '200kg', '300kg'], 'Arm (Food Grade)': ['75kg'], 'Rail': ['125kg', '300kg'], 'Drive': ['125kg', '300kg'], } if selection in options: items = options[selection] else: items = [] print(items) # remove old elements cap_dropdown['menu'].delete(0, 'end') # add new items for text in items: cap_dropdown['menu'].add_command(label=text, command=lambda arg=text:length_filter(arg)) def length_filter(selection): type_ = lift_type.get() if selection == '50kg' and type_ == 'Arm': length_choice = ['3m'] elif selection == '75kg': length_choice = ['4.2m'] elif selection == '100kg' and type_ == 'Arm': length_choice = ['3m', '4m', '5m'] elif selection == '125kg': length_choice = ['N/A'] elif selection == '200kg' and type_ == 'Arm': length_choice = ['3m', '4m', '5m'] elif selection == '300kg' and type_ == 'Arm': length_choice = ['3m', '4m'] elif selection == '300kg' and type_ == 'Rail': length_choice = ['N/A'] elif selection == '300kg' and type_ == 'Drive': length_choice = ['N/A'] else: length_choice = ['N/A'] print(length_choice) app = tk.Tk() lift_type = tk.StringVar() lift_type.set('Lift Type') files = ['Arm', 'Arm (Food Grade)', 'Rail', 'Drive'] lift_dropdown = tk.OptionMenu(app, lift_type, *files, command=cap_filter) lift_dropdown.pack() lift_cap = tk.StringVar() lift_cap.set('Capacity') cap_choice = [''] cap_dropdown = tk.OptionMenu(app, lift_cap, *cap_choice, command=length_filter) cap_dropdown.pack() app.mainloop()

中替换项目
{{1}}