I am pretty new to python and am trying to create a product configurator using tkinter optionmenu. I need to use multiple optionmenus and each value selected should create the next set of data for the next optionmenu. I have researched everything I can think to get it right, to no avail.
NOTE: the second optionmenu is in a note, as it is not working with the current configuration. Also, will need 4 optionmenus in total, all based on values selected before them.
Edited code:
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()
Code before:
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()
cap_dropdown
copies values from cap_choice
at start but later it doesn't use it - so if you even change values in cap_choice
it will not work.
Inside cap_filter
you have to replace items directly in 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()