Question about Tkinter MenuOption variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chad

    #1

    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_opti on = OptionMenu(self , default_option, *OPTIONS)
    self.month_opti on.grid(row = 5, column = 2, sticky = W)

  • Rob Wolfe

    #2
    Re: Question about Tkinter MenuOption variable


    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_opti on = OptionMenu(self , default_option, *OPTIONS)
    self.month_opti on.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").g rid(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(r oot, default_option, *OPTIONS.keys() )
    month_option.gr id(row=1, column=2, sticky=Tk.W)
    Tk.Button(root, command=state, text='state').g rid(row=2, column=1)

    root.mainloop()
    </code>

    --
    HTH,
    Rob

    Comment

    • Scott David Daniels

      #3
      Re: Question about Tkinter MenuOption variable

      Rob Wolfe wrote:
      Chad wrote:
      >... I have a menu option(code is below).... I would like to have
      >correspondin g 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
      scott.daniels@a cm.org

      Comment

      Working...