<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Michael Lange napsal(a):
<blockquote cite="mid20060503112912.62f323ca.klappnase@web.de"
type="cite">
<pre wrap="">
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 .
</pre>
</blockquote>
yes, really ugly :-)<br>
<blockquote cite="mid20060503112912.62f323ca.klappnase@web.de"
type="cite">
<pre wrap="">
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'))
</pre>
</blockquote>
this works with me even without new ok_new - with the old one:<br>
<pre wrap=""> w['menu'].insert('end', 'command', label='blah', command=lambda : ok('blah'))
</pre>
<br>
<blockquote cite="mid20060503112912.62f323ca.klappnase@web.de"
type="cite">
<pre wrap="">
Maybe it is even nicer not to use an OptionMenu command at all and use a trace callback for the variable instead:
</pre>
<blockquote type="cite">
<blockquote type="cite">
<blockquote type="cite">
<pre wrap="">v = StringVar()
v.set('a')
def ok(*args):
</pre>
</blockquote>
</blockquote>
</blockquote>
<pre wrap=""><!---->... print v.get()
...
</pre>
<blockquote type="cite">
<blockquote type="cite">
<blockquote type="cite">
<pre wrap="">v.trace('w', ok)
</pre>
</blockquote>
</blockquote>
</blockquote>
<pre wrap=""><!---->'-1216076380ok'
</pre>
<blockquote type="cite">
<blockquote type="cite">
<blockquote type="cite">
<pre wrap="">om = OptionMenu(r, v, 'a', 'b', 'c')
om.pack()
om['menu'].insert('end', 'command', label='foo', command=lambda : v.set('foo'))
</pre>
</blockquote>
</blockquote>
</blockquote>
<pre wrap=""><!---->
</pre>
</blockquote>
I completely forgot about this kind of solution....<br>
<blockquote cite="mid20060503112912.62f323ca.klappnase@web.de"
type="cite">
<pre wrap="">At least you do not need a second callback this way.
I hope this helps
</pre>
</blockquote>
Sure this helps. Thank you for all.<br>
<pre class="moz-signature" cols="100">--
Pavel Kosina
</pre>
</body>
</html>