Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_autohide_scrollbars.py
- # Importing tkinter
- from tkinter import *
- # Creating class AutoScrollbar
- class AutoScrollbar(Scrollbar):
- # Defining set method with all
- # its parameter
- def set(self, low, high):
- if float(low) <= 0.0 and float(high) >= 1.0:
- # Using grid_remove
- self.tk.call("grid", "remove", self)
- else:
- self.grid()
- Scrollbar.set(self, low, high)
- # creating tkinter window
- root = Tk()
- # Defining vertical scrollbar
- verscrollbar = AutoScrollbar(root)
- # Calling grid method with all its
- # parameter w.r.t vertical scrollbar
- verscrollbar.grid(row=0, column=1,
- sticky=N+S)
- # Defining horizontal scrollbar
- horiscrollbar = AutoScrollbar(root,
- orient=HORIZONTAL)
- # Calling grid method with all its
- # parameter w.r.t horizontal scrollbar
- horiscrollbar.grid(row=1, column=0,
- sticky=E+W)
- # Creating scrolled canvas
- canvas = Canvas(root,
- yscrollcommand=verscrollbar.set,
- xscrollcommand=horiscrollbar.set)
- canvas.grid(row=0, column=0, sticky=N+S+E+W)
- verscrollbar.config(command=canvas.yview)
- horiscrollbar.config(command=canvas.xview)
- # Making the canvas expandable
- root.grid_rowconfigure(0, weight=1)
- root.grid_columnconfigure(0, weight=1)
- # creating canvas contents
- frame = Frame(canvas)
- frame.rowconfigure(1, weight=1)
- frame.columnconfigure(1, weight=1)
- # creating label contents
- sss = "Drag The Canvas Frame To Resize", "Until Scrollbars Disappear"
- for s in [0,1]:
- label = Label(frame, padx=8, font='ariel 17', text=sss[s])
- label.grid(row=s, column=1, columnspan=16, sticky='w')
- # Defining number of rows and columns
- for i in range(1,10):
- for j in range(1,13):
- button = Button(frame, padx=8, pady=8,
- text="[%d,%d]" % (i,j))
- button.grid(row=i+1, column=j, sticky='news')
- # Creating canvas window
- canvas.create_window(0, 0, anchor=NW, window=frame)
- # Calling update_idletasks method
- frame.update_idletasks()
- # Configuring canvas
- canvas.config(scrollregion=canvas.bbox("all"))
- # Calling mainloop method
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement