如何根据组合框选择更改多个标签? [英] How to change multiple Labels based on Combobox selection?
问题描述
我想知道如何根据组合框
下拉列表选择在不同的标签中具有多个值。例如,假设您有一个 Combobox
,其值是( car
, house
,计算机
)和多个标签
,它们在选择组合框
。如果选择 car
,则尺寸为:大
,颜色:黑色
等...
I would like yo know how to have multiple values in different labels depending on a Combobox
drop down list selection. For instance, suppose you have one Combobox
with the values (car
, house
, computer
) and multiple Label
s which reflect different sizes and colors when selecting Combobox
. If I select car
, I will have for size: big
, color: black
and so on...
推荐答案
Combobox小部件会生成虚拟事件< ;< ComboboxSelected>>
,您可以根据当前值更改标签的选项:
The Combobox widget generates the virtual event <<ComboboxSelected>>
, which you can use to change the options of the labels according to the current value:
import Tkinter as tk
import ttk
values = ['car', 'house', 'computer']
root = tk.Tk()
labels = dict((value, tk.Label(root, text=value)) for value in values)
def handler(event):
current = combobox.current()
if current != -1:
for label in labels.values():
label.config(relief='flat')
value = values[current]
label = labels[value]
label.config(relief='raised')
combobox = ttk.Combobox(root, values=values)
combobox.bind('<<ComboboxSelected>>', handler)
combobox.pack()
for value in labels:
labels[value].pack()
root.mainloop()
这篇关于如何根据组合框选择更改多个标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!