473,326 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 11208
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stewart Midwinter | last post by:
I would like to link the contents of three OptionMenu lists. When I select an item from the first list (call it continents), the contents of the 2nd list (call it countries) would update. And in...
1
by: Martin | last post by:
Anybody know how to change the menu button that is displayed by the Tkinter Menubutton and the (derived from it) OptionMenu classes? I think it must be something from the Motif look and feel which...
0
by: mariox19 | last post by:
Hello, The Tkinter OptionMenu widget has me a bit confused. I have set up an OptionMenu to expand along the X axis as the window expands. What I find though is that the width of the submenu...
5
by: max(01)* | last post by:
hello. the following code: 1 from Tkinter import * 2 3 class MiaApp: 4 def __init__(self, genitore): 5 self.mioGenitore = genitore 6 self.i = IntVar()
5
by: max(01)* | last post by:
hello. i wrote a very simple tkinter demo program that uses menus, buttons, labels, entries, frames and secondary toplevels. it is a python version of a java program made by a colleague. ...
1
by: erikober | last post by:
I'm creating a OptionMenu button for a gui and I'm having a problem where the drop down list is so long that most of the options are off screen. The correct behavior would be that another drop...
4
by: Alex Hunsley | last post by:
Can anyone recommend some code for creating drop-down menus in tkinter? To be absolutely clear, here's an example of a drop-down: http://www.google.co.uk/preferences?hl=en (see the language...
1
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that...
2
by: Chad | last post by:
Is there anyway to set the individual options in Tkinter to a particular variable. For example, I have a menu option(code is below) which has January, February, March and so on, which I would like...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.