我正在为带有 7 英寸触摸屏的 python 树莓派制作一个信息亭应用程序。 一切正常,现在,我正在尝试使滚动条像在触摸屏上一样工作。我知道 raspbian 没有合适的触摸界面,所以,屏幕上的每一次触摸都相当于鼠标点击,如果我移动手指触摸屏幕,就像拖动功能一样。
为了在 python 上实现这个,我使用了我修改过的版本代码 Vertical Scrolled Frame使用 Canvas ,我需要添加事件绑定(bind) <ButtonPress-1>
和 <B1-Motion>
.
<ButtonPress-1>
可能会保存点击的 y 位置并启用 bind_all
<B1-Motion>
的功能.
<B1-Motion>
可能会将滚动设置向上或向下设置 <ButtonPress-1>
保存的 y 值之间的差异和 event.y
本次事件。
<ButtonRelease-1>
可能会禁用 bind_all
函数 <unbind_all>
卷轴的。
我添加的事件代码是这样的,但我不知道如何让它正常工作 .yview
Canvas 的功能,使我的功能按预期工作。
def moving(event):
#In this part I don't know how to make my effect
self.canvas.yview('scroll',event.y,"units")
def clicked(event):
global initialy
initialy = event.y
self.canvas.bind_all('<B1-Motion>', moving)
def released(event):
self.canvas.unbind_all('<B1-Motion>')
self.canvas.bind_all('<ButtonPress-1>',clicked)
self.canvas.bind_all('<ButtonRelease-1>', released)
最佳答案
服用 Saad的代码作为基础,我对其进行了修改以使其适用于每个 S.O. (win, linux,mac) 使用 yview_moveto
并且我应用了一些修改,正如我在此处解释的那样。
编辑:我已经编辑了代码以显示完整的类(class)。
class VerticalScrolledFrame(Frame):
"""A pure Tkinter scrollable frame that actually works!
* Use the 'interior' attribute to place widgets inside the scrollable frame
* Construct and pack/place/grid normally
* This frame only allows vertical scrolling
"""
def __init__(self, parent, bg,*args, **kw):
Frame.__init__(self, parent, *args, **kw)
# create a canvas object and a vertical scrollbar for scrolling it
canvas = Canvas(self, bd=0, highlightthickness=0,bg=bg)
canvas.pack(side=LEFT, fill=BOTH, expand=TRUE)
# reset the view
canvas.xview_moveto(0)
canvas.yview_moveto(0)
self.canvasheight = 2000
# create a frame inside the canvas which will be scrolled with it
self.interior = interior = Frame(canvas,height=self.canvasheight,bg=bg)
interior_id = canvas.create_window(0, 0, window=interior,anchor=NW)
# track changes to the canvas and frame width and sync them,
# also updating the scrollbar
def _configure_interior(event):
# update the scrollbars to match the size of the inner frame
size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
canvas.config(scrollregion="0 0 %s %s" % size)
if interior.winfo_reqwidth() != canvas.winfo_width():
# update the canvas's width to fit the inner frame
canvas.config(width=interior.winfo_reqwidth())
interior.bind('<Configure>', _configure_interior)
def _configure_canvas(event):
if interior.winfo_reqwidth() != canvas.winfo_width():
# update the inner frame's width to fill the canvas
canvas.itemconfigure(interior_id, width=canvas.winfo_width())
canvas.bind('<Configure>', _configure_canvas)
self.offset_y = 0
self.prevy = 0
self.scrollposition = 1
def on_press(event):
self.offset_y = event.y_root
if self.scrollposition < 1:
self.scrollposition = 1
elif self.scrollposition > self.canvasheight:
self.scrollposition = self.canvasheight
canvas.yview_moveto(self.scrollposition / self.canvasheight)
def on_touch_scroll(event):
nowy = event.y_root
sectionmoved = 15
if nowy > self.prevy:
event.delta = -sectionmoved
elif nowy < self.prevy:
event.delta = sectionmoved
else:
event.delta = 0
self.prevy= nowy
self.scrollposition += event.delta
canvas.yview_moveto(self.scrollposition/ self.canvasheight)
self.bind("<Enter>", lambda _: self.bind_all('<Button-1>', on_press), '+')
self.bind("<Leave>", lambda _: self.unbind_all('<Button-1>'), '+')
self.bind("<Enter>", lambda _: self.bind_all('<B1-Motion>', on_touch_scroll), '+')
self.bind("<Leave>", lambda _: self.unbind_all('<B1-Motion>'), '+')
关于python - 触摸屏滚动 Tkinter Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56165257/