【问题标题】:How to create tkinter scrollbar for frame [duplicate]如何为框架创建tkinter滚动条[重复]
【发布时间】:2020-05-15 03:23:40
【问题描述】:
from tkinter import *
from tkinter import ttk 

class uFrame(Frame):
    def __init__(self,parent, *args, **kw):
        Frame.__init__(self,parent,*args,**kw)
        sb = Scrollbar(self,orient=VERTICAL).pack(fill=Y,side=RIGHT)


main = Tk()
main.geometry('400x400')

#main
a = uFrame(main,bg="black")
#Top bar
b = Frame(main,bg="green")

b.pack(fill=X,side=TOP,ipady = 15)
a.pack(fill=BOTH,side=TOP,expand = YES)
# a.update()

bLabel = Label(b,text="User",bg="red").pack(side=LEFT,anchor = S)
cLabel = Label(b,text="HELLO",bg="blue").pack(side=RIGHT,ipadx = 10,anchor = S)
cLabel = Label(b,text="HELLO",bg="white").pack(side=RIGHT, ipadx = 10,anchor = S)

for i in range(10):
    Frame(a,height=50,bd = 5,bg="yellow",highlightbackground="red", highlightcolor="red", highlightthickness=1).pack(side=TOP,fill=X)


main.mainloop()   

你好!我正在制作 Messenger 的用户列表。 UserList 通过 Frame 显示 userInfo。因为一个 Frame 包括 Username、UserImage 和 UserStatus。

我已经为 userList Frame 制作了滚动条。和分离的用户信息框架。但我找不到如何绑定框架和滚动条。

请帮帮我...

【问题讨论】:

标签: python tkinter


【解决方案1】:
from tkinter import *
from tkinter import ttk 

class uFrame(Frame):
    def __init__(self, parent, *args, **kw):
        Frame.__init__(self, parent, *args, **kw)            

        # create a canvas object and a vertical scrollbar for scrolling it
        vscrollbar = Scrollbar(self, orient=VERTICAL)
        vscrollbar.pack(fill=Y, side=RIGHT, expand=FALSE)
        canvas = Canvas(self,bg="black", bd=0, highlightthickness=0,yscrollcommand=vscrollbar.set)
        canvas.pack(side=LEFT, fill=BOTH, expand=TRUE)
        vscrollbar.config(command=canvas.yview)

        # reset the view
        canvas.xview_moveto(0)
        canvas.yview_moveto(0)

        # create a frame inside the canvas which will be scrolled with it
        self.interior = interior = Frame(canvas)
        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)


main = Tk()
main.geometry('400x400')

#main
a = uFrame(main,bg="black")
#Top bar
b = Frame(main,bg="green")

b.pack(fill=X,side=TOP,ipady = 15)
a.pack(fill=BOTH,side=TOP,expand = YES)
# a.pack()
# a.update()

bLabel = Label(b,text="User",bg="red").pack(side=LEFT,anchor = S)
cLabel = Label(b,text="HELLO",bg="blue").pack(side=RIGHT,ipadx = 10,anchor = S)
cLabel = Label(b,text="HELLO",bg="white").pack(side=RIGHT, ipadx = 10,anchor = S)


frames = []

for i in range(10):
    frames.append(Frame(a.interior,height=50,bd = 5,bg="yellow",highlightbackground="red", highlightcolor="red", highlightthickness=1))
    frames[-1].pack(fill=X)
# for i in range(10):
#     Frame(a.canvas,height=50,bd = 5,bg="yellow",highlightbackground="red", highlightcolor="red", highlightthickness=1).pack(side=TOP,fill=X)


main.mainloop()   

我解决了这个问题。在这里帮助-> Tkinter Scrollbard for Frame

【讨论】:

    猜你喜欢
    • 2013-04-17
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 2020-08-04
    相关资源
    最近更新 更多