[Tkinter-discuss] How to set optionmenu without setting the variable

david.giesen at kodak.com david.giesen at kodak.com
Wed Nov 4 14:23:39 CET 2009


I don't believe there is any way to create an OptionMenu without using a 
Tk variable.

However, if all you want to do is to change the value of an already 
created OptionMenu without using the variable, you can do this:

import Tkinter as Tk
root = Tk.Tk()
var = Tk.StringVar()
om = Tk.OptionMenu(root, var, 'a', 'b', 'c')
om['menu'].invoke(index)

where index is the index of the value you want to select.  In this case, 
index=0 for 'a', 1 for 'b', or 2 for 'c'.

I'm not sure that's really an improvement for you, but it works.

Also note - you can add additional options after creation to the 
OptionMenu through a bit of a backdoor using a private Tk function (so 
this isn't strictly recommended, I guess) using the following syntax:

om['menu'].add_command(label='new_value', command=Tk._setit(var, 
'new_value'))

Dave

> The conventional way goes this
> var = Stringvar()
> var.set('value') 
> But i want to happen without using the variable. Can someone help me out 
on
> this?
David J. Giesen | Research Scientist | FPEG US Display OLED Materials R+D 
| 
Eastman Kodak Company | 2/83/KRL MC02216 | Rochester, NY 14650 | 
david.giesen at kodak.com | 1-585-588-0480 Office | 
www.kodak.com 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20091104/ac8ac835/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/gif
Size: 826 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20091104/ac8ac835/attachment.gif>


More information about the Tkinter-discuss mailing list