Hey everyone!

Been messing around with some Python Tkinter Canvas and I must say, it is very interesting.

Currently, I am working on creating a CanvasList class that like the list box, will allow you to create lists of instead of text, other widgets as well. As the name implies, it will use the Canvas widget to do so.

I will keep everyone posted on my progress.

Here is the code:

from Tkinter import *

class CanvasList(Canvas):
	def __init__(self, master=None, width=100, height=500, items=None, itemheight=None, **canvasOptions):
		Canvas.__init__(self, master, width=width, height=height, **canvasOptions)

		self.config(width=width, height=height)		# dimensions

		self.config(highlightthickness=0)				   # no pixels to border

		sbar = Scrollbar(self)
		sbar.config(command=self.yview)				   # xlink sbar and canv
		self.config(yscrollcommand=sbar.set)			   # move one moves other
		sbar.pack(side=RIGHT, fill=Y)					  # pack first=clip last
		self.pack(side=LEFT)		# canv clipped first

		self.width = width
		self.height = height
		self.itemheight = itemheight or (items and height/len(items))

		self.items = items or None
		self.itemIds = []

		self.y = 0
		self.add_items()

	def add_item(self, body, y):
		body['width'] = int(self.width); body['height'] = int(y)
		if body not in self.items: self.items.append(body)
		self.itemIds.append(self.create_window(int(0),int(y), window=body, width=self.width, height=self.itemheight))
		return len(self.itemIds)-1

	def add_items(self, items):
		if items or self.items:
			for item in items or self.items:
				self.add_item(item, -self.y)
				self.y -= self.itemheight

		self.config(scrollregion=(0,0,width, self.scrollheight(items or self.items)))

	def scrollheight(self, items=None):
		items = items or self.items
		if items:
			return len(items)*self.itemheight
			print 'scroll: ', len(items)*self.itemheight
		else:
			print items, 'items'
			return self.height

if __name__ == "__main__":
	root = Tk()
	root.title("Canvas List")

	def printitem(i): print str(i)

	items = []

	for i in range(20):
		frm = Frame()
		Label(frm, text="Lab"+str(i)).pack(side=LEFT, expand=YES, fill=BOTH)
		Button(frm, text=str(i), command=(lambda i=i: printitem(i))).pack(side=RIGHT, expand=YES, fill=BOTH)

		items.append(frm)

	can = CanvasList(root, width=200, height=500, items=items)
	print can.itemheight
	can.pack()
	root.mainloop()

-Sunjay Varma