Advertisement
here2share

# Tk_scrollbars.py

Feb 4th, 2021
1,040
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. # Tk_scrollbars.py
  2.  
  3. from Tkinter import *
  4. root = Tk()
  5.  
  6. #Add a canvas to the window
  7. canvas = Canvas(root,width=300, height=300)
  8. canvas.grid(column=0, row=0, sticky=N+S+E+W)
  9.  
  10. #Allow the canvas (in row/column 0,0)
  11. #to "grow" to fill the entire window.
  12. root.grid_rowconfigure(0, weight=1)
  13. root.grid_columnconfigure(0, weight=1)
  14.  
  15.  
  16. #Add a scrollbar that will scroll the canvas vertically
  17. vscrollbar = Scrollbar(root)
  18. vscrollbar.grid(column=1, row=0, sticky=N+S)
  19. #Link the scrollbar to the canvas
  20. canvas.config(yscrollcommand=vscrollbar.set)
  21. vscrollbar.config(command=canvas.yview)
  22.  
  23.  
  24. #Add a scrollbar that will scroll the canvas horizontally
  25. hscrollbar = Scrollbar(root, orient=HORIZONTAL)
  26. hscrollbar.grid(column=0, row=1, sticky=E+W)
  27. canvas.config(xscrollcommand=hscrollbar.set)
  28. hscrollbar.config(command=canvas.xview)
  29.  
  30.  
  31.  
  32. #This frame must be defined as a child of the canvas,
  33. #even though we later add it as a window to the canvas
  34.  
  35. f = Frame(canvas)  
  36.                    
  37. #
  38. #f.rowconfigure(1, weight=1)
  39. #f.columnconfigure(1, weight=1)
  40.  
  41. #Create a button in the frame.
  42. b = Button(f,text="Hello Python World!")
  43. b.grid(row=0, column=0)
  44.  
  45. #Add a large grid of sample label widgets to fill the space
  46. c = 1
  47. for x in range(1,33):
  48.     for y in range(1,43):
  49.         if x > 30 or y > 40:
  50.             s = '...'
  51.         else:
  52.             s = str(c)
  53.             c += 1
  54.         Label(f, text=s).grid(row=1+x, column=y)
  55.  
  56. #Add the frame to the canvas
  57. canvas.create_window((0,0), anchor=NW, window=f)
  58.  
  59. #IMPORTANT:
  60. #Unless you want to bind an event handler to be called
  61. #when the frame's geometry manager finishes calculating
  62. #the frame size you need to call f.update_idletasks() so
  63. #that the call to f.bbox() on the following line will
  64. #return the correct bounding box size
  65. f.update_idletasks()
  66.  
  67. #Tell the canvas how big of a region it should scroll
  68. #The size of the frame.
  69. canvas.config(scrollregion= f.bbox("all")  )
  70.  
  71. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement