Question about Tkinter MenuOption variable
Rob Wolfe
rw at smsnet.pl
Thu Apr 19 03:43:09 EDT 2007
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
More information about the Python-list
mailing list