tk 创建含垂直滚动条的frame

一、控件结构

tk 创建含垂直滚动条的frame

二 、代码如下

'''
python 实现带滑动框canvas
控件结构如下
myfram
    canvas
        frame myscrollbar
注意事项,
'''
from tkinter import *

class ScrollbarFrame(Frame):
    def __init__(self,master,**kw):
        myframe=Frame(master,kw)
        canvas=Canvas(myframe,kw)
        super().__init__(canvas,kw)
        myscrollbar=Scrollbar(myframe,orient="vertical",command=canvas.yview)
        myscrollbar.pack(side="right",fill="y")
        canvas.configure(yscrollcommand=myscrollbar.set)
        canvas.pack(side="left")
        canvas.create_window((0,0),window=self,anchor='nw')
        super().bind("<Configure>",self.myfunction)
        self.myframe=myframe
        self.canvas=canvas
        self.myscrollbar=myscrollbar
        #方法从self.myframe继承
        # text_meths = vars(Frame).keys()#Canvas的方法
        methods = vars(Pack).keys() | vars(Grid).keys() | vars(Place).keys()
        # methods = methods.difference(text_meths)

        for m in methods:
            if m[0] != '_' and m != 'config' and m != 'configure':
                setattr(self, m, getattr(self.myframe, m))
        
    def myfunction(self,event):
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))
    
    def destroy(self):
        super().destroy()
        self.canvas.destroy()
        self.myscrollbar.destroy()
        self.myframe.destroy()
        


    # def __str__(self):
    #     return str(self.myframe)

if __name__ == "__main__":

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


    root=Tk()
    sizex = 400
    sizey = 300
    posx  = 100
    posy  = 100
    root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
    a=ScrollbarFrame(root,bd=1)
    a.myframe.place(x=10,y=10)
    data()
    root.mainloop()

 

三、结果

tk 创建含垂直滚动条的frame

 

 

四、参考与感谢 

 

上一篇:Tkinter小工具程序


下一篇:使用TK框架中selectByPrimaryKey