OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Combobox with multiple selection returning string values

  • Thread starter Thread starter Enzo
  • Start date Start date
E

Enzo

Guest
I want to have 2 multiple selection combobox returning/showing the values of the list. My code looks like that:

Code:
data = [[],[]]
nom = [[],[]]
data[0] = [['01_Flat', '02_Curv', '03_RS', '04_RNS', '05_Esyr', '06_Fsyr', '07_AI']]
data[1] = [['Manual', 'ADS laser', 'BD laser', '1UP']]
nom[0] = ['Part']
nom[1] = ['Tag cutting']


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 = 530,height = 500)
canva.pack(fill = "both", expand = True)

i=0 
k = 0

a = Combo(root)
a = canva.create_window(125,50,anchor = "nw",window = a)

i = 0
k = 1

pos = Combo(root)
pos = canva.create_window(225,50,anchor = "nw",window = pos)

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:

Code:
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'
<p>I want to have 2 multiple selection combobox returning/showing the values of the list. My code looks like that:</p>
<pre><code>data = [[],[]]
nom = [[],[]]
data[0] = [['01_Flat', '02_Curv', '03_RS', '04_RNS', '05_Esyr', '06_Fsyr', '07_AI']]
data[1] = [['Manual', 'ADS laser', 'BD laser', '1UP']]
nom[0] = ['Part']
nom[1] = ['Tag cutting']


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],
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]):
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]):
value.append(self.choices[choice].get())
print(value)
# selec = value
return value

root = tk.Tk()

canva = tk.Canvas(root, width = 530,height = 500)
canva.pack(fill = "both", expand = True)

i=0
k = 0

a = Combo(root)
a = canva.create_window(125,50,anchor = "nw",window = a)

i = 0
k = 1

pos = Combo(root)
pos = canva.create_window(225,50,anchor = "nw",window = pos)

root.mainloop()
</code></pre>
<p>A huge part of this code comes from this topic : <a href="https://stackoverflow.com/questions...-multiple-selection-of-values-from-a-combobox">How do I enable multiple selection of values from a combobox?</a></p>
<p>For now it works for the second combobox but there is an error on the first:</p>
<pre><code>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'
</code></pre>
 

Latest posts

A
Replies
0
Views
1
Abraham Zsombor Nagy
A
A
Replies
0
Views
1
Abraham Zsombor Nagy
A
Top