Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inertial scrolling in Mac OS X with Tkinter and Python

Tags:

python

tkinter

tk

I am working on a Python 3.3 project that uses Tkinter as a Window manager. I have mouse scroll wheel events set up for a canvas. The scrolling works in Windows 7, 8, and Ubuntu, but upon scrolling with a Magic Mouse in Mac OS X Mountain Lion, the program crashes with teh following error in the Tk main loop:

File "/Users/xxxx/Documents/Repositories/tycoon/agentsim.py", line 291, in start
    self._root.mainloop()
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 1038, in mainloop
self.tk.mainloop(n)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe7 in position 0: invalid continuation byte

My code was:

self._hscroll.configure( command=self._canvas.xview )
self._vscroll.configure( command=self._canvas.yview )
self._canvas.bind('<MouseWheel>', lambda event: self.rollWheel(event))

where hscroll and vscroll are scrollbar objects in the form.

If I use a regular mouse, the problem doesn't occur. It also occurs when I try scroll with my trackpad (with inertial scrolling turned on)

Do I have to update Tk to make this functionality work, or is it just broken in general?

like image 690
neptune798 Avatar asked Jun 08 '13 04:06

neptune798


People also ask

What is scrolling with inertia on Mac?

with inertia: Scrolling comes to a gradual stop. without inertia: Scrolling stops immediately.

Can you use tkinter on Mac?

If you are using a Python from any current python.org Python installer for macOS (3.10. 0+ or 3.9. 0+), no further action is needed to use IDLE or tkinter. A built-in version of Tcl/Tk 8.6 will be used.


1 Answers

These errors can be caught:

while True:
    try:
        root.mainloop()
        break
    except UnicodeDecodeError:
        pass

This seems to work perfectly, even scrolling inertially, and does not require any installation/upgrading.

like image 102
Kevin Kostlan Avatar answered Oct 31 '22 17:10

Kevin Kostlan