468,140 Members | 1,467 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,140 developers. It's quick & easy.

Question about Tkinter MenuOption variable

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)

Apr 19 '07 #1
2 1368

Chad wrote:
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>

--
HTH,
Rob

Apr 19 '07 #2
Rob Wolfe wrote:
Chad wrote:
>... I have a menu option(code is below).... I would like to have
corresponding values of 01, 02, 03 and so on....

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()))

--
--Scott David Daniels
sc***********@acm.org
Apr 19 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Russell E. Owen | last post: by
6 posts views Thread by max(01)* | last post: by
3 posts views Thread by William Gill | last post: by
6 posts views Thread by Tuvas | last post: by
1 post views Thread by Michael Yanowitz | last post: by
8 posts views Thread by Lie | last post: by
1 post views Thread by C Martin | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.