I want to have 2 multiple selection combobox returning/showing the values of the list. My code looks like that:
import tkinter as tk
from tkinter import ttk
class Combo(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
menubutton = tk.Menubutton(self, text=nom[k][i],
indicatoron=True, borderwidth=1, relief="raised")
menu = tk.Menu(menubutton, tearoff=False)
menubutton.configure(menu=menu)
menubutton.pack(padx=10, pady=10)
self.choices = {}
for choice in (data[k][i]):
self.choices[choice] = tk.IntVar(value=0)
# print(self.choices[choice].get())
#print(self.choices.get())
menu.add_checkbutton(label=choice, variable=self.choices[choice],
onvalue=1, offvalue=0,command=self.show)
def show(self):
# global selec
value = []
for choice in (data[k][i]):
value.append(self.choices[choice].get())
print(value)
# selec = value
return value
root = tk.Tk()
canva = tk.Canvas(root, width = 130,height = 50)
canva.pack(fill = "both", expand = True)
pos = Combo(root)
pos = canva.create_window(125,50,anchor = "nw",window = pos)
i=0
k = 1
a = Combo(root)
a = canva.create_window(125,50,anchor = "nw",window = a)
k = 2
b = Combo(root)
b = canva.create_window(225,50,anchor = "nw",window = b)
root.mainloop()
A huge part of this code comes from this topic : How do I enable multiple selection of values from a combobox?
For now it works for the second combobox but there is an error on the first:
Traceback (most recent call last):
File "C:", line 1892, in __call__
return self.func(*args)
File "c:dbcreation.py", line 285, in show
value.append(self.choices[choice].get())
KeyError: 'Manual'
New contributor
Enzo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.