不止一个组合框帮助了我

问题描述 投票:0回答:1

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

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

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

python tkinter combobox
1个回答
0
投票

只需从列表中创建一个字典,并让您的<<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()
© www.soinside.com 2019 - 2024. All rights reserved.