繁体   English   中英

不止一个组合框

[英]More than one combobox

我在第一个 ComboBox 上选择列表名称。 无法在第二个组合框上显示元素。 (等 x=[1,2,3] - y=[4,5,6,7] - z=[8,9] 如果在第一个组合框上选择了 x,我想在第二个组合框。)

İÇECEKLER = ['Su', 'Maden Suyu', 'Çay', 'Kahve', 'Nescafe', 'Kola', 'Fanta', 'Sprite']
YİYECEKLER = ['Simit', 'Poğaça', 'Açma', 'Boyoz', 'Tahinli', 'Kumru', 'Tost', 'Kumpir']
TATLILAR = ['Sütlaç', 'Puding', 'Aşure', 'Revani', 'Pasta', 'Künefe', 'Waffle', 'Tiramisu']
DİĞER = ['KDV', 'Servis Bedeli']

value0 = StringVar ()
value1 = StringVar ()
value2 = StringVar ()
value3 = StringVar ()

KategoriKutu = Frame (root, width = 1000, height = 300, bd = 16, relief = 'raise')
KategoriKutu.pack (side = TOP)
Kategori = Frame (KategoriKutu, width = 800, height = 300, bd = 8, relief = 'raise')
Kategori.pack (side = LEFT)

KategoriSeç = ttk.Combobox (Kategori, textvariable = value0, state = 'readonly', width = 16, justify = "center", font = ('arial', 30, 'bold'))
KategoriSeç ['values'] = ('', 'İÇECEKLER', 'YİYECEKLER', 'TATLILAR', 'DİĞER')
KategoriSeç.current()
KategoriSeç.grid (row = 0, column = 0)

ÜrünSeç = ttk.Combobox (Kategori, textvariable = value1, state = 'readonly', width = 16, justify = "center", font = ('arial', 30, 'bold'))
ÜrünSeç ['values'] = ('', )
ÜrünSeç.current()
ÜrünSeç.grid (row = 1, column = 0)

root.mainloop()

这是让我困惑的地方。

a = ['Su', 'Maden Suyu', 'Çay', 'Kahve', 'Nescafe', 'Kola', 'Fanta', 'Sprite']
b = ['Simit', 'Poğaça', 'Açma', 'Boyoz', 'Tahinli', 'Kumru', 'Tost', 'Kumpir']
c = ['Sütlaç', 'Puding', 'Aşure', 'Revani', 'Pasta', 'Künefe', 'Waffle', 'Tiramisu']
d = ['KDV', 'Servis Bedeli']

我总共有四个列表,它们都有不同的元素。

a_combo = ttk.Combobox(Kategori, state = 'readonly',width = 16, justify = "center", font = ('arial', 30, 'bold'))
a_combo ['values'] = ('', 'a', 'b', 'c', 'd')
a_combo.grid (row = 0, column = 0)
a_combo.bind("<<ComboboxSelected>>",lambda e: set_next_combo(e,b_combo,a))

b_combo = ttk.Combobox(Kategori, state = 'readonly', width = 16, justify = "center", font = ('arial', 30, 'bold'))
b_combo.grid (row = 1, column = 0)

如果我在第一个 ComboBox 上选择了哪个列表,我将无法获取我在第二个 ComboBox 上选择的列表元素。

如果第一个组合框是 'a' 选择,第二个组合框 'a' 元素或 'b' 选择的 'b' 元素或 'c' 选择的 'c' 元素需要到达。

只需从您的列表中创建一个字典,并让您的<<ComboboSelected>>事件调用相应的列表。

from tkinter import *
from tkinter import ttk

root = Tk()
test_dict = {
"a" : ['Su', 'Maden Suyu', 'Çay', 'Kahve', 'Nescafe', 'Kola', 'Fanta', 'Sprite'],
"b" : ['Simit', 'Poğaça', 'Açma', 'Boyoz', 'Tahinli', 'Kumru', 'Tost', 'Kumpir'],
"c" : ['Sütlaç', 'Puding', 'Aşure', 'Revani', 'Pasta', 'Künefe', 'Waffle', 'Tiramisu'],
"d" : ['KDV', 'Servis Bedeli']}

KategoriKutu = Frame (root, width = 1000, height = 300, bd = 16, relief = 'raise')
KategoriKutu.pack (side = TOP)
Kategori = Frame (KategoriKutu, width = 800, height = 300, bd = 8, relief = 'raise')
Kategori.pack (side = LEFT)

def set_next_combo(event,widget):
    result = test_dict.get(a_combo.get(),[])
    widget["values"] = result
    widget.set(result[0] if result else " ")

a_combo = ttk.Combobox(Kategori, state = 'readonly',width = 16, justify = "center", font = ('arial', 30, 'bold'))
a_combo ['values'] = ('', 'a', 'b', 'c', 'd')
a_combo.grid (row = 0, column = 0)
a_combo.bind("<<ComboboxSelected>>",lambda e: set_next_combo(e,b_combo))

b_combo = ttk.Combobox(Kategori, state = 'readonly', width = 16, justify = "center", font = ('arial', 30, 'bold'))
b_combo.grid (row = 1, column = 0)

root.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM