Remove ttk Combobox Mousewheel Binding

I have a ttk Combobox that I would like to unbind with the mouse wheel so that scrolling with the wheel while the Combobox is active does not change the value (it scrolls the frame instead).

I've tried unbind and also binding and empty function, but doesn't work. See below:

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()

      

Any help would be greatly appreciated.

Thank!

+3


source to share


1 answer


The default binding is in an internal widget class that runs after you add any custom bindings. You can remove the default binding which will affect your entire application, or you can bind a specific widget to a custom function that returns a string "break"

that will prevent the default binding from running.

Remove class binding

The inner class of the ttk combobox command TCombobox

. You can pass this 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>")

      



Adding a custom binding

When binding to an individual returns a string "break"

that will prevent the default binding from being processed.

# 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"

      

+3


source







All Articles