[Tutor] Recursive Tkinter buttons

jfouhy at paradise.net.nz jfouhy at paradise.net.nz
Fri Feb 25 00:07:42 CET 2005

Quoting Danny Yoo <dyoo at hkn.eecs.berkeley.edu>:

> I think that only widgets that are designated as "containers" can
> contain other widgets. A Frame is an example of a widget that can contain 
> other widgets:

This is (I think) true; but the root can also contain widgets, and it is the
default if no other master is specified.

For example:

>>> from Tkinter import *
>>> def foo():
...  print 'foo'
>>> Button(text='foo', command=foo).pack()

This will produce a window with a button which, when clicked, will print 'foo'
to stdout.

To be honest, I'm a bit stumped by Adam's problem.  Here is some code I just
wrote which works perfectly (it produces a window with a column of buttons
labeled 'field 0' through 'field 9'; each button (when clicked) prints out its

>>> from Tkinter import *
>>> def p(x):
...  print x
>>> for i in xrange(10):
...  buttonName = 'field ' + str(i)
...  b = Button(text=buttonName, command=lambda i=i: p(i))
...  b.grid(row=i)


More information about the Tutor mailing list