Inertial scrolling in Mac OS X with Tkinter and Python

為{幸葍}努か 提交于 2019-11-30 01:52:04

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/16995969/inertial-scrolling-in-mac-os-x-with-tkinter-and-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!