Well - now I can say I learned something today!

Your method also demonstrates how to add additional items after creation 
without using the Tkinter private routine _setit, which is also nice.

Dave

David J. Giesen | Research Scientist | FPEG US Display OLED Materials R+D 
| 
Eastman Kodak Company | 2/83/KRL MC02216 | Rochester, NY 14650 | 
david.gie...@kodak.com | 1-585-588-0480 Office | 
www.kodak.com 




From:
Michael Lange <klappn...@web.de>
To:
tkinter-discuss@python.org
Date:
11/04/2009 02:30 PM
Subject:
Re: [Tkinter-discuss] How to set optionmenu without setting the variable
Sent by:
tkinter-discuss-bounces+david.giesen=kodak....@python.org



Hi,

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

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

Actually you can:

<code>

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):
    om.configure(text=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))
callback('a')

root.mainloop()

</code>

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

Michael

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss


<<image/gif>>

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to