Tkinter Scrollbar not scrolling

2019-08-18 07:28发布

问题:

I have some code which is creating a scrollable Tkinter canvas with buttons inside. I believe my configuration of the scrollbar and Canvas is ok, but when I run the program, the scrollbar does not scroll. I have looked at other solutions online and I believe I am fulfilling everything that should be applied.

Thank you for any and all help:)

from tkinter import *

root = Tk()


responsesFr = Frame(root, width=700, height=275, bg="#d40b04")
responseFrCoverCanvas = Canvas(root, width=700,height=14 ,highlightthickness=0, border=0,bg="#d40b04")
w = Canvas(responsesFr, width=650, height=225, borderwidth=0,highlightthickness=0,background="white")
w.config(scrollregion=[0,0,1000,225])

for column in range(30):
    button1 = Button(w, width=20, height=4)
    button1.pack(side='left', padx=25)
    if column == 29:
        w.config(scrollregion=[0,0,1000,1000])
        print("done")
    else:
        pass
w.pack(padx=25,pady=10)
hbar=Scrollbar(responsesFr, orient=HORIZONTAL)
hbar.pack(padx=25, side=TOP, fill=X)
hbar.config(command=w.xview)
w.config(xscrollcommand=hbar.set)

responsesFr.pack()


root.mainloop()

回答1:

You can't scroll things added with pack. You must use the canvas create_window method to add widgets to the canvas.