删除 ttk Combobox 鼠标滚轮绑定

Remove ttk Combobox Mousewheel Binding

我有一个 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()

如有任何帮助,我们将不胜感激。

谢谢!

默认绑定在内部小部件 class 上,它会在您添加的任何自定义绑定之后执行。您可以删除会影响整个应用程序的默认绑定,或者您可以将特定小部件绑定到自定义函数,该自定义函数 returns 字符串 "break" 将阻止默认绑定 运行 .

删除 class 绑定

ttk 组合框的内部 class 是 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>")

添加自定义绑定

当对个人进行绑定时 returns 字符串 "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"