多个组合框

2024-04-20 03:57:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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

如果在第一个组合框上选择了哪个列表,则无法获取在第二个组合框上选择的列表元素

如果选择了第一个组合框“a”,则需要到达第二个组合框“a”元素或“b”元素或“c”元素


Tags: 元素列表widthcenterstatefontttkreadonly
1条回答
网友
1楼 · 发布于 2024-04-20 03:57:03

只需从列表中创建一个字典,并让<<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()

相关问题 更多 >