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

编程入门 行业动态 更新时间:2024-10-19 02:21:55
本文介绍了tkinter 中的 Python 3 unicode 编解码器错误绑定鼠标滚轮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

在 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:

手动通过 Python Release 上的二进制包安装最新版本的 Python 3.7.13.7.1

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

更多推荐

[db:关键词]

本文发布于:2023-04-30 06:52:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390698.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:鼠标   绑定   滚轮   错误   编解码器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!