Question about Tkinter MenuOption variable

C

Chad

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 to
have corresponding values of 01, 02, 03 and so on. Can someone please
tell me how to do that within the context of the code I have below -
or even totally modify it if you must.

Label(self,
text = "Month"
).grid(row = 5, column = 1, sticky = W)
OPTIONS = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"June",
"July",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"]
default_option = StringVar(self)
default_option.set(OPTIONS[0])
self.month_option = OptionMenu(self, default_option, *OPTIONS)
self.month_option.grid(row = 5, column = 2, sticky = W)
 
R

Rob Wolfe

Chad said:
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 to
have corresponding values of 01, 02, 03 and so on. Can someone please
tell me how to do that within the context of the code I have below -
or even totally modify it if you must.

Label(self,
text = "Month"
).grid(row = 5, column = 1, sticky = W)
OPTIONS = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"June",
"July",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"]
default_option = StringVar(self)
default_option.set(OPTIONS[0])
self.month_option = OptionMenu(self, default_option, *OPTIONS)
self.month_option.grid(row = 5, column = 2, sticky = W)

What about using dictionary? For example:

<code>
import Tkinter as Tk

def state():
print OPTIONS[default_option.get()]

root = Tk.Tk()
Tk.Label(root, text="Month").grid(row=1, column=1, sticky=Tk.W)
OPTIONS = dict(Jan=1, Feb=2, Mar=3, Apr=4, May=5, June=6, July=7,
Aug=8, Sep=9, Oct=10, Nov=11, Dec=12)
# or
#OPTIONS = dict(Jan="01", Feb="02", Mar="03", Apr="04", May="05",
June="06", July="07",
# Aug="08", Sep="09", Oct="10", Nov="11", Dec="12")

default_option = Tk.StringVar()
default_option.set("Jan")
month_option = Tk.OptionMenu(root, default_option, *OPTIONS.keys())
month_option.grid(row=1, column=2, sticky=Tk.W)
Tk.Button(root, command=state, text='state').grid(row=2, column=1)

root.mainloop()
</code>
 
S

Scott David Daniels

Rob said:
>
What about using dictionary? For example: ....
OPTIONS = dict(Jan=1, Feb=2, Mar=3, Apr=4, May=5, June=6, July=7,
Aug=8, Sep=9, Oct=10, Nov=11, Dec=12)
# or
#OPTIONS = dict(Jan="01", Feb="02", Mar="03", Apr="04", May="05",
June="06", July="07",
# Aug="08", Sep="09", Oct="10", Nov="11", Dec="12")

or

OPTIONS = dict((m, n + 1) for n, m in enumerate(
'Jan Feb Mar Apr May June July Aug Sep Oct Nov Dec'.split()))
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top