AceScottie

rtk.py

Jan 10th, 2022 (edited)
1,461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. from tkinter import *
  2. def pack_opts(**kwargs):
  3.     pak = ["side", "expand", "fill"]
  4.     kw_wid = {}
  5.     kw_pak = {}
  6.     for k, v in kwargs.items():
  7.         if k in pak:
  8.             kw_pak[k] = v
  9.         else:
  10.             kw_wid[k] = v
  11.     return kw_wid, kw_pak
  12. class cFrame(Frame):
  13.     def __init__(self, master,  **kwargs):
  14.         kw_wid, kw_pak = pack_opts(**kwargs)
  15.         Frame.__init__(*(self, master), kw_wid)
  16.         if len(kw_pak) != 0:
  17.             self.pack(kw_pak)
  18. class cCanvas(Canvas):
  19.     def __init__(self, master,  **kwargs):
  20.         kw_wid, kw_pak = pack_opts(**kwargs)
  21.         Canvas.__init__(*(self, master), kw_wid)
  22.         if len(kw_pak) != 0:
  23.             self.pack(kw_pak)
  24.  
  25. class scrollable_area(cFrame):
  26.     def __init__(self, master, **kwargs):
  27.         kw_wid, kw_pak = pack_opts(**kwargs)
  28.         h = 0
  29.         v = 0
  30.         if "h" in kw_wid.keys():
  31.             h = kw_wid['h']
  32.             del kw_wid['h']
  33.         if "v" in kw_wid.keys():
  34.             v = kw_wid['v']
  35.             del kw_wid['v']
  36.         cFrame.__init__(*(self, master), **kw_wid)
  37.         self.Canvas = cCanvas(self, side=LEFT, fill=BOTH, expand=1)
  38.         self.sFrame = cFrame(self.Canvas)
  39.         self.cw = self.Canvas.create_window((0, 0), window=self.sFrame, anchor=NW)
  40.         self.scroll_v = Scrollbar(master, orient=VERTICAL, command=self.Canvas.yview)
  41.         self.scroll_v.pack(side=RIGHT, fill=Y)
  42.         self.Canvas.configure(yscrollcommand=self.scroll_v.set)
  43.         self.sFrame.bind("<Configure>", self._update_scrollregion)
  44.         self.Canvas.bind('<Configure>', self._FrameWidth)
  45.         if len(kw_pak) != 0:
  46.             self.pack(kw_pak)
  47.  
  48.     def _FrameWidth(self, event):
  49.         self.Canvas.itemconfig(self.cw, width=event.width - 4, height=event.height - 4)
  50.  
  51.     def _update_scrollregion(self, event):
  52.         self.Canvas.configure(scrollregion=self.Canvas.bbox("all"))
  53.  
  54. def create_scoll(tab):
  55.     s = cFrame(tab, side=LEFT, fill=BOTH, expand=1)
  56.     c = Canvas(s)
  57.     c.pack(side=LEFT, fill=BOTH, expand=1)
  58.     sf = cFrame(c, side=TOP)
  59.     cf = c.create_window((0, 0), window=sf, anchor=NW)
  60.     sb = Scrollbar(s, orient="vertical", command=c.yview)
  61.     sb.pack(side=RIGHT, fill=Y)
  62.     c.config(yscrollcommand=sb.set)
  63.     sf.bind("<Configure>", lambda e=Event(), c=c: update_scrollregion(e, c))
  64.     c.bind('<Configure>', lambda e=Event(), c=c, cf=cf: FrameWidth(e, c, cf))
  65.     return sf, c, s
  66.  
  67. def update_scrollregion(event, c):
  68.     c.configure(scrollregion=c.bbox("all"))
  69.     return "break"
  70. def FrameWidth(event, c, cf):
  71.     canvas_width = event.width
  72.     c.itemconfig(cf, width=canvas_width - 20)
  73.  
  74.  
  75.  
  76. root = Tk()
  77. a = Frame(root)
  78. a.pack()
  79. #f, c, s = create_scoll(a)
  80. sa1 = scrollable_area(a, side=TOP, fill=BOTH, expand=1, h=0, v=1)
  81. for i in range(100):
  82.     Label(sa1.sFrame, text="Hello%s World %s: Hello World"%(i, i)).pack(side=TOP)
  83.     #Label(f, text="Hello%s World %s: Hello World"%(i, i)).pack(side=TOP)
  84. root.mainloop()
Add Comment
Please, Sign In to add comment