tkinter 选项菜单的虚拟事件

Virtual Events for tkinter Optionmenu

我已经四处寻找了很长一段时间,但我找不到我要找的东西,所以如果这不存在或者不是最好的方法,请告诉我.

我希望能够在从 Optionmenu()

中选择一个选项时触发事件

我也想让它使用虚拟事件,类似这样:my_tree.bind('<<TreeviewSelect>>', function)

有没有我可以找到 tkinter 小部件的所有虚拟事件的列表?

I want to be able to trigger and event when an option is selected from an Optionmenu()

这样做的方法是向与 OptionMenu 关联的变量添加跟踪。

colors = ("red", "orange", "yellow", "green", "blue", "indigo", "violet")
color_var = tk.StringVar(value=colors[0])
om = tk.OptionMenu(root, color_var, *colors)


def color_callback(varname, idx, mode):
    print(f"the color changed to {root.getvar(varname)}")
color_var.trace_add(("write", "unset"), color_callback)

I also want it to use virtual events, similar to this: my_tree.bind('<>', function)

OptionMenu 不支持虚拟活动,但您可以根据需要创建虚拟活动。在跟踪的回调中,您可以生成一个虚拟事件,然后您可以将其绑定到:

def create_virtual_event(varname, idx, mode):
    om.event_generate("<<OptionMenuChanged>>")
color_var.trace_add(("write", "unset"), create_virtual_event)

def color_callback(event):
    print(f"the color changed to {color_var.get()}")
om.bind("<<OptionMenuChanged>>", color_callback)

Is there a list that I can find all the virtual events for tkinter widgets?

所有预定义虚拟事件的规范列表在 tcl/tk events man page