如何 :触控板滑动(从左到右)在Tkinter中水平滚动条



我一直在寻找将触控板滑动事件绑定到水平滚动条的方法。

我已经能够将<mousewheel>绑定到垂直滚动条(vsb(并将<shift-mousewheel>绑定到水平滚动条(hsb(,但仍然想实现触控板滑动(尽管两者都工作正常(。

注意:我尝试使用<Button-6><Button-7>,如水平边缘滚动的Tkinter事件是什么(在Linux中(中建议的那样?并且还尝试像在Python TKinter中一样检查event.num:检测触摸板上的水平滚动,但不知何故它只对按钮(1,2,3..(做出反应,而不是对"应该"是触控板滑动的6和7做出反应

下面是一些可以尝试的示例代码:

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

提前致谢:)

在我的系统 Debian GNU/Linux 11(靶心(上,对应于使用触摸板水平滚动的序列是'<Shift-Button-4>''<Shift-Button-5>'。您可以进行以下更改以开始使用。

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

或者,您可以使用单个滚动处理程序,该处理程序在执行任何滚动操作之前检查状态的 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.

你在Linux上吗?如果是,则应使用 '<Button-4>''<Button-5>' 进行垂直滚动,而不是'<MouseWheel>'

最新更新