GUI Frameworks in Python?

Kenneth McDonald kmmcdonald at wisc.edu
Sun Apr 4 17:35:32 EDT 2004


In article <mailman.102.1080629227.20120.python-list at python.org>, Hugh Macdonald wrote:
> I've recently been trying out various different GUI frameworks in Python and
> was wondering if I could get your input on the pros and cons of the
> different ones...
>
I would take a strong, close look at Tkinter again. In my opinion,
people have dismissed Tk as 'old technology', but I have seen
nothing else that remotely approaches the power of Tk.

First, a few points:
	1) Tk at the moment looks ugly. There is a major
	effort undergoing (tile--look on sourceforge) to
	develop a themable Tk, and they already have
	some very good-looking Windows-looking screenshots.
	This won't be available in the immediate future,
	but I think it won't be too too far off either.
	The Tk community understands that the ugly,
	nonstandard look is hurting Tk use.

	2) Tkinter is a (relatively) thin layer over
	Tk calls. The best way to use it is to build
	some functions over it to do what you want
	to do, and then reuse those functions.

	3) Tkinter does make Tk into an object oriented
	paradigm. So as you mention, you can subclass
	a Tk widget and then pass in appropriate methods
	of that class as functions. However, you often
	don't even need to do this. A complex widget is
	almost certainly going to be declared as a class
	so just do:

		class MyWidget(Frame):
			def __init__(self)
				button1=Button(command=self.foo1)
				button2=Button(command=self.foo2)

			def foo1(event):...
			def foo2(event):...

	I can't remember if 'command' is the right option to Button,
	but you get the idea.

	3) People often complain about the Tk widget set not
	being powerful enough. This is nonsense. First, read
	through all of the man pages for the Text and Canvas
	widgets--nothing else even comes close. (If something
	does, please let me know). I've looked at QScintilla,
	and it is far less flexible than Text. Then, download
	the BLT addon package for Tcl/Tk and read through
	its graph widget documentation. As for basic widget
	sets, There may be a few widget types missing, but
	it is very few, and what is missing can usually
	be found in an Python extension (MegaWidgets) or
	somewhere else, or coded fairly quickly using Tkinter.

	4) The event-handling mechanism in Tk is awesome
	It is very easy to specify not just single events,
	but also "event sequences"; so, for example, you
	can specify that ^X^S (the emacs save command)
	saves your files.

	5) As I mentioned above, it is often best to put
	a nicer wrapper around some of the Tkinter stuff.
	For example, here is the way I put things into a
	gridded layout:

		button = Button(...)
		label = Label(...)
		subframe = Frame(...)
		mywidget = MyWidget(...)

		grid([
		[button,	'-',		label],
		[subframe,	'x',		'^'], 
		['^',		mywidget,	'-']
		])

	Which, using the captalized first letter of each
	widget's variable, result in a layout like this:

		BBL
		S L
		SMM
	with the middle cell empty. The ^ and - characters
	simply indicate that the cell in the previous
	row/column should be carred into this cell. This
	grid function (not method)
	took me about 8 lines to write, and I've
	put it up on the Tkinter mailing list hosted on
	the Python web site.

	Likewise, I'm in the process of writing something
	to make event binding easier, and will put that
	up when it's done. It will look something like:

	bindevents(widget,
		ca_x_space_b1=fun1,
		m_backspace=fun2,
		Y=fun3,
	)

	Which indicates that fun1 should be invoked on the
	event sequence ctrl-alt-x ctrl-alt-space ctrl-mousebutton1;
	fun2 on meta-backspace; and fun3 on the single key 'Y'.

	There are a few problems with Tk. To use much of the
	Tkinter stuff, you need to have the Tk man pages
	handy, which is why writing more pythonic wrappers
	is a good idea. There is a third-party table widget
	available which is useful for relatively simple
	applications, but which lacks the flexibility and
	power of the Text and Canvas widgets. (Still, it's
	better than a lot of other table implementations
	I've seen.) However, once you've gotten over the
	initial learning curve (which mostly has to do
	with learning the Tk side, and isn't really that
	bad--Brent Welch and a co-author have a brand new
	book out which is just great), you can throw a
	powerful UI together in an amzingly short time.

Cheers,
Ken



More information about the Python-list mailing list