Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove ttk Combobox Mousewheel Binding

I have a ttk Combobox I'd like to unbind from the mousewheel so that scrolling with the wheel while the Combobox is active doesn't change the value (instead it scrolls the frame).

I've tried unbind as well as binding to and empty function but neither works. 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.

Thanks!

like image 833
Andrew Brown Avatar asked May 30 '17 17:05

Andrew Brown


1 Answers

The default binding is on the internal widget class, which gets executed after any custom bindings that you add. You can remove that default binding which will affect your whole app, or you can bind a specific widget to a custom function that returns the string "break" which will prevent the default binding from being run.

Remove the class binding

The internal class of the ttk combobox is TCombobox. You can pass that to 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 a binding on an individual returns the string "break" which will prevent default bindings 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"
like image 79
Bryan Oakley Avatar answered Nov 13 '22 09:11

Bryan Oakley