14

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?

neptune798
  • 141
  • 1
  • 3

3 Answers3

10

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.

Kevin Kostlan
  • 3,311
  • 7
  • 29
  • 33
1

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.

Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • This does not appear to solve the problem. Possibly the bug was re-introduced? – dougalg May 23 '14 at 03:45
  • Have you installed ActiveTcl 8.5.15? – Ned Deily May 23 '14 at 07:21
  • 1
    I just solved the issue. The problem is with the version of Python I had from Homebrew. After installing the official python3 release it worked fine. – dougalg May 25 '14 at 13:10
  • I still have this problem, running Python 3.4.1 and updating my ActiveTcl. Any other ideas (for anyone looking at an old question)? – en_Knight Aug 04 '14 at 00:31
  • Are you using the python.org 64-bit 3.4.1 installer for OS X and ActiveTcl 8.5.15.1? – Ned Deily Aug 04 '14 at 02:54
  • I'm having exactly the same issue with the official Python 3.4 and ActiveTcl 8.5. Has anyone found something that works? – Michal Mar 08 '15 at 19:01
1

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.

Mark Bentley
  • 151
  • 1
  • 5