tkinter 中的 Python 3 unicode 编解码器错误绑定鼠标滚轮 [英] Python 3 unicode codec error binding mousewheel in tkinter

查看:30
本文介绍了tkinter 中的 Python 3 unicode 编解码器错误绑定鼠标滚轮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using Python 3.6/tkinter on MacOS, I created a frame within a canvas and bound a scrollbar to it. This all works fine. The problem is when I try to snag MouseWheel events when the cursor is in the scrollable frame. I set up a binding:

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

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

and created a short dummy event handler:

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

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

Every time I use the scroll wheel, Python responds with:

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) 上.

I found some references online useful to grasp the problem, it seems to happen on an older version of ActiveTcl (8.5.x).

在 Mac OS X 中使用 Tkinter 和蟒蛇

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

解决方案是将ActiveTlc版本升级>=8.6,你可以这样做两种方式:

The solution is to upgrade your version of ActiveTlc to >=8.6 and you can do this in two ways:

这篇关于tkinter 中的 Python 3 unicode 编解码器错误绑定鼠标滚轮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆