在 Windows 下,当存在 tk 滚动条时,tk 似乎会间歇性地丢失运动事件。 在 linux 下的东西如预期的那样。
当您在未注释 tk.Scrollbar 的情况下运行下面的代码并在右侧框架上拖动手指时,您会看到滚动条在移动并且没有运动事件的输出。如果您等待几秒钟并再次在帧上拖动手指,运动事件将再次出现。
当您使用 ttk.Scrollbar 时,一切正常。
有没有办法让旧的 tk.Scrollbar 正常工作,还是我被迫使用 ttk.Scrollbar?
在使用 activepython 3.8.2 的 windows 10 下使用触摸屏会出现此问题。在 linux 下它工作正常。
import tkinter as tk
import tkinter.ttk as ttk
class Example(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
frame = tk.Frame(root)
box = tk.Listbox(frame)
box.pack(side=tk.LEFT)
# this scrollbar interferes with motion events on the frame
scrollbar = tk.Scrollbar(frame, takefocus=tk.NO)
# this scrollbar does not interferes with motion events on the frame
# scrollbar = ttk.Scrollbar(pg1, takefocus=tk.NO)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
box.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=box.yview)
# fill the listbox so a scrollbar is needed
for i, item in enumerate(map(str, range(1, 20))):
box.insert(i, item)
frame.pack(side=tk.LEFT)
self.frame = tk.Frame(width=400, height=400)
self.frame.bind("<ButtonPress-1>", self.allevts)
self.frame.bind("<B1-Motion>", self.allevts)
self.frame.pack(side=tk.RIGHT, expand=tk.YES, fill=tk.BOTH, anchor=tk.N)
def allevts(self, event):
print(event)
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()