简体   繁体   English

使用Tkinter和Python在Mac OS X中进行惯性滚动

[英]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. 我正在开发一个使用Tkinter作为Window管理器的Python 3.3项目。 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: 滚动在Windows 7,8和Ubuntu中工作,但在Mac OS X Mountain Lion中使用Magic Mouse滚动时,程序会在Tk主循环中出现以下错误:

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. 其中hscroll和vscroll是表单中的滚动条对象。

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? 我是否必须更新Tk才能使此功能正常工作,或者它是否只是一般性地破坏了?

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. 这似乎工作得很好,甚至是惯性滚动,并且不需要任何安装/升级。

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.3(目前3.3.2)的python.org 64位/ 32位安装,请确保您还安装了最新版本ActiveTcl目前8.5.13,如注意到这里

Neptune798, It should work. 海王星798,它应该工作。 Apparently this bug has resurfaced in ActiveTcl 8.6. 显然,这个错误在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. 这绝对是Tk的一个错误,因为我在使用Python 3.4.4,3.5.4和3.6.2时遇到了同样的问题。 All of them reported using the Tcl/Tk libraries installed in: 所有这些都报告使用安装在以下位置的Tcl / Tk库:

**/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. 我特意在ActiveTcl 8.6.6中遇到了这个错误,在降级到8.5.18.0后它就消失了。 Checking what Python was using after the downgrade, it reported: 检查降级后Python使用了什么,它报告说:

>>> 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 注意它现在在/ Library中找到ActiveTcl,而不是/ 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. 使用Python 3.7,Tcl / Tk与Python捆绑在一起,而tkinter不再依赖于系统的Tcl / Tk版本。 I've tested both the CPython release, and the Anaconda release, and both work fine with the bundled Tcl/Tk 8.6 included. 我已经测试了CPython版本和Anaconda版本,并且两者都包含了捆绑的Tcl / Tk 8.6。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM