[Tkinter-discuss] optionmenu insert

Pavel Kosina geon at post.cz
Wed May 3 13:37:31 CEST 2006


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tkinter-discuss/attachments/20060503/3558edfe/attachment.html 


More information about the Tkinter-discuss mailing list