Tk MouseWheel Support

Dick Holmes encore at dslextreme.com
Fri Mar 11 14:49:44 EST 2011


On Thu, 10 Mar 2011 21:56:52 +0100, Alexander Kapps
<alex.kapps at web.de> wrote:

<snip>
>Can you post your code please (if it's too long, strip it down to 
>the smallest program which still shows the problem.)

First, thanks to MRAB for showing me how to get the wheel working.

In the following code (Windows only), rolling the wheel doesn't invoke
the event method. If I change the mouse wheel binding to
self.master.bind... the event gets called and the scrolling works.
Note that the scrolling occurs even when the mouse is outside the
listbox. I guess I'll have to look at the mouse position when I enter
the wheel_event method to see if the mouse is over the listbox.

# mouse wheel in listbox
import Tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid()
        
        self.button = tk.Button(self, text='Button')
        self.button.grid(row=0, column=0)
        
        self.yscroll = tk.Scrollbar(self, orient=tk.VERTICAL)
        self.yscroll.grid(row=1, column=1, sticky=tk.N+tk.S)
        self.list = tk.Listbox(self, selectmode=tk.SINGLE, width=40,
                                height=5,
yscrollcommand=self.yscroll.set)
        self.list.grid(row=1, column=0, sticky=tk.N+tk.S+tk.E+tk.W)
        self.yscroll["command"] = self.list.yview
        self.list.bind("<MouseWheel>", self.wheel_event)
        for i in xrange(10):
            self.list.insert(tk.END, 'line ' + str(i))
        
    def wheel_event(self, evt):
        lines = evt.delta // 120
        self.list.yview_scroll(-lines, tk.UNITS)
        
root = tk.Tk()
root.geometry('300x300+400+200')
root.title('  Wheel Test')
app = Application(master = root)
root.mainloop()




More information about the Python-list mailing list