How to apply a scrollbar to a frame in a canvas
I'm having a really hard time trying to achieve a decdent GUI layout in tkinter. I'm aiming for the layout on the left, but didn't get any further than the layout on the right:
I feel completely lost in all that different layout managers. This is my code so far:
from Tkinter import *
class ScrolledCanvas:
def __init__(self, master, width=500, height=350):
Label(master, text="top of master frame").pack(side=TOP)
self.control=Frame(master)
self.control.pack(side=BOTTOM, fill=X, padx=2)
Label(self.control, text="bottom of master frame").pack()
self.grid = Frame(master, bd=2, bg="red")
self.canvas = Canvas(master, relief=SUNKEN, borderwidth=2,
scrollregion=('-11c', '-11c', '50c', '20c'))
self.vscroll = Scrollbar(master, command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.vscroll.set)
self.grid.pack(expand=YES, fill=BOTH, padx=1, pady=1)
开发者_Python百科 self.grid.rowconfigure(0, weight=1, minsize=0)
self.grid.columnconfigure(0, weight=1, minsize=0)
self.canvas.grid(padx=1, in_=self.grid, pady=1, row=0,
column=0, rowspan=1, columnspan=1, sticky='news')
self.vscroll.grid(padx=1, in_=self.grid, pady=1, row=0,
column=1, rowspan=1, columnspan=1, sticky='news')
self.oldFill = None
self.canvas = self.fillCanvas(self.canvas)
def fillCanvas(self, theCanvas):
for i in range(10):
frame = Frame(theCanvas)
Label(frame, text="text for inner frame (%s)" % i).pack(side=TOP)
Button(frame, text="button %s.1" % i).pack(side=LEFT)
Button(frame, text="button %s.2" % i).pack(side=LEFT)
frame.pack()
return theCanvas
if __name__ == '__main__':
root = Tk()
scroll = ScrolledCanvas(root)
root.mainloop()
For some reason the scrollregion expands to full size, making all scrollbar actions obsolete (This code bases on a scrollbar example where the canvas is filled with some rectangles, and everything scrolls really nice). I probably missed something fundamental about resizing, because most components seem to completely ignore their fixed width and height.
I'm looking for help on how to get this example to work with tkinter. I know that there are other layout technologies, and I know that other modules offer scrollable widgets out of the box. But unless completely unavoidable I really like to stick to this example, which cost me at least 3 nights so far...
You cannot pack (or grid, or place) widgets into the canvas and expect them to scroll. Instead, you must use the create_window method.
精彩评论