Michael Lange napsal(a):
Oops, you are right. A look at Tkinter.OptionMenu shows that it is not that trivial.
Tkinter wraps the command in its internal _setit class, so it should actually be:
from Tkinter import _setit
w['menu'].insert('end', 'command', label='blah', command=_setit(var, 'blah', ok))
The arguments for the _setit constructor are: _setit(variable, value, callback) .
Not very nice, indeed. Maybe it would be a nice improvement to Tkinter to add at least
an insert() and a delete() method to Tkinter.OptionMenu .
yes, really ugly :-)
If you do not want to use the "hidden" _setit() you might define another callback for the inserted items:
def ok_new(value):
var.set(value)
ok(value)
w['menu'].insert('end', 'command', label='blah', command=lambda : ok_new('blah'))
this works with me even without new ok_new - with the old one:
w['menu'].insert('end', 'command', label='blah', command=lambda : ok('blah'))
Maybe it is even nicer not to use an OptionMenu command at all and use a trace callback for the variable instead:
v = StringVar()
v.set('a')
def ok(*args):
... print v.get()
...
v.trace('w', ok)
'-1216076380ok'
om = OptionMenu(r, v, 'a', 'b', 'c')
om.pack()
om['menu'].insert('end', 'command', label='foo', command=lambda : v.set('foo'))
I completely forgot about this kind of solution....
At least you do not need a second callback this way.
I hope this helps
Sure this helps. Thank you for all.
--
Pavel Kosina
|
_______________________________________________
Tkinter-discuss mailing list
[email protected]
http://mail.python.org/mailman/listinfo/tkinter-discuss