Python 3 unicode codec error binding mousewheel in tkinter

自作多情 提交于 2019-12-10 20:39:34

问题


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)

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

Here is the code:

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

Any ideas?


回答1:


I found some references online useful to grasp the problem, it seems to happen on an older version of 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

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

  • manually
  • installing the latest version of Python 3.7.1 via the binary package on Python Release 3.7.1


来源:https://stackoverflow.com/questions/48085420/python-3-unicode-codec-error-binding-mousewheel-in-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!