472,986 Members | 3,101 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 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 11105
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.