Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_scrollbars.py
- from Tkinter import *
- root = Tk()
- #Add a canvas to the window
- canvas = Canvas(root,width=300, height=300)
- canvas.grid(column=0, row=0, sticky=N+S+E+W)
- #Allow the canvas (in row/column 0,0)
- #to "grow" to fill the entire window.
- root.grid_rowconfigure(0, weight=1)
- root.grid_columnconfigure(0, weight=1)
- #Add a scrollbar that will scroll the canvas vertically
- vscrollbar = Scrollbar(root)
- vscrollbar.grid(column=1, row=0, sticky=N+S)
- #Link the scrollbar to the canvas
- canvas.config(yscrollcommand=vscrollbar.set)
- vscrollbar.config(command=canvas.yview)
- #Add a scrollbar that will scroll the canvas horizontally
- hscrollbar = Scrollbar(root, orient=HORIZONTAL)
- hscrollbar.grid(column=0, row=1, sticky=E+W)
- canvas.config(xscrollcommand=hscrollbar.set)
- hscrollbar.config(command=canvas.xview)
- #This frame must be defined as a child of the canvas,
- #even though we later add it as a window to the canvas
- f = Frame(canvas)
- #
- #f.rowconfigure(1, weight=1)
- #f.columnconfigure(1, weight=1)
- #Create a button in the frame.
- b = Button(f,text="Hello Python World!")
- b.grid(row=0, column=0)
- #Add a large grid of sample label widgets to fill the space
- c = 1
- for x in range(1,33):
- for y in range(1,43):
- if x > 30 or y > 40:
- s = '...'
- else:
- s = str(c)
- c += 1
- Label(f, text=s).grid(row=1+x, column=y)
- #Add the frame to the canvas
- canvas.create_window((0,0), anchor=NW, window=f)
- #IMPORTANT:
- #Unless you want to bind an event handler to be called
- #when the frame's geometry manager finishes calculating
- #the frame size you need to call f.update_idletasks() so
- #that the call to f.bbox() on the following line will
- #return the correct bounding box size
- f.update_idletasks()
- #Tell the canvas how big of a region it should scroll
- #The size of the frame.
- canvas.config(scrollregion= f.bbox("all") )
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement