我有一个 ttk 组合框,我想从鼠标滚轮解除绑定,以便在组合框处于活动状态时使用滚轮滚动不会更改值(而是滚动框架)。
我尝试过解除绑定以及绑定到空函数,但都不起作用。见下文:
import Tkinter as tk
import ttk
class app(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.interior = tk.Frame(self)
tkvar = tk.IntVar()
combo = ttk.Combobox(self.interior,
textvariable = tkvar,
width = 10,
values =[1, 2, 3])
combo.unbind("<MouseWheel>")
combo.bind("<MouseWheel>", self.empty_scroll_command)
combo.pack()
self.interior.pack()
def empty_scroll_command(self, event):
return
sample = app()
sample.mainloop()
任何帮助将不胜感激。
Thanks!
默认绑定位于内部小部件类上,该类在您添加的任何自定义绑定之后执行。您可以删除会影响整个应用程序的默认绑定,或者您可以将特定的小部件绑定到返回字符串的自定义函数"break"
这将阻止运行默认绑定。
删除类绑定
ttk组合框的内部类是TCombobox
。你可以将其传递给unbind_class
:
# Windows & OSX
combo.unbind_class("TCombobox", "<MouseWheel>")
# Linux and other *nix systems:
combo.unbind_class("TCombobox", "<ButtonPress-4>")
combo.unbind_class("TCombobox", "<ButtonPress-5>")
添加自定义绑定
当对个体的绑定返回字符串时"break"
这将阻止处理默认绑定。
# Windows and OSX
combo.bind("<MouseWheel>", self.empty_scroll_command)
# Linux and other *nix systems
combo.bind("<ButtonPress-4>", self.empty_scroll_command)
combo.bind("<ButtonPress-5>", self.empty_scroll_command)
def empty_scroll_command(self, event):
return "break"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)