Advertisement
Uno-Dan

Dynamic Canvas Scrolling

Dec 10th, 2019
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | None | 0 0
  1.  
  2. import tkinter as tk
  3. import tkinter.ttk as ttk
  4.  
  5.  
  6. lorem_ipsum = 'Lorem ipsum dolor sit amet, luctus non. Litora viverra ligula'
  7.  
  8.  
  9. class Scrollbar(ttk.Scrollbar):
  10.     def __init__(self, parent, canvas, **kwargs):
  11.         ttk.Scrollbar.__init__(self, parent, **kwargs)
  12.  
  13.         command = canvas.xview if kwargs.get('orient', tk.VERTICAL) == tk.HORIZONTAL else canvas.yview
  14.         self.configure(command=command)
  15.  
  16.     def set(self, low, high):
  17.         if float(low) > 0 or float(high) < 1:
  18.             self.grid()
  19.         else:
  20.             self.grid_remove()
  21.  
  22.         ttk.Scrollbar.set(self, low, high)
  23.  
  24.  
  25. class App(tk.Tk):
  26.     def __init__(self):
  27.         super().__init__()
  28.         self.rowconfigure(0, weight=1)
  29.         self.columnconfigure(0, weight=1)
  30.         self.title('Paned Window Demo')
  31.         self.geometry('420x200')
  32.  
  33.         style = ttk.Style()
  34.         style.theme_use('clam')
  35.         style.configure('TPanedwindow', background='black')
  36.  
  37.         pw = ttk.PanedWindow(self, orient=tk.HORIZONTAL)
  38.         left_frame = ttk.Frame(pw)
  39.         right_frame = ttk.Frame(pw)
  40.  
  41.         ttk.Label(left_frame, text='Left Pane').grid()
  42.         left_frame.rowconfigure(0, weight=1)
  43.         left_frame.columnconfigure(0, weight=1)
  44.         left_frame.grid(sticky=tk.NSEW)
  45.  
  46.         right_frame.rowconfigure(0, weight=1)
  47.         right_frame.columnconfigure(0, weight=1)
  48.         right_frame.grid(sticky=tk.NSEW)
  49.  
  50.         pw.add(left_frame)
  51.         pw.add(right_frame)
  52.         pw.grid(sticky=tk.NSEW)
  53.  
  54.         canvas = tk.Canvas(right_frame, bg=style.lookup('TFrame', 'background'))
  55.         canvas.frame = ttk.Frame(canvas)
  56.         canvas.rowconfigure(0, weight=1)
  57.         canvas.columnconfigure(0, weight=1)
  58.         canvas.grid(sticky=tk.NSEW)
  59.         canvas.frame.rowconfigure(990, weight=1)
  60.         canvas.frame.columnconfigure(0, weight=1)
  61.         canvas.frame.grid(sticky=tk.NSEW)
  62.  
  63.         content = ttk.Frame(canvas.frame)
  64.         content.rowconfigure(0, weight=1)
  65.         content.columnconfigure(0, weight=1)
  66.         content.grid(sticky=tk.NSEW)
  67.  
  68.         xscroll = Scrollbar(right_frame, canvas, orient=tk.HORIZONTAL)
  69.         yscroll = Scrollbar(right_frame, canvas, orient=tk.VERTICAL)
  70.         xscroll.grid(row=990, column=0, sticky=tk.EW)
  71.         yscroll.grid(row=0, column=990, sticky=tk.NS)
  72.  
  73.         for idx in range(1, 11):
  74.             tk.Label(content, bg='#aaaaaa', fg='#000000', text=f'{idx} {lorem_ipsum}').grid(sticky=tk.NW)
  75.  
  76.         ttk.Separator(content, orient=tk.HORIZONTAL).grid(pady=10, sticky=tk.EW)
  77.  
  78.         for idx in range(11, 21):
  79.             tk.Label(content, bg='#aaaaaa', fg='#000000', text=f'{idx} {lorem_ipsum}').grid(sticky=tk.NW)
  80.  
  81.         self.win = canvas.create_window((0, 0), window=canvas.frame, anchor=tk.NW)
  82.  
  83.         self.update_idletasks()
  84.         pw.sashpos(0, newpos=100)
  85.  
  86.         def update_canvas(e):
  87.             if e.width < content.bbox(tk.ALL)[2]:
  88.                 if not self.win:
  89.                     canvas.frame.grid()
  90.                     self.win = canvas.create_window((0, 0), window=canvas.frame, anchor=tk.NW)
  91.             else:
  92.                 canvas.delete(self.win)
  93.                 canvas.frame.grid()
  94.                 self.win = None
  95.         canvas.bind('<Configure>', update_canvas)
  96.  
  97.         canvas.configure(scrollregion=content.bbox(tk.ALL))
  98.         canvas.configure(xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
  99.  
  100.  
  101. def main():
  102.     app = App()
  103.     app.mainloop()
  104.  
  105.  
  106. if __name__ == '__main__':
  107.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement