Android中是否有Tkinter的可滚动查看器?

时间:2019-05-12 00:33:54

标签: android python tkinter

我正在使用python在Android上使用tkinter的应用程序上。 我可以在Appstore上使用PyDroid3运行tkinter代码。 但是我在滚动视图方面遇到了一些问题。

在tkinter中似乎有些小部件,即。 (“文字”)可以读取手机上的滚动轻拂手势来上下滚动行。但是,我试图创建一个也可以复制此行为的查看器。

我尝试添加带有滚动条的框架,但未注册滚动手势。 我曾尝试使用鼠标移动和释放来实现自己的滚动查看器,但不如“文本”小部件上的手势识别器一样。

from tkinter import *

global canvas,scrolly


def on_scrollwheel(event):
    global canvas,scrolly
    # canvas.yview_scroll(-1*(event.delta/120), "units")
    mag = 2
    scrollVal = mag*int(-1*event.delta/abs(event.delta))
    canvas.yview_scroll(scrollVal, "units")


def on_mousemove(event):
    global canvas,scrolly
    # canvas.yview_scroll(-1*(event.delta/120), "units")
    # scrollVal = int(-1*event.delta/abs(event.delta))

    try:
        print(scrolly)
        scrollVal = event.y - scrolly
        if(abs(scrollVal) > 10):
            print(scrollVal)
            scrollDir = int(scrollVal/abs(scrollVal))
            canvas.yview_scroll(scrollDir,'units')
            scrolly = event.y
    except NameError:
        scrolly = event.y

def on_mousedown(event):
    global scrolly
    scrolly = event.y



def on_mouseup(event):
    global canvas,scrolly
    scrollVal = event.y - scrolly
    print(scrollVal)
    if(abs(scrollVal) > 1):
        print(scrollVal)
        for i in range(3):
            scrollDir = int(scrollVal/abs(scrollVal))
            canvas.yview_scroll(scrollDir,'units')
    del scrolly


def on_configure(event):
    # update scrollregion after starting 'mainloop'
    # when all widgets are in canvas
    canvas.configure(scrollregion=canvas.bbox('all'))



def data():
    for i in range(200):
       Label(frame,text=i).grid(row=i,column=0)
       Label(frame,text="my text"+str(i)).grid(row=i,column=1)
       Label(frame,text=".....asdfasdfasdfasdfadsfadsf.....").grid(row=i,column=2)

def myfunction(event):
    canvas.configure(scrollregion=canvas.bbox("all"),width=600,height=1100)

root=Tk()
sizex = 400
sizey = 400
posx  = 100
posy  = 100
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))

myframe=Frame(root,relief=GROOVE,width=600,height=1100,bd=1)
myframe.place(x=10,y=10)

# --- create canvas with scrollbar ---
global canvas
canvas=Canvas(myframe)
frame=Frame(canvas)
myscrollbar=Scrollbar(myframe,orient="vertical",command=canvas.yview)
canvas.configure(yscrollcommand=myscrollbar.set)

myscrollbar.pack(side="right",fill="y")
canvas.pack(side="left")
canvas.create_window((0,0),window=frame,anchor='nw')
frame.bind("<Configure>",myfunction)
data()

# Scroll Approach 1
canvas.bind_all("<B1-Motion>", on_mousemove)
canvas.bind_all("<Button-1>", on_mousedown)
canvas.bind_all("<ButtonRelease-1>", on_mouseup)
canvas.bind_all("<MouseWheel>", on_scrollwheel)

while True:
    try:
        root.mainloop()
        break
    except UnicodeDecodeError:
        pass

0 个答案:

没有答案