[Tkinter-discuss] creating widgets generically, with a loop?
mkieverpy at tlink.de
mkieverpy at tlink.de
Wed Jun 21 08:54:14 CEST 2006
>
> Some background info: I have a Python/Tkinter script that displays a
> dialog with buttons, where each button launches a file for editing.
> Sort of like an HTML file in a browser, except with my script I can
> launch it using a particular program.
>
> I've been trying to figure out a way of storing widget information
> (name of each widget, x and y positions, and a command to execute
> when activated) in an array so I can generically create each widget
> instead of hard-coding all the information. For example, here's a
> snippet of what I currently use:
>
> ...
> Button(top, text="OO design info",
> command=self.launch_file23).grid(sticky="w",row=10,column=5)
> ...
>
> ...
> def launch_file23 ( self ): os.spawnl ( os.P_NOWAIT ,
> "c:\\Program Files\\TextPad 4\\TextPad.exe" , "TextPad.exe" , "d:\\My
>
>
> Documents\\prog_oo_design.txt" )
> ...
>
> If there was a way I could pass arguments to launch_file23 from the
> "command=" parameter, that would solve the problem nicely - I could
> store the info in an array, and then just loop to create the buttons.
>
> Any ideas?
>
>
> Michael Steiner
> MikeJaySteiner at yahoo.com
> (480) 720-8902 (cell)
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
An idea using subclassing of 'Button':
--------------------------------------------------
from Tkinter import *
class ButtonParam ( Button ):
def __init__ ( self, param, parent, **kw ):
Button.__init__ ( self, parent, kw )
self.Param = param
def launch ( self ):
print 'My Param:', self.Param
if __name__ == '__main__':
tk = Tk ()
for bspec in [('text1','par1'),('text2','par2'),('text3','par3')]:
b = ButtonParam ( bspec [1], tk, text=bspec [0] )
b.configure ( command=b.launch )
b.pack ()
tk.mainloop ()
------------------------------------------------------------------
Perhaps the parameters to __init__ need additional tweaking
and you might want more than one parameter.
Hope this helps,
Matthias Kievernagel
Software-Development
mkiever/at/web/dot/de
More information about the Tkinter-discuss
mailing list