Newbie: Tkinter Mouse Wheel Support

Johnny Cass johnnycass at icon.co.za
Mon Mar 20 16:58:24 EST 2000


OK, so here is how you do it. This script was adapted from an example
in Johny E. Grayson's Python and Tkinter Programming. It creates a simple
list box and allows you to scroll up and down the list using the mouse wheel:

from Tkinter import *

def scrollDown(event):
    list.yview_scroll(2, 'units')
def scrollUp(event):
    list.yview_scroll(-2, 'units')

root = Tk()
root.option_readfile('optionDB')
root.title('Scrollbar')
list = Listbox(root, height=6, width=15)
scroll = Scrollbar(root, command=list.yview)
list.configure(yscrollcommand=scroll.set)
list.pack(side=LEFT)
scroll.pack(side=RIGHT, fill=Y)
list.bind('<Button-4>', scrollUp)
list.bind('<Button-5>', scrollDown)

for item in range(30):
    list.insert(END, item)

root.mainloop()

It works fine, but I suspect that it doesn't implement the callbacks in an
optimal (correct / best) way...

Johnny Cass wrote:

> Mashe,
>
> I'm using RedHat 6.1 (i.e. XWindows). How and / or to what do I make the
> binding?
>
> Thanks for your help.
> - Johnny
>
> Moshe Zadka wrote:
>
> > On Mon, 20 Mar 2000, Johnny Cass wrote:
> >
> > > What do you have to do to enable mouse wheel support in your Tkinter
> > > Python programs? (e.g. bind wheel to scroll bar etc.). Is there some
> > > kind of binding that you need to code?
> >
> > It would be nice to know the platform you're using. IIRC, mouse wheel on
> > X-Windows is simply buttons 4 and 5 (for wheel up and wheel down), so you
> > better bind to those. For windows, you'll have to wait for some windows
> > expert.
> >
> > HTH,
> > --
> > Moshe Zadka <mzadka at geocities.com>.
> > http://www.oreilly.com/news/prescod_0300.html
> > http://www.linux.org.il -- we put the penguin in .com


More information about the Python-list mailing list