编辑组合框时拦截事件 [英] Intercept event when combobox edited
问题描述
我使用 ComboBox
作为更大的 GUI 的一部分,用 python/tkinter 编写.
I'm using a ComboBox
as part of a larger GUI, written in python/tkinter.
当下拉选择更改时,颜色会更新以向用户表明某些内容已更改.
When the drop-down selection is changed, the color is updated to indicate to the user that something has changed.
然而,组合框也允许用户输入他们自己的值.我还希望在发生这种情况时颜色发生变化.
However, the combobox also allows the user to type in their own value. I also want the color to change when this happens.
问题是,我在 ComboBox
文档 指示如何执行此操作.
The problem is, I don't see anything in the ComboBox
documentation indicating how to do this.
推荐答案
我注意到在 Python 的 Tkinter
文档的某处,它提到 Combobox
是 的子类>入口
.
I noticed that somewhere in Python's Tkinter
docs, it mentions that Combobox
is a subclass of Entry
.
使用 Entry
,这就是您要做的.设置配置:
With an Entry
, here's what you do. Set the configuration:
-validatecommand
应该设置为您希望在按下某个键时发生的事情(在我的情况下,更改颜色).-validate
应该设置为key
,这意味着当光标在条目中时,只要按下一个键,就会调用验证命令.
-validatecommand
should be set to the thing that you want to happen when a key is pressed (in my case, change a color).-validate
should be set tokey
, meaning that the validation command will be called whenever a key is pressed while the cursor is in the entry.
这里是有关文本条目的 tk 页面以供进一步了解参考.
Here's the tk page on text entries for further reference.
对组合框执行完全相同的操作的方式相同(据我所知).
Doing the exact same thing for comboboxes works just the same way (as far as I can tell).
这篇关于编辑组合框时拦截事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!