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

Michael Lange klappnase at web.de
Wed Nov 4 20:30:38 CET 2009


On Wed, 4 Nov 2009 08:23:39 -0500
david.giesen at kodak.com wrote:

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

Actually you can:


from Tkinter import *

root = Tk()
om = OptionMenu(root, None, '1', '2')
om.pack(padx=100, pady=100)

items = ('a', 'b', 'c', 'd')
def callback(item):
    print 'selected:', item

om['menu'].delete(0, 'end')
for x in items:
    om['menu'].add_command(label=x, command=lambda item=x: callback(item))



I needed this once, because I was not able to handle unicode filenames
properly using a Tk textvariable, so you see it looks overly
complicated but in some cases may have its use.

I hope this helps


More information about the Tkinter-discuss mailing list