SHARE
TWEET

Tkinter - ScrolledFrame inside ScrolledFrame

furas Jul 21st, 2018 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # ScrolledFame
  3. # https://github.com/furas/python-examples/tree/master/tkinter/scrolled-frame-canvas
  4. #
  5.  
  6. import tkinter as tk
  7. from PIL import Image, ImageTk
  8.  
  9.  
  10. class ScrolledFrame(tk.Frame):
  11.  
  12.     def __init__(self, parent, vertical=True, horizontal=False):
  13.         super().__init__(parent)
  14.  
  15.         # canvas for inner frame
  16.         self._canvas = tk.Canvas(self)
  17.         self._canvas.grid(row=0, column=0, sticky='news') # changed
  18.  
  19.         # create right scrollbar and connect to canvas Y
  20.         self._vertical_bar = tk.Scrollbar(self, orient='vertical', command=self._canvas.yview)
  21.         if vertical:
  22.             self._vertical_bar.grid(row=0, column=1, sticky='ns')
  23.         self._canvas.configure(yscrollcommand=self._vertical_bar.set)
  24.  
  25.         # create bottom scrollbar and connect to canvas X
  26.         self._horizontal_bar = tk.Scrollbar(self, orient='horizontal', command=self._canvas.xview)
  27.         if horizontal:
  28.             self._horizontal_bar.grid(row=1, column=0, sticky='we')
  29.         self._canvas.configure(xscrollcommand=self._horizontal_bar.set)
  30.  
  31.         # inner frame for widgets
  32.         self.inner = tk.Frame(self._canvas)
  33.         self._window = self._canvas.create_window((0, 0), window=self.inner, anchor='nw')
  34.  
  35.         # autoresize inner frame
  36.         self.columnconfigure(0, weight=1) # changed
  37.         self.rowconfigure(0, weight=1) # changed
  38.  
  39.         # resize when configure changed
  40.         self.inner.bind('<Configure>', self.resize)
  41.        
  42.         # resize inner frame to canvas size
  43.         self.resize_width = False
  44.         self.resize_height = False
  45.         self._canvas.bind('<Configure>', self.inner_resize)
  46.  
  47.     def resize(self, event=None):
  48.         self._canvas.configure(scrollregion=self._canvas.bbox('all'))
  49.  
  50.     def inner_resize(self, event):
  51.         # resize inner frame to canvas size
  52.         if self.resize_width:
  53.             self._canvas.itemconfig(self._window, width=event.width)
  54.         if self.resize_height:
  55.             self._canvas.itemconfig(self._window, height=event.height)
  56.  
  57.  
  58. # --- main ---
  59.  
  60. root = tk.Tk()              
  61.  
  62. # ---
  63.  
  64. canvas = tk.Canvas(root)
  65. canvas.grid(row=0, column=0, sticky='ns')
  66.  
  67. sf = ScrolledFrame(root, vertical=False) # turn off scrollbar inside ScrolledFrame
  68. window = canvas.create_window((100, 0), window=sf, anchor='nw', width=150)#, height=100)
  69.  
  70. #sf.resize_height = True  # <-- added
  71. sf.resize_width = True  # <-- added
  72.  
  73. # assign external scrollbar to ScrolledFrame
  74. vertical_bar = tk.Scrollbar(root, orient='vertical', command=sf._canvas.yview)
  75. vertical_bar.grid(row=0, column=1, sticky='ns')
  76. sf._canvas.configure(yscrollcommand=vertical_bar.set)
  77.  
  78. sf2 = ScrolledFrame(sf.inner)  # <-- added
  79. sf2.pack()  # <-- added
  80.  
  81. sf3 = ScrolledFrame(sf.inner)  # <-- added
  82. sf3.pack()  # <-- added
  83.  
  84. canvas.create_rectangle((0, 0, 300, 200), fill='red')
  85. canvas.create_rectangle((50, 100, 400, 400), fill='green')
  86.  
  87. # ---
  88.  
  89. # add some items to scrolled frame
  90.  
  91. data = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't']
  92. variables = []
  93.  
  94. # add widgets to scrolled frame - as parent you have to use `sf.inner` instead of `sf`
  95. for txt in data:
  96.     var = tk.IntVar()
  97.     variables.append(var)
  98.     l = tk.Checkbutton(sf2.inner, text=txt, variable=var)  # <-- added sf2 instead of sf
  99.     l.grid(sticky='w')
  100.  
  101. # ---
  102.  
  103. 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
 
Top