首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python 3 unicode编解码器在tkinter中绑定鼠标滚轮时出错

Python 3 unicode编解码器在tkinter中绑定鼠标滚轮时出错
EN

Stack Overflow用户
提问于 2018-01-04 05:32:35
回答 1查看 557关注 0票数 3

在MacOS上使用Python3.6/tkinter,我在画布中创建了一个框架,并将滚动条绑定到它。这一切都运行得很好。问题是,当光标位于可滚动的框架中时,我试图捕获MouseWheel事件。我设置了一个绑定:

代码语言:javascript
运行
复制
main_window.bind("<MouseWheel>",on_mousewheel)

并创建了一个简短的虚拟事件处理程序:

代码语言:javascript
运行
复制
def on_mousewheel(event):
    print(event.delta)

每次我使用滚轮时,Python都会响应:

代码语言:javascript
运行
复制
Traceback (most recent call last):
  File "/Users/Gary/IPPS/opendb.py", line 160, in <module>
    main_window.mainloop()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1277, in mainloop
    self.tk.mainloop(n)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

代码如下:

代码语言:javascript
运行
复制
from tkinter import *

def on_main_window_resize(event):
    canvas.configure(scrollregion=canvas.bbox('all'))

def on_mousewheel(event):
    print(event.delta)

main_window = Tk()
main_window.bind("<MouseWheel>",on_mousewheel)
canvas = Canvas(main_window, width=500, height=500)
canvas.pack(side=LEFT, fill=BOTH, expand=YES)
cust_scroll = Scrollbar(main_window, orient=VERTICAL, command=canvas.yview)
cust_scroll.pack(side=RIGHT, fill=Y, expand=NO)
cust_list = Frame(canvas)
canvas['yscrollcommand'] = cust_scroll.set
canvas.bind('<Configure>', on_main_window_resize)
canvas.create_window((0,0), window=cust_list, anchor=NW)
main_window.grid_columnconfigure(0, weight=1)
main_window.grid_rowconfigure(0, weight=1)

memberbuttons = list();
for member in range(1,20):
    memberbutton = Button(cust_list, text="BUTTON", justify=LEFT, pady=2)
    memberbutton.pack(side=TOP, fill=X)
main_window.mainloop()

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2018-10-22 08:30:05

我发现一些在线参考资料对理解这个问题很有帮助,它似乎发生在旧版本的ActiveTcl (8.5.x)上。

Inertial scrolling in Mac OS X with Tkinter and Python

nltk/nltk on Github - OS X: downloader GUI crashes in Python 3 when mouse wheel is used for scrolling

solution将把您的ActiveTlc版本升级到>=8.6,您可以通过两种方式完成此操作:

通过Python Release 3.7.1上的二进制软件包

  • manually
  • installing最新版本的Python3.7.1
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48085420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档