在 Mac OS X 中使用 Tkinter 和 Python 进行惯性滚动 [英] Inertial scrolling in Mac OS X with Tkinter and Python
问题描述
我正在开发一个使用 Tkinter 作为窗口管理器的 Python 3.3 项目.我为画布设置了鼠标滚轮事件.滚动在 Windows 7、8 和 Ubuntu 中有效,但在 Mac OS X Mountain Lion 中使用 Magic Mouse 滚动时,程序崩溃并在 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
我的代码是:
self._hscroll.configure( command=self._canvas.xview )
self._vscroll.configure( command=self._canvas.yview )
self._canvas.bind('<MouseWheel>', lambda event: self.rollWheel(event))
其中 hscroll 和 vscroll 是表单中的滚动条对象.
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)
我是否必须更新 Tk 才能使此功能正常工作,还是只是整体损坏?
Do I have to update Tk to make this functionality work, or is it just broken in general?
推荐答案
可以捕获这些错误:
while True:
try:
root.mainloop()
break
except UnicodeDecodeError:
pass
这似乎工作得很好,甚至可以惯性滚动,而且不需要任何安装/升级.
This seems to work perfectly, even scrolling inertially, and does not require any installation/upgrading.
这篇关于在 Mac OS X 中使用 Tkinter 和 Python 进行惯性滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!