Tkinter: how to fill values in OptionMenu dynamically

Discussion in 'Python' started by Jeffrey Barish, Apr 22, 2004.

  1. 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
    Jeffrey Barish, Apr 22, 2004
    #1
    1. Advertising

  2. Jeffrey Barish

    Peter Otten Guest

    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
    Peter Otten, Apr 22, 2004
    #2
    1. Advertising

  3. In article <>,
    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?


    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
    Russell E. Owen, Apr 22, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thor

    Optionmenu.

    Thor, Sep 4, 2003, in forum: Python
    Replies:
    4
    Views:
    398
    Michael Peuser
    Sep 5, 2003
  2. stewart
    Replies:
    1
    Views:
    314
    stewart
    Feb 23, 2004
  3. Stewart Midwinter

    How to link 3 Tkinter OptionMenu lists?

    Stewart Midwinter, Feb 25, 2004, in forum: Python
    Replies:
    2
    Views:
    655
    Eric Brunel
    Feb 25, 2004
  4. mariox19
    Replies:
    0
    Views:
    824
    mariox19
    Dec 30, 2004
  5. Replies:
    1
    Views:
    468
    Jeff Epler
    Apr 30, 2005
Loading...

Share This Page