簡體   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