468,117 Members | 1,524 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,117 developers. It's quick & easy.

Tkinter: how to fill values in OptionMenu dynamically

Is there a way to fill the values in an OptionMenu dynamically? I need
something like the add_command method from Menu. Is there a better way
to implement a pull-down list?
--
Jeffrey Barish
Jul 18 '05 #1
2 9370
Jeffrey Barish wrote:
Is there a way to fill the values in an OptionMenu dynamically? I need
something like the add_command method from Menu. Is there a better way
to implement a pull-down list?


OptionMenu wasn't designed with this usage pattern in mind. But you can
force it:

import Tkinter as tk

class OptionMenu(tk.OptionMenu):
def __init__(self, *args, **kw):
self._command = kw.get("command")
tk.OptionMenu.__init__(self, *args, **kw)
def addOption(self, label):
self["menu"].add_command(label=label,
command=tk._setit(variable, label, self._command))

if __name__ == "__main__":
root = tk.Tk()

variable = tk.StringVar()
variable.set("beta")

optionMenu = OptionMenu(root, variable, "alpha", "beta", "gamma")
optionMenu.pack()

btn = tk.Button(root, text="Add",
command=lambda: optionMenu.addOption("DELTA"))
btn.pack()

root.mainloop()

If you are looking for something more robust/complete than my little adhoc
subclass, you might have a look at the Python Megawidgets (pmw.sf.net).

Peter

Jul 18 '05 #2
In article <ma**************************************@python.o rg>,
Jeffrey Barish <je********@starband.net> wrote:
Is there a way to fill the values in an OptionMenu dynamically? I need
something like the add_command method from Menu. Is there a better way
to implement a pull-down list?


I see other postings showing two common solutions. A third is to try
RO.Wdg from my RO package
<http://www.astro.washington.edu/rowen/ROPython.html>. It may be a bit
more stuff than you want, but RO.Wdg.OptionMenu allows you to set new
values whenever you want and it includes improved versions of most of
the other widgets.

-- Russell
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Stewart Midwinter | last post: by
1 post views Thread by Martin | last post: by
reply views Thread by mariox19 | last post: by
5 posts views Thread by max(01)* | last post: by
5 posts views Thread by max(01)* | last post: by
1 post views Thread by erikober | last post: by
4 posts views Thread by Alex Hunsley | last post: by
1 post views Thread by Michael Yanowitz | last post: by
2 posts views Thread by Chad | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.