[Tutor] tkinter, create widgets during runtime?

Karim karim.liateni at free.fr
Thu Jan 27 14:59:56 CET 2011


I never did that but this is python!
You can create anything you want at runtime!
Make a factory method in you gui class which inherit from Frame or 
whatever container code below is testing:

 >>> from Tkinter import *
 >>> class gui(Frame):
...      def __init__(self, master=None):
...          Frame.__init__(self, master)
...          self.pack()
...      def createLabels(self):
...          labels = []
...          for  i in range(4):
...               labelName = "myLabel_{0}".format(i)
...               labels.append(Label(master=self, text="this is label " 
+ labelName))
...          for label in labels:
...              label.pack()
...
 >>> a=gui()
 >>> a.createLabels()


self is the instance of your inherited Frame or else container.

Regards
Karim


On 01/27/2011 01:15 PM, Elwin Estle wrote:
> From the lack of replies...I am guessing that this can't be done.  Tho 
> I just realized I had a typo in part of it.
>
> The line that reads:
> "Is there a way to do something like this in Tkinter?  Or am I correct 
> in guessing that if it is not possible, it is probably more 
> complicated than the above?"
>
> ...should have said, "Or am I correct in guessing tat if it IS 
> possible, it is probably more..."
>
> --- On *Wed, 1/26/11, Elwin Estle /<chrysalis_reborn at yahoo.com>/* wrote:
>
>
>     From: Elwin Estle <chrysalis_reborn at yahoo.com>
>     Subject: [Tutor] tkinter, create widgets during runtime?
>     To: tutor at python.org
>     Date: Wednesday, January 26, 2011, 12:21 PM
>
>     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
>     }
>
>     ...which will generate five labels from "myLabel_0" to "myLabel_5".
>
>     Useful if you need a varying number of widgets, or a series of
>     "incremental" widgets.
>
>     Is there a way to do something like this in Tkinter?  Or am I
>     correct in guessing that if it is not possible, it is probably
>     more complicated than the above?
>
>
>
>
>
>     -----Inline Attachment Follows-----
>
>     _______________________________________________
>     Tutor maillist  - Tutor at python.org </mc/compose?to=Tutor at python.org>
>     To unsubscribe or change subscription options:
>     http://mail.python.org/mailman/listinfo/tutor
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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


More information about the Tutor mailing list