neptune798
neptune798

Reputation: 141

Inertial scrolling in Mac OS X with Tkinter and Python

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?

Upvotes: 14

Views: 3676

Answers (3)

Mark Bentley
Mark Bentley

Reputation: 151

Neptune798, It should work. Apparently this bug has resurfaced in ActiveTcl 8.6. It's definitely a bug with Tk, as I encountered the same issue testing with Python 3.4.4, 3.5.4, and 3.6.2. All of them reported using the Tcl/Tk libraries installed in:

**/System**/Library/Frameworks/Tcl.framework/Versions/8.5/ 

I encountered this bug with ActiveTcl 8.6.6 specifically, and after downgrading to 8.5.18.0 it went away. Checking what Python was using after the downgrade, it reported:

>>> import tkinter
>>> root = tkinter.Tk()
>>> print(root.tk.exprstring('$tcl_library'))
/Library/Frameworks/Tcl.framework/Versions/8.5/Resources/Scripts
>>> print(root.tk.exprstring('$tk_library'))
/Library/Frameworks/Tk.framework/Versions/8.5/Resources/Scripts

Notice it's finding ActiveTcl in just /Library now, not /System/Library

Although they do not offer old releases for download any longer, I was able to find an old link that still works here

With Python 3.7, Tcl/Tk is being bundled with Python, and tkinter is no longer relying on the system's Tcl/Tk version. I've tested both the CPython release, and the Anaconda release, and both work fine with the bundled Tcl/Tk 8.6 included.

Upvotes: 1

Kevin Kostlan
Kevin Kostlan

Reputation: 3519

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.

Upvotes: 10

Ned Deily
Ned Deily

Reputation: 85095

This looks like the problem described here. If you are using the python.org 64-bit/32-bit installer for 3.3 (currently 3.3.2), make sure you've also installed the latest ActiveTcl release, currently 8.5.13, as noted here.

Upvotes: 1

Related Questions