[Tutor] tkinter, create widgets during runtime?

Wayne Werner waynejwerner at gmail.com
Thu Jan 27 16:54:33 CET 2011


On Wed, Jan 26, 2011 at 11:21 AM, Elwin Estle <chrysalis_reborn at yahoo.com>wrote:

> With Tcl/Tk, you can generate widgets "on the fly" during program
> execution, without having to explicitly create them in your code.  i.e.,
> something like:
>
> for {set i 0} {$i <= 5} {incr i} {
>     label .myLabel_$i -text "this is label myLabel_$i"
>     pack .myLabel_$i

}
>

for x in xrange(5):
    Label(root, text="This is label %d" %x).pack()

will generate the labels. You can't put the variable names in the local
namespace without some tricksy methods. You could, however, have a dict
labels and just do

labels[x] = Label(root, text="blah")
labels[x].pack()

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110127/fda1953e/attachment.html>


More information about the Tutor mailing list