#Jay Summet - CS 2316 - Spring 2014 # #Demo that creates a "scrollable frame". # Left as an exercise to the student: # Add a scrollbar (orient=HORIZONTAL) at the bottom # to scroll left and right.... from tkinter import * root = Tk() root.title("Scroll Frame") scrollbarY = Scrollbar(root, orient=VERTICAL) scrollbarY.pack(side=RIGHT, fill=Y) canvas = Canvas(root,width=200, height=300) #Be sure to allow the canvas to expand # if the user makes the window larger! canvas.pack(side=LEFT, expand=True, fill=BOTH) #The inner frame MUST be created as a child of the #canvas, even though we add it as a window later. #otherwise, it may draw over other elements of the main window! f = Frame(canvas) #add some widgets to the frame Label(f,text="A Widget").grid(row=0, column=0) width = 10 height = 20 #Dynamicly allocated buttons! #Use a lambda function that takes x*y as an argument to do #something different for each button... (q=x*y gets around # the natural closure behavior of a lambda function...) for x in range(1,width): for y in range(1,height): tmp = Button(f, text=str(x*y), command = lambda q=x*y: print(q) ) tmp.grid(row=y, column=x) #Add the frame as a "window" onto the canvas. #Anchor the top left corner of the frame at 0,0 canvas.create_window((0,0), anchor=NW, window=f) #Link the canvas to the Y-scrollbar canvas.config(yscrollcommand=scrollbarY.set) #Link the Y-scrollbar to the canvas scrollbarY.config(command=canvas.yview) #Update the frame so that it's bounding box is correct! f.update() #Tell the canvas how big the frame is that needs to scroll! canvas.config(scrollregion= f.bbox("all") ) #The mainloop waits for and handles user input! mainloop()