触摸屏滚动Tkinter Python

触摸屏滚动Tkinter Python,python,tkinter,scroll,touchscreen,Python,Tkinter,Scroll,Touchscreen,我正在为一个7英寸触摸屏的python树莓pi制作一个信息亭应用程序。 一切都很好,现在,我正试图使卷轴的作品,因为它在触摸屏上。我知道raspbian没有合适的触摸界面,所以,屏幕上的每一次触摸都像鼠标点击一样工作,如果我移动手指触摸屏幕,就像拖动功能一样工作 为了在python上实现这一点,我使用canvas使用我修改过的版本代码,并且我需要添加事件绑定和 可能会保存单击的y位置,并为启用bind_all功能 可以设置滚动设置,设置保存的y值与此事件的事件.y之间的差值 可能会禁用滚动条的绑

我正在为一个7英寸触摸屏的python树莓pi制作一个信息亭应用程序。 一切都很好,现在,我正试图使卷轴的作品,因为它在触摸屏上。我知道raspbian没有合适的触摸界面,所以,屏幕上的每一次触摸都像鼠标点击一样工作,如果我移动手指触摸屏幕,就像拖动功能一样工作

为了在python上实现这一点,我使用canvas使用我修改过的版本代码,并且我需要添加事件绑定

可能会保存单击的y位置,并为
启用
bind_all
功能

可以设置滚动设置,设置
保存的y值与此事件的
事件.y
之间的差值

可能会禁用滚动条的
绑定所有功能

我添加的事件代码如下,但我不知道如何使用画布的
.yview
功能使其正常工作,以使我的功能按预期工作

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)
def移动(事件):
#在这一部分我不知道如何发挥我的作用
self.canvas.yview('scroll',event.y,“units”)
已单击的定义(事件):
全局初始值
initialy=event.y
self.canvas.bind_all(“”,移动)
def释放(事件):
self.canvas.unbind_all(“”)
self.canvas.bind_all(“”,单击)
self.canvas.bind_all(“”,已发布)

我修改了
垂直滚动框架
,只使用了几行代码,可以按照您的意愿滚动。我使用测试了代码,它可以很好地使用鼠标。将以下代码添加到垂直滚动框架中

self.offset_y = 0
def on_press(evt):
    self.offset_y = evt.y_root

def on_touch_scroll(evt): 
    if evt.y_root-self.offset_y<0: 
        evt.delta = -1
    else: 
        evt.delta = 1
    # canvas.yview_scroll(-1*(evt.delta), 'units') # For MacOS
    canvas.yview_scroll( int(-1*(evt.delta/120)) , 'units') # For windows

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>'), '+')
self.offset_y=0
def开启按钮(evt):
self.offset_y=evt.y_根
def在触摸屏上滚动(evt):
如果evt.y_root-self.offset_y以的代码为基础,我已经使用
yview_moveto
对它进行了修改,使它在每个s.O.(win、linux、mac)上都能工作,并且我已经应用了一些修改,正如我在这里解释的那样

编辑:我已经编辑了代码以显示完整的类

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>'), '+')
类垂直滚动帧(帧):
“”“一个真正有效的纯Tkinter可滚动框架!
*使用“内部”属性将小部件放置在可滚动框架内
*正常建造和包装/放置/网格
*此框架仅允许垂直滚动
"""
定义初始值(自身、父项、背景、*args、**kw):
帧。\uuuuu初始化(自,父,*args,**kw)
#创建画布对象和用于滚动的垂直滚动条
canvas=canvas(self,bd=0,highlightthickness=0,bg=bg)
canvas.pack(side=LEFT,fill=BOTH,expand=TRUE)
#重置视图
canvas.xview_moveto(0)
canvas.yview_moveto(0)
self.canvashheight=2000
#在画布内创建一个框架,该框架将与其一起滚动
self.interior=interior=Frame(画布,高度=self.canvasheight,bg=bg)
内部\u id=画布。创建\u窗口(0,0,窗口=内部,锚定=NW)
#跟踪画布和帧宽度的更改并同步它们,
#同时更新滚动条
def_配置_内部(事件):
#更新滚动条以匹配内部框架的大小
大小=(interior.winfo_reqwidth(),interior.winfo_reqheight())
canvas.config(scrollregion=“0%s%s”%size)
if interior.winfo_reqwidth()!=canvas.winfo_width():
#更新画布的宽度以适合内部框架
canvas.config(width=interior.winfo_reqwidth())
内部绑定(“”,\u配置\u内部)
定义配置画布(事件):
if interior.winfo_reqwidth()!=canvas.winfo_width():
#更新内部框架的宽度以填充画布
canvas.itemconfigure(interior\u id,width=canvas.winfo\u width())
canvas.bind(“”,\u configure\u canvas)
self.offset_y=0
self.prevy=0
self.scrollposition=1
def on_按下(事件):
self.offset_y=event.y_root
如果self.scroll位置<1:
self.scrollposition=1
elif self.scrollposition>self.canvasheight:
self.scrollposition=self.canvasheight
canvas.yview\u moveto(self.scrollposition/self.canvasheight)
def在触摸屏上滚动(事件):
nowy=event.y\u根
截面移动=15
如果nowy>self.prevy:
event.delta=-sectionmoved
elif nowy
我想我明白你想做什么,我做了一些类似于
yview\u moveto()
的事情,但没有按预期工作,但是,你的代码对我不起作用,我不知道我是否做错了,事件会正确触发,但是,滚动仍然不起作用。它与
事件.delta有关,因为它们在不同的系统上是不同的。看看这个。我知道在windows上你用120除以delta,但不确定raspberry pi。我正在windows下测试它,但我认为
yview\u moveto()
的工作原理类似。我正在修改您的部分代码,以使其按预期工作。非常感谢您的发言。是的,我试过了,也可以使用
yview\u moveto()
来完成,但这需要更多的代码行和一个相应变化的变量,该变量不会随画布窗口的大小而变化。例如,如果画布窗口的大小很大,那么根据修改的val,滚动可能会减慢