[Tutor] Recursive Tkinter buttons

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Feb 24 23:50:11 CET 2005



On Thu, 24 Feb 2005, Adam Cripps wrote:

> I'm trying to create recursive Tkinter buttons with:
>
> 		for i in range(0,10):
> 			print i
> 			buttonlabel = "field " +str(i)
> 			button[i] = Button (text=buttonlabel)
> 			button[i].grid(column=3, row = i+3)


Hi Adam,

I'm not quite sure I understand what you mean by "recursive" buttons.
I'll assume, for the moment, that you mean composing widgets within
widgets.

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:

###
>>> from Tkinter import *
>>> root = Tk()
>>> frame = Frame(root)
>>> for i in range(5):
...     b = Button(frame, text="button " + str(i))
...     b.pack()
...
>>> frame.pack()
###

There's an object hierarchy here that we construct:

    root <----- frame <------ button 0
                              button 1
                              button 2
                              button 3
                              button 4

As far as I can tell, only Frames and Toplevels can contain other widgets
in Tkinter.  Extensions to Tkinter (like Python MegaWidgets) may have
extended the system with more container types.


Best of wishes to you!



More information about the Tutor mailing list