Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Tkinter: how to fill values in OptionMenu dynamically

Reply
Thread Tools

Tkinter: how to fill values in OptionMenu dynamically

 
 
Jeffrey Barish
Guest
Posts: n/a
 
      04-22-2004
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


 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      04-22-2004
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

 
Reply With Quote
 
 
 
 
Russell E. Owen
Guest
Posts: n/a
 
      04-22-2004
In article <(E-Mail Removed)>,
Jeffrey Barish <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
OptionMenu Look Martin Python 0 07-12-2004 01:18 AM
How to link 3 Tkinter OptionMenu lists? Stewart Midwinter Python 2 02-25-2004 09:15 AM
Pmw optionMenu dynamic contents display? stewart Python 1 02-23-2004 07:36 AM
Optionmenu. Thor Python 4 09-05-2003 07:40 PM



Advertisments