我想要 2 个多重选择组合框返回/显示列表的值。我的代码看起来像这样:
data = [[],[]]
nom = [[],[]]
data[0] = [['01_Flat', '02_Curv', '03_RX', '04_RY', '05_RZ', '06_Fsyr', '07_AI']]
data[1] = [['Manual', 'Laser', 'Gamma', 'Proto']]
nom[0] = ['Part']
nom[1] = ['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()
此代码的很大一部分来自此主题:如何从组合框中启用多个值选择?
目前它适用于第二个组合框,但第一个组合框有错误:
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'
坦率地说,我不知道问题是什么,因为首先我改变了它的工作方式 - 使其更具可读性。
首先,我将所有数据直接放入字典中,而不使用一些
data[0]
或其他变量nom
data = {
'Part': ['01_Flat', '02_Curv', '03_RX', '04_RY', '05_RZ', '06_Fsyr', '07_AI'],
'Cutting': ['Manual', 'Laser', 'Gamma', 'Proto']
}
接下来我显式发送密钥和数据到 Combo
Combo(root, key='Part', data=data['Part'])
后来我使用
self.
将所有元素保留在类中 - 我不访问外部数据。
我使用可读变量
key
而不是 k
class Combo(tk.Frame):
def __init__(self, parent, key, data):
tk.Frame.__init__(self, parent)
self.key = key
self.data = data
self.menubutton = tk.Menubutton(self, text=self.key,
indicatoron=True, borderwidth=1, relief="raised")
self.menu = tk.Menu(self.menubutton, tearoff=False)
self.menubutton.configure(menu=self.menu)
self.menubutton.pack(padx=10, pady=10)
self.choices = {}
for item in self.data:
self.choices[item] = tk.BooleanVar(value=False)
self.menu.add_checkbutton(label=item, variable=self.choices[item],
onvalue=True, offvalue=False, command=self.show)
和函数
show()
我也改为使用self.data和self.choices
def show(self):
value = [self.choices[item].get() for item in self.data]
print(value)
return value
因为这个函数给出了 0,1 的列表(在我的版本中
True
,False`),这是不可读的,所以我创建了仅使用选定元素创建列表的函数
def get_selected(self):
value = [item for item in self.data if self.choices[item].get() is True ]
return value
一切工作都没有问题。
完整的工作代码:
data = {
'Part': ['01_Flat', '02_Curv', '03_RX', '04_RY', '05_RZ', '06_Fsyr', '07_AI'],
'Cutting': ['Manual', 'Laser', 'Gamma', 'Proto']
}
import tkinter as tk
from tkinter import ttk
class Combo(tk.Frame):
def __init__(self, parent, key, data):
tk.Frame.__init__(self, parent)
self.key = key
self.data = data
self.menubutton = tk.Menubutton(self, text=self.key,
indicatoron=True, borderwidth=1, relief="raised")
self.menu = tk.Menu(self.menubutton, tearoff=False)
self.menubutton.configure(menu=self.menu)
self.menubutton.pack(padx=10, pady=10)
self.choices = {}
for item in self.data:
self.choices[item] = tk.BooleanVar(value=False)
self.menu.add_checkbutton(label=item, variable=self.choices[item],
onvalue=True, offvalue=False, command=self.show)
def show(self):
value = [self.choices[item].get() for item in self.data]
print(value, self.get_selected())
return value
def get_selected(self):
value = [item for item in self.data if self.choices[item].get() is True ]
return value
def show_all_selections():
results.delete('0.0', 'end')
results.insert('end', f'{combo_a.key}: {combo_a.get_selected()}\n')
results.insert('end', f'{combo_pos.key}: {combo_pos.get_selected()}\n')
root = tk.Tk()
canva = tk.Canvas(root, width=530, height=200)
canva.pack(fill="both", expand=True)
combo_a = Combo(root, key='Part', data=data['Part'])
a = canva.create_window(125, 50, anchor="nw", window=combo_a)
combo_pos = Combo(root, key='Cutting', data=data['Cutting'])
pos = canva.create_window(225, 50, anchor="nw", window=combo_pos)
results = tk.Text(root, height=4)
results.pack()
button = tk.Button(root, text="Show all selected", command=show_all_selections)
button.pack()
root.mainloop()