I created a group of comboboxes from looping a list and used dict key as item em combolist. Problems: I cannot get back results to a 'results' list. All comboboxes change at same time. I´ve studied other posts about the subject, but I cant understand the concept involved in the solution. To sum: The code generate 3 combobox but they change togeter, and I cant append results.
Thanks for your help, in advance:
#-----------------------code
from tkinter import
from tkinter import ttk
win = Tk()
win.geometry('400x600')
win.title('combobox')
result =[] #--> the new list with results of comboboxes
opts = StringVar()
anyFruits =['any_grape', 'any_tomato', 'any_banana']
#--> list just to generate the loop
fruits = {'specialgrape':'specialgrape', 'specialtomato':'specialtomato','specialbanana':'specialbanana'}
#--> dictonary to generate drop down options menu (key of dict)
for index, fruit in enumerate(anyFruits):
mycombo = ttk.Combobox(win,
values= (list(fruits.keys())),textvariable=opts)
mycombo.bind('<<ComboboxSelected>>', lambda event, index=index: fruit_callBack(index, event))
mycombo.pack()
def fruit_callBack(index, event):
for opts in mycombo:
result.append(opts)
def print_():
print(result)
bt = Button(win,command= print_)
bt.pack()
win.mainloop()
CodePudding user response:
The value all change together because all Combobox
have same textvariable
. Hence change in one, will force the others to keep the value same. Anyway, you are appending it wrong too, you do not need to pass in index
, just pass the combobox itself and append the value inside it:
for index, fruit in enumerate(anyFruits):
mycombo = ttk.Combobox(win,values=list(fruits.keys()))
mycombo.bind('<<ComboboxSelected>>',lambda e,cmb=mycombo: fruit_callBack(e,cmb))
mycombo.pack()
def fruit_callBack(event,cmb):
result.append(cmb.get())