SHARE
TWEET

Python - scrolledframe

furas Jun 10th, 2018 (edited) 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import tkinter as tk
  2.  
  3. # --- classes ---
  4.  
  5. class ScrolledFrame(tk.Frame):
  6.  
  7.     def __init__(self, parent, vertical=True, horizontal=False):
  8.         super().__init__(parent)
  9.  
  10.         # canvas for inner frame
  11.         self._canvas = tk.Canvas(self)
  12.         self._canvas.grid(row=0, column=0, sticky='news') # changed
  13.  
  14.         # create right scrollbar and connect to canvas Y
  15.         self._vertical_bar = tk.Scrollbar(self, orient='vertical', command=self._canvas.yview)
  16.         if vertical:
  17.             self._vertical_bar.grid(row=0, column=1, sticky='ns')
  18.         self._canvas.configure(yscrollcommand=self._vertical_bar.set)
  19.  
  20.         # create bottom scrollbar and connect to canvas X
  21.         self._horizontal_bar = tk.Scrollbar(self, orient='horizontal', command=self._canvas.xview)
  22.         if horizontal:
  23.             self._horizontal_bar.grid(row=1, column=0, sticky='we')
  24.         self._canvas.configure(xscrollcommand=self._horizontal_bar.set)
  25.  
  26.         # inner frame for widgets
  27.         self.inner = tk.Frame(self._canvas)
  28.         self._window = self._canvas.create_window((0, 0), window=self.inner, anchor='nw')
  29.  
  30.         # autoresize inner frame
  31.         self.columnconfigure(0, weight=1) # changed
  32.         self.rowconfigure(0, weight=1) # changed
  33.  
  34.         # resize when configure changed
  35.         self.inner.bind('<Configure>', self.resize)
  36.        
  37.         # resize inner frame to canvas size
  38.         self.resize_width = False
  39.         self.resize_height = False
  40.         self._canvas.bind('<Configure>', self.inner_resize)
  41.  
  42.     def resize(self, event=None):
  43.         self._canvas.configure(scrollregion=self._canvas.bbox('all'))
  44.  
  45.     def inner_resize(self, event):
  46.         # resize inner frame to canvas size
  47.         if self.resize_width:
  48.             self._canvas.itemconfig(self._window, width=event.width)
  49.         if self.resize_height:
  50.             self._canvas.itemconfig(self._window, height=event.height)
  51.  
  52. # --- functions ---
  53.  
  54. def add_label():
  55.     global m
  56.  
  57.     m += 2
  58.  
  59.     e = tk.Label(sf.inner, text=m)
  60.     e.pack()
  61.    
  62. # --- start ---
  63.  
  64. m = 10
  65.  
  66. root = tk.Tk()
  67.  
  68. sf = ScrolledFrame(root)
  69. sf.grid(row=0, column=0)
  70.  
  71. b = tk.Button(root, text="add", command=add_label)
  72. b.grid(row=0, column=2, sticky="N")
  73.  
  74. root.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top