删除 ttk 组合框鼠标滚轮绑定

And*_*own 8 python tkinter ttk

我有一个 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()
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激。

谢谢!

Bry*_*ley 9

默认绑定在内部小部件类上,它在您添加任何自定义绑定后执行。您可以删除会影响整个应用程序的默认绑定,或者您可以将特定小部件绑定到自定义函数,该函数返回"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>")
Run Code Online (Sandbox Code Playgroud)

添加自定义绑定

当对个人的绑定返回"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"
Run Code Online (Sandbox Code Playgroud)