473,395 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Make Tkinter canvas respond to MouseWheel event

Hello. When I tried to make Tkinter canvas widget respond to
MouseWheel event on Windows XP, I failed. The canvas just doesn't
receive MouseWheel event. I used bind_all to find out which widget
receives this event and the result showed that only the top Tk widget
gets it. This is really annoying. I wonder if this problem exists on
other platform. I googled, but found no appliable solution, so I wrote
one as following. It's not perfect, but works well for me. Please
comment on my implementation, and if you have better solution, please
show it.

In this implementation, I bind the MouseWheel to the toplevel window
which contains the canvas.
To test if the canvas shall scroll, I check if the coordinates of the
mouse is within the canvas.

from Tkinter import *

class ScrolledCanvas(Canvas):
def __init__(self, parent, xscroll=False, yscroll=False, *args,
**kargs):
frm = Frame(parent)
frm1 = Frame(frm)
Canvas.__init__(self, frm1, *args, **kargs)
self.pack(side=LEFT, fill=BOTH, expand=YES)

frm1.pack(side=TOP, fill=BOTH, expand=YES)
frm2 = Frame(frm)
frm2.pack(side=BOTTOM, expand=YES, fill=X)

if xscroll:
xsbar = Scrollbar(frm2)
xsbar.config(orient=HORIZONTAL)
xsbar.config(command=self.xview)
self.config(xscrollcommand=xsbar.set)
xsbar.pack(fill=BOTH, expand=YES)
self.winfo_toplevel().bind('<Control-MouseWheel>',
self.onMouseWheelX)
if yscroll:
ysbar = Scrollbar(frm1)
ysbar.config(orient=VERTICAL)
ysbar.config(command=self.yview)
self.config(yscrollcommand=ysbar.set)
ysbar.pack(side=RIGHT, fill=BOTH, expand=YES)
self.winfo_toplevel().bind('<MouseWheel>',
self.onMouseWheelY)
self.pack = frm.pack # because the canvas is not contained in
a frame
self.canvasFrame = frm
def onMouseWheelY(self, event):
if event.widget == self.winfo_toplevel() and \
self.bInCanvas(event.x, event.y):
self.yview_scroll(-event.delta, UNITS)
def onMouseWheelX(self, event):
if event.widget == self.winfo_toplevel() and \
self.bInCanvas(event.x, event.y):
self.xview_scroll(-event.delta, UNITS)
def bInCanvas(self, x, y):
if x self.canvasFrame.winfo_x() and \
y self.canvasFrame.winfo_y() and \
x < self.canvasFrame.winfo_x() + int(self.winfo_width())
and \
y < self.canvasFrame.winfo_y() + int(self.winfo_height()):
return True
return False
def test():
top = Frame()
top.pack(expand=YES, fill=BOTH)
sc = ScrolledCanvas(top, xscroll=True, yscroll=True, bg='Brown',
relief=SUNKEN)
sc.config(scrollregion=(0,0,1000, 1000))
sc.config(yscrollincrement=1)
sc.config(xscrollincrement=1)
sc.pack()

for i in range(10):
sc.create_text(150, 50+(i*100), text='spam'+str(i),
fill='beige')
top.mainloop()

if __name__ == '__main__':
test()

Mar 1 '07 #1
0 2465

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: syed_saqib_ali | last post by:
Please take a look at and run the code snippet shown below. It creates a canvas with vertical & Horizontal scroll-bars. If you shrink the window to smaller than the area of the canvas, the...
3
by: Gigs_ | last post by:
how to write text on canvas. i know that i need to use canvas.create_text, but how to write text than when i create_text? or how to access object ID in canvas and change some options? thanks...
2
TMS
by: TMS | last post by:
Schools over!!! Now its time to play. I would like to learn how to make objects move from one location to the next on a canvas widget. For example: from Tkinter import * class square:...
4
by: Davy | last post by:
Hi all, I have written a simple Tkinter program, that is draw a rectangle in a canvas, when I press Up key, the rectangle move up. But the program seems work not properly? My environment is...
4
by: skanemupp | last post by:
mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\elmapovic.gif') w.create_image(10, 10, image = mapq, anchor = NW) after doing this is there any possibility of getting the characteristics of...
4
by: skanemupp | last post by:
when calling function hmm here, what do i get? the widget i clicked on? if i have a canvs on wich i have a bitmap and i click on the bitmap, is the event.widget then the bitmap? can i get info...
3
by: skanemupp | last post by:
so i load a gif onto a canvas and when i click the canvs i want to get the color of the pixel that is clicked. so i need to ge the object im clicking. i was told in another thread to use...
3
by: joshdw4 | last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes, it's that pesky root window and I have tried withdraw to no avail. I'm assuming this is because of the methods I'm using. I...
1
by: Eric_Dexter | last post by:
I saw this (close to this anyway) lieing around on the internet and was wanting to use it to define a start point exc but I need the graphics to stay within a set y coords and I am not realy sure...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.