Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- def pack_opts(**kwargs):
- pak = ["side", "expand", "fill"]
- kw_wid = {}
- kw_pak = {}
- for k, v in kwargs.items():
- if k in pak:
- kw_pak[k] = v
- else:
- kw_wid[k] = v
- return kw_wid, kw_pak
- class cFrame(Frame):
- def __init__(self, master, **kwargs):
- kw_wid, kw_pak = pack_opts(**kwargs)
- Frame.__init__(*(self, master), kw_wid)
- if len(kw_pak) != 0:
- self.pack(kw_pak)
- class cCanvas(Canvas):
- def __init__(self, master, **kwargs):
- kw_wid, kw_pak = pack_opts(**kwargs)
- Canvas.__init__(*(self, master), kw_wid)
- if len(kw_pak) != 0:
- self.pack(kw_pak)
- class scrollable_area(cFrame):
- def __init__(self, master, **kwargs):
- kw_wid, kw_pak = pack_opts(**kwargs)
- h = 0
- v = 0
- if "h" in kw_wid.keys():
- h = kw_wid['h']
- del kw_wid['h']
- if "v" in kw_wid.keys():
- v = kw_wid['v']
- del kw_wid['v']
- cFrame.__init__(*(self, master), **kw_wid)
- self.Canvas = cCanvas(self, side=LEFT, fill=BOTH, expand=1)
- self.sFrame = cFrame(self.Canvas)
- self.cw = self.Canvas.create_window((0, 0), window=self.sFrame, anchor=NW)
- self.scroll_v = Scrollbar(master, orient=VERTICAL, command=self.Canvas.yview)
- self.scroll_v.pack(side=RIGHT, fill=Y)
- self.Canvas.configure(yscrollcommand=self.scroll_v.set)
- self.sFrame.bind("<Configure>", self._update_scrollregion)
- self.Canvas.bind('<Configure>', self._FrameWidth)
- if len(kw_pak) != 0:
- self.pack(kw_pak)
- def _FrameWidth(self, event):
- self.Canvas.itemconfig(self.cw, width=event.width - 4, height=event.height - 4)
- def _update_scrollregion(self, event):
- self.Canvas.configure(scrollregion=self.Canvas.bbox("all"))
- def create_scoll(tab):
- s = cFrame(tab, side=LEFT, fill=BOTH, expand=1)
- c = Canvas(s)
- c.pack(side=LEFT, fill=BOTH, expand=1)
- sf = cFrame(c, side=TOP)
- cf = c.create_window((0, 0), window=sf, anchor=NW)
- sb = Scrollbar(s, orient="vertical", command=c.yview)
- sb.pack(side=RIGHT, fill=Y)
- c.config(yscrollcommand=sb.set)
- sf.bind("<Configure>", lambda e=Event(), c=c: update_scrollregion(e, c))
- c.bind('<Configure>', lambda e=Event(), c=c, cf=cf: FrameWidth(e, c, cf))
- return sf, c, s
- def update_scrollregion(event, c):
- c.configure(scrollregion=c.bbox("all"))
- return "break"
- def FrameWidth(event, c, cf):
- canvas_width = event.width
- c.itemconfig(cf, width=canvas_width - 20)
- root = Tk()
- a = Frame(root)
- a.pack()
- #f, c, s = create_scoll(a)
- sa1 = scrollable_area(a, side=TOP, fill=BOTH, expand=1, h=0, v=1)
- for i in range(100):
- Label(sa1.sFrame, text="Hello%s World %s: Hello World"%(i, i)).pack(side=TOP)
- #Label(f, text="Hello%s World %s: Hello World"%(i, i)).pack(side=TOP)
- root.mainloop()
Add Comment
Please, Sign In to add comment