繁体   English   中英

Python 3 unicode编解码器错误绑定tkinter中的鼠标滚轮

[英]Python 3 unicode codec error binding mousewheel in tkinter

在MacOS上使用Python 3.6 / tkinter,我在画布中创建了一个框架并将滚动条绑定到它。 一切正常。 问题是当我尝试在光标位于可滚动框架中时阻止MouseWheel事件。 我设置了一个绑定:

main_window.bind("<MouseWheel>",on_mousewheel)

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

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

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

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

这是代码:

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()

有任何想法吗?

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

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

Github上的nltk / nltk - OS X:当鼠标滚轮用于滚动时,下载器GUI在Python 3中崩溃

解决方案您的ActiveTlc版本升级>=8.6 ,您可以通过两种方式执行此操作:

暂无
暂无

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

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