[英]How to : trackpad swipe (left-right) for horizontal scrollbar in Tkinter?
I've been looking around for way to bind trackpad swipe event to horizontal scrollbar .我一直在寻找将触控板滑动事件绑定到水平滚动条的方法。
I've been able to bind <mousewheel>
to vertical scrollbar (vsb) and <shift-mousewheel>
to horizontal scrollbar (hsb), but still want to implement trackpad swipe (both work fine though).我已经能够将
<mousewheel>
绑定到垂直滚动条 (vsb) 并将<shift-mousewheel>
绑定到水平滚动条 (hsb),但仍然想实现触控板滑动(虽然两者都可以正常工作)。
note: I've tried to use <Button-6>
and <Button-7>
like suggested in What are the Tkinter events for horizontal edge scrolling (in Linux)?注意:我已经尝试使用
<Button-6>
和<Button-7>
就像What are the Tkinter events for horizontal edge scrolling (in Linux)? and also tried to check for the event.num
like in python tkinter: detecting horizontal scrolling on touchpad , but somehow it only react to button(1,2,3..) but not 6 and 7 which 'supposed' to be trackpad swipe并且还尝试检查
event.num
,如python tkinter: detecting horizontal scrolling on touchpad ,但不知何故它只对按钮(1,2,3..)做出反应,而不是“应该”是触控板滑动的 6 和 7
Here's some sample code to try out:这里有一些示例代码可以试用:
import tkinter as tk
from tkinter import ttk
def frame_conf(event):
"""
Reset the scroll region to encompass the inner frame
"""
canvas.configure(scrollregion=canvas.bbox("all"))
def scroll_vertical(event):
"""
Enable vertical scrolling by mouse scroll
"""
if vsb.get() != (0.0, 1.0):
canvas.yview_scroll(-1 * int(event.delta / 60), "units")
def scroll_horizontal(event):
"""
Enable horizontal scrolling by shift + mouse scroll
"""
if hsb.get() != (0.0, 1.0):
canvas.xview_scroll(-1 * int(event.delta / 60), "units")
def bound_to_mousewheel(event):
"""
Bound scrollbar to mouse wheel
"""
canvas.bind_all('<MouseWheel>', scroll_vertical)
canvas.bind_all('<Shift-MouseWheel>', scroll_horizontal)
def unbound_to_mousewheel(event):
"""
Unbound scrollbar to mouse wheel
"""
canvas.unbind_all('<MouseWheel>')
canvas.unbind_all('<Shift-MouseWheel>')
root = tk.Tk()
frame = ttk.Frame(root)
frame.pack()
canvas = tk.Canvas(frame)
canvas.pack(side="left", expand=True, fill="both")
# Setup horizontal scrollbar
hsb = ttk.Scrollbar(frame, command=canvas.xview, orient="horizontal")
canvas.configure(xscrollcommand=hsb.set)
hsb.pack(side="bottom", fill="x", before=canvas)
# Setup vertical scrollbar
vsb = ttk.Scrollbar(frame, command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)
vsb.pack(side="right", fill="y")
# Create main frame for other widget
widget_frame = tk.Frame(canvas, bg="blue")
canvas_frame = canvas.create_window((0, 0), window=widget_frame, anchor="nw")
# Dynamic frame setup
widget_frame.bind("<Configure>", frame_conf)
# Populate frame
for row in range(50):
tk.Label(widget_frame, text="%s" % row, width=3, borderwidth="1",
relief="solid").pack(side="left", anchor="n")
for row in range(50):
tk.Label(widget_frame, text="%s" % row, width=3, borderwidth="1",
relief="solid").pack(anchor="sw")
# Bind canvas to mousewheel
canvas.bind("<Enter>", bound_to_mousewheel)
canvas.bind("<Leave>", unbound_to_mousewheel)
root.mainloop()
Thanks in advance:)提前致谢:)
On my system, Debian GNU/Linux 11 (bullseye), the sequences corresponding to horizontally scrolling using the touchpad are '<Shift-Button-4>'
and '<Shift-Button-5>'
.在我的系统 Debian GNU/Linux 11(靶心)上,使用触摸板水平滚动对应的序列是
'<Shift-Button-4>'
和'<Shift-Button-5>'
。 You can make the following change to get started.您可以进行以下更改以开始使用。
def bound_to_mousewheel(event):
"""
Bound scrollbar to mouse wheel
"""
canvas.bind_all('<MouseWheel>', scroll_vertical)
canvas.bind_all('<Shift-MouseWheel>', scroll_horizontal)
canvas.bind_all('<Shift-Button-4>', lambda *args: canvas.xview(tk.SCROLL, -1, tk.UNITS))
canvas.bind_all('<Shift-Button-5>', lambda *args: canvas.xview(tk.SCROLL, 1, tk.UNITS))
Alternatively, you can have a single scroll handler which checks the LSB of the state before performing any scrolling action.或者,您可以有一个滚动处理程序,它在执行任何滚动操作之前检查 state 的 LSB。
def scroll(event):
if event.state & 1:
# Either Shift was held down while scrolling the mouse wheel
# or a horizontal two-finger swipe was performed on the touchpad.
# Scroll horizontally.
else:
# Either Shift was not held down while scrolling the mouse wheel
# or a vertical two-finger swipe was performed on the touchpad.
# Scroll vertically.
Are you on Linux?你在 Linux 吗? If yes, you should use
'<Button-4>'
and '<Button-5>'
for vertical scrolling, not '<MouseWheel>'
.如果是,您应该使用
'<Button-4>'
和'<Button-5>'
进行垂直滚动,而不是'<MouseWheel>'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.