1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| import Tkinter
class Tkscrolledframe(Tkinter.Frame):
def __init__(self, container=None, **kwds):
Tkinter.Frame.__init__(self, container)
self.no_hbar = False
if 'no_hbar' in kwds.keys():
self.no_hbar = kwds['no_hbar']
kwds.pop('no_hbar')
self.no_vbar = False
if 'no_vbar' in kwds.keys():
self.no_vbar = kwds['no_vbar']
kwds.pop('no_vbar')
self.canvas = Tkinter.Canvas(self, **kwds)
self.canvas.grid(row=0, column=0, sticky='nswe')
if not self.no_hbar:
self.hscroll = Tkinter.Scrollbar(self, orient=Tkinter.HORIZONTAL, command=self.canvas.xview)
self.hscroll.grid(row=1, column=0, sticky='we')
self.canvas.configure(xscrollcommand=self.hscroll.set)
if not self.no_vbar:
self.vscroll = Tkinter.Scrollbar(self, orient=Tkinter.VERTICAL, command=self.canvas.yview)
self.vscroll.grid(row=0, column=1, sticky='ns')
self.canvas.configure(yscrollcommand=self.vscroll.set)
self.frame = Tkinter.Frame(self.canvas)
def getFrame(self):
"""returns the frame where to place the graphic objects"""
return self.frame
def update(self):
self.frame.update()
self.canvas.create_window(0, 0, window=self.frame, anchor=Tkinter.NW)
self.canvas.configure(scrollregion=self.canvas.bbox(Tkinter.ALL))
objects = [
[Tkinter.Entry, {'text':"This is an Entry"}],
[Tkinter.LabelFrame, {'text':"This is Label"}],
[Tkinter.Button, {'text':"This is a Button"}],
[Tkinter.Entry, {'text':"This is an Entry"}],
[Tkinter.LabelFrame, {'text':"This is Label"}],
[Tkinter.Button, {'text':"This is a Button"}],
[Tkinter.Entry, {'text':"This is an Entry"}],
[Tkinter.LabelFrame, {'text':"This is Label"}],
[Tkinter.Button, {'text':"This is a Button"}],
[Tkinter.Entry, {'text':"This is an Entry"}],
[Tkinter.LabelFrame, {'text':"This is Label"}],
[Tkinter.Button, {'text':"This is a Button"}],
]
if __name__=="__main__":
# main window creation
root = Tkinter.Tk()
# frame with scrollbars creation
frame = Tkscrolledframe(root, width=200, height=50)
frame.grid()
# gets and fill the container
container = frame.getFrame()
container.grid()
for index, (object, params) in enumerate(objects):
obj = object(container, **params)
obj.grid(column=index%4, row=index/4)
#computes the new dimensions of the frame.
frame.update()
#endless loop
root.mainloop() |