List Info

Thread: OptionMenu, change the list of choices




OptionMenu, change the list of choices
user name
2006-10-24 13:51:37
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-discusspython.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
OptionMenu, change the list of choices
user name
2006-10-24 15:30:28
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()
---------

mkieverpytlink.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-discusspython.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>   

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discusspython.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
OptionMenu, change the list of choices
user name
2006-10-24 15:54:13
On Tue, Oct 24, 2006 at 05:30:28PM +0200, Vasilis Vlachoudis
wrote:
			.
			.
			.
> changed (see below). Is it really necessary all the
lambda... function I 
> am doing or there is a smarter way of doing the job?
			.
			.
			.
In general, any time you code with a lambda (an anonymous
function), you might equally well choose to define a named
function, and use that instead.  The choice between them
is a matter of style, for comparable functionality is avail-
able either way.

Modern Python style generally *deprecates* lambdas in favor
of named functions.  Tkinter might be one of the domains
where lambdas are most common.
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discusspython.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )