[Tutor] tkinter, create widgets during runtime?

Elwin Estle chrysalis_reborn at yahoo.com
Thu Jan 27 23:39:40 CET 2011


Thanks!  That was just what I was looking for.

--- On Thu, 1/27/11, Wayne Werner <waynejwerner at gmail.com> wrote:

From: Wayne Werner <waynejwerner at gmail.com>
Subject: Re: [Tutor] tkinter, create widgets during runtime?
To: "Elwin Estle" <chrysalis_reborn at yahoo.com>
Cc: tutor at python.org
Date: Thursday, January 27, 2011, 10:54 AM


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/d7927e32/attachment.html>


More information about the Tutor mailing list