python触屏模块_触摸屏滚动Tkinter Python

本文介绍了一个基于Tkinter的Python触屏滚动框架,通过Canvas和垂直滚动条实现,适用于Windows、Linux和Mac操作系统。示例代码详细展示了如何创建和管理内部框架,以及如何响应触摸滚动事件来移动内容。
摘要由CSDN通过智能技术生成

以Saad的代码为基础,我使用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_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_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("", lambda _: self.bind_all('', on_press), '+')

self.bind("", lambda _: self.unbind_all(''), '+')

self.bind("", lambda _: self.bind_all('', on_touch_scroll), '+')

self.bind("", lambda _: self.unbind_all(''), '+')

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值