Tkinter widgets: Write options *and* option tags dynamically from dictionary?

Martin Franklin martin.franklin at westerngeco.com
Fri Feb 1 08:52:01 EST 2002


Johannes Eble wrote:
> 
> Hello all,
> 
> In Tkinter the widgets are created in the form:
> 
> wgt = Widget(master, option_tag1 = option1,  option_tag2 =
> option2,...)
> 
> I want to create widgets dynamically. Therefore I have created a
> dictionary for the options (well, it is a list of lists of lists which
> have this dictionary included, but I will keep it simple here). The
> problem is that not only the options are unknown at compile time, but
> also the *option tags*. For example in Button(root, text='Hello') also
> the *text* option tag should be taken from the keys of the options
> dictionary.
> 
> This is hardcoded:
> 
> from Tkinter import *
> root = Tk()
> bt = Button(root, text='Hello')
> bt.pack(side=TOP)
> root.mainloop()
> 
> When I change it to something like
> 
> ...
> dictOptions = {text: 'Hello',....}
> bt.config(?)
> ...
> 
> ,Python complains that there is no defined symbol text. Also, I
> wouldn't know what to write in bt.config(....)
> 
> On the other hand, I have tried to write
> 
> dictOptions={'text':'Hello'} and compose the string "text='Hello'" on
> the fly, but then I built a button with the text "text='Hello'".
> 
> So, how can I change the options and the option tags dynamically?
> 
> Any help would be appreciated.
> 
> ByeBye
> 
> Johannes

This works......


from Tkinter import *
d={'text' : 'hello'}
r=Tk()
b=Button(r, d)
b.pack()
d={'text' : 'world'}
b.config(d)
r.mainloop()



HTH 

Martin.



More information about the Python-list mailing list