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!
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.
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>")
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With