Python Tkinter滚动条不起作用

时间:2016-05-09 06:12:15

标签: python tkinter scrollbar

我一直在尝试将滚动条放在Canvas内,其中LabelFrame只有一些Button,但我似乎无法使滚动条工作。< / p>

是否有可能不使用课程,因为我还是编程新手。

这里是代码:

def all_alb():
    global allAlbum
    global all_albwin
    global lg

    all_albwin = Toplevel()
    all_albwin.state('zoomed')
    ctr = 0
    alb_row = 0
    alb_cover = PhotoImage(file="img/placeholder.gif")

    scrollbar = Scrollbar(all_albwin)
    scrollbar.pack(side=RIGHT, fill=Y)

    container = Canvas(all_albwin, yscrollcommand=scrollbar.set, scrollregion=(0,0,1000,1000))

    # I do nothing scrollbar-related here

    if lg == 1:
        btnAdd = Button(container, text="Add New Album", command=add_alb)
        btnAdd.grid(row=0)

    btnMain = Button(container, text="Back to Main Menu", command=all_albwin.destroy)
    btnMain.grid(row=1)

    all_frame = LabelFrame(container, text="All Album", padx=10, pady=10)
    all_frame.grid(padx=15, pady=15, row=2)

    file = open("album/allAlbum.txt", "r")
    total = file.readline()
    file.close()

    if int(total) == 0:
        status = Label(all_frame, text="No album found.")
        status.grid(row=0)

    else:
        while ctr < int(total):
            file = open("album/alb_info/" + allAlbum[ctr].replace("\n", "") + ".txt", "r")
            coverDir = file.readline().replace("\n", "")
            coverImg = PhotoImage(file=coverDir)

            cover = Button(all_frame, image=coverImg, bd=0, command=lambda ctr=ctr: alb_info(ctr, "all_alb"))
            cover.image = coverImg
            cover.grid(row=alb_row, column=0, pady=20)

            alb_name = Button(all_frame, text=allAlbum[ctr].replace("\n", ""), font = "Helvetica 16 bold", command=lambda ctr=ctr: alb_info(ctr, "all_alb"), bd=0)
            alb_name.grid(row=alb_row, column=1, sticky=NW, padx=20, pady=20)

            ctr = ctr + 1
            alb_row = alb_row + 1

    # Scrollbar stuff under here
    container.pack(expand=True)
    scrollbar.config(command=container.yview)

1 个答案:

答案 0 :(得分:0)

您无法使用grid滚动添加到画布的对象。您必须使用create_window将labelframe放置在画布中。