SHARE
TWEET

Tkiner - scroll Frame, not move image

furas Jul 20th, 2018 (edited) 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import tkinter as tk
  2. from PIL import Image, ImageTk
  3.  
  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.  
  53. # --- main ---
  54.  
  55. root = tk.Tk()              
  56.  
  57. # ---
  58.  
  59. canvas = tk.Canvas(root)
  60. canvas.grid(row=0, column=0, sticky='ns')
  61.  
  62. sf = ScrolledFrame(root, vertical=False) # turn off scrollbar inside ScrolledFrame
  63. window = canvas.create_window((100, 0), window=sf, anchor='nw', width=100)#, height=100)
  64.  
  65. # assign external scrollbar to ScrolledFrame
  66. vertical_bar = tk.Scrollbar(root, orient='vertical', command=sf._canvas.yview)
  67. vertical_bar.grid(row=0, column=1, sticky='ns')
  68. sf._canvas.configure(yscrollcommand=vertical_bar.set)
  69.  
  70. #img = Image.open('image.jpg')
  71. #image = ImageTk.PhotoImage(img)
  72. #canvas.create_image(0, 0, image=image, anchor='nw')
  73.  
  74. canvas.create_rectangle((0, 0, 300, 200), fill='red')
  75. canvas.create_rectangle((50, 100, 400, 400), fill='green')
  76.  
  77. # ---
  78.  
  79. # add some items to scrolled frame
  80.  
  81. data = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't']
  82. variables = []
  83.  
  84. # add widgets to scrolled frame - as parent you have to use `sf.inner` instead of `sf`
  85. for txt in data:
  86.     var = tk.IntVar()
  87.     variables.append(var)
  88.     l = tk.Checkbutton(sf.inner, text=txt, variable=var)
  89.     l.grid(sticky='w')
  90.  
  91. # ---
  92.  
  93. 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