Advertisement
here2share

# Tk_autohide_scrollbars.py

Nov 21st, 2020
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. # Tk_autohide_scrollbars.py
  2.    
  3. # Importing tkinter
  4. from tkinter import *
  5.    
  6. # Creating class AutoScrollbar
  7. class AutoScrollbar(Scrollbar):
  8.        
  9.     # Defining set method with all  
  10.     # its parameter
  11.     def set(self, low, high):
  12.            
  13.         if float(low) <= 0.0 and float(high) >= 1.0:
  14.                
  15.             # Using grid_remove
  16.             self.tk.call("grid", "remove", self)
  17.         else:
  18.             self.grid()
  19.         Scrollbar.set(self, low, high)
  20.  
  21. # creating tkinter window  
  22. root = Tk()
  23.    
  24. # Defining vertical scrollbar
  25. verscrollbar = AutoScrollbar(root)
  26.    
  27. # Calling grid method with all its
  28. # parameter w.r.t vertical scrollbar
  29. verscrollbar.grid(row=0, column=1,  
  30.                   sticky=N+S)
  31.    
  32. # Defining horizontal scrollbar
  33. horiscrollbar = AutoScrollbar(root,  
  34.                               orient=HORIZONTAL)
  35.    
  36. # Calling grid method with all its  
  37. # parameter w.r.t horizontal scrollbar
  38. horiscrollbar.grid(row=1, column=0,  
  39.                    sticky=E+W)
  40.    
  41. # Creating scrolled canvas
  42. canvas = Canvas(root,
  43.                 yscrollcommand=verscrollbar.set,
  44.                 xscrollcommand=horiscrollbar.set)
  45.  
  46. canvas.grid(row=0, column=0, sticky=N+S+E+W)
  47.    
  48. verscrollbar.config(command=canvas.yview)
  49. horiscrollbar.config(command=canvas.xview)
  50.    
  51. # Making the canvas expandable
  52. root.grid_rowconfigure(0, weight=1)
  53. root.grid_columnconfigure(0, weight=1)
  54.    
  55. # creating canvas contents
  56. frame = Frame(canvas)
  57. frame.rowconfigure(1, weight=1)
  58. frame.columnconfigure(1, weight=1)
  59.  
  60. # creating label contents
  61. sss = "Drag The Canvas Frame To Resize", "Until Scrollbars Disappear"
  62. for s in [0,1]:
  63.     label = Label(frame, padx=8, font='ariel 17', text=sss[s])
  64.     label.grid(row=s, column=1, columnspan=16, sticky='w')
  65.  
  66. # Defining number of rows and columns
  67. for i in range(1,10):
  68.     for j in range(1,13):
  69.         button = Button(frame, padx=8, pady=8,  
  70.                         text="[%d,%d]" % (i,j))
  71.         button.grid(row=i+1, column=j, sticky='news')
  72.  
  73. # Creating canvas window
  74. canvas.create_window(0, 0, anchor=NW, window=frame)
  75.    
  76. # Calling update_idletasks method
  77. frame.update_idletasks()
  78.    
  79. # Configuring canvas
  80. canvas.config(scrollregion=canvas.bbox("all"))
  81.    
  82. # Calling mainloop method
  83. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement