[Tkinter-discuss] OptionMenu, change the list of choices

Vasilis Vlachoudis Vasilis.Vlachoudis at cern.ch
Tue Oct 24 17:30:28 CEST 2006


Thank you very much. It seems it works. How ever, if I don't use a 
command=lambda... in the add_command I cannot get the OptionMenu value 
changed (see below). Is it really necessary all the lambda... function I 
am doing or there is a smarter way of doing the job?

Regards
Vasillis

--------
from Tkinter import *
values = ["one","two","three"]
root = Tk()
var = StringVar()
var.set(values[0])
# set initial options
o = OptionMenu(root,var,*values)
o.pack()
# change options
m = o.children['menu']
m.delete(0,END)
newvalues = "a b c d e f".split()
for val in newvalues:
    m.add_command(label=val,command=lambda v=var,l=val:v.set(l))
var.set(newvalues[0])
root.mainloop()
---------

mkieverpy at tlink.de wrote:
> Vasilis Vlachoudis wrote:
>   
>> values=["One","Two","Three"]   # Initial list
>> o = OptionMenu(frame, *values)
>> #...
>> #then later in the code read new values from a file
>> newvalues = f.readline().split()
>> o.config(???=newvalues)
>>
>>     
> With
>   m = o.children ['menu']
> you can access the menu which implements the OptionMenu.
> OptionMenu uses 'command' entries.
> So you can add new entries with
>   m.add_command(label='a_new_entry',command=if_you_need_to_set_a_var_or_something)
> I never used this personally.
> Hope it works for you.
>
> Matthias Kievernagel
> mkiever at web dot de
>
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>   

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Vasilis.Vlachoudis.vcf
Type: text/x-vcard
Size: 312 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tkinter-discuss/attachments/20061024/a98344cf/attachment.vcf 


More information about the Tkinter-discuss mailing list