Tkmenubar and checkbuttons

Discussion in 'Ruby' started by Joe Van Dyk, May 5, 2005.

  1. Joe Van Dyk

    Joe Van Dyk Guest

    I have this snippet as part of my Tk menu bar spec:

    ['Options',
    ['Display Map', proc {show_map()}],
    ['Hide Map', proc {hide_map()}]

    How can I combine those two menu options into a single checkbutton
    menu option? The API documentation on it wasn't clear at all.

    Thanks,
    Joe
     
    Joe Van Dyk, May 5, 2005
    #1
    1. Advertising

  2. From: Joe Van Dyk <>
    Subject: Tkmenubar and checkbuttons
    Date: Thu, 5 May 2005 17:10:29 +0900
    Message-ID: <>
    > I have this snippet as part of my Tk menu bar spec:
    >
    > ['Options',
    > ['Display Map', proc {show_map()}],
    > ['Hide Map', proc {hide_map()}]
    >
    > How can I combine those two menu options into a single checkbutton
    > menu option? The API documentation on it wasn't clear at all.


    I'm very sorry.
    Is the following the one of the samples which you want?
    ---------------------------------------------------
    map_st = TkVariable.new

    :

    ['Options',
    ['Display Map', map_st, nil, nil, {:command=>proc{show_map()}}],
    ['Hide Map', map_st, nil, nil, {:command=>proc{hide_map()}}]
    ]
    ---------------------------------------------------
    --
    Hidetoshi NAGAI ()
     
    Hidetoshi NAGAI, May 5, 2005
    #2
    1. Advertising

  3. From: Joe Van Dyk <>
    Subject: Tkmenubar and checkbuttons
    Date: Thu, 5 May 2005 17:10:29 +0900
    Message-ID: <>
    > I have this snippet as part of my Tk menu bar spec:
    >
    > ['Options',
    > ['Display Map', proc {show_map()}],
    > ['Hide Map', proc {hide_map()}]
    >
    > How can I combine those two menu options into a single checkbutton
    > menu option? The API documentation on it wasn't clear at all.


    Maybe, do you want such like as the following?
    ----------------------------------------------------------
    require 'tk'

    m = nil # variable to pass the menu object to procedures

    spec = [
    ['Option',

    # define a command entry by Hash
    {
    :type=>'command',
    :label=>'Display Map',
    :command=>proc{
    show_map()
    m.entryconfigure('Display Map', :state=>:disabled)
    m.entryconfigure('Hide Map', :state=>:normal)
    },
    :state=>:normal
    },

    # define a command entry by Array
    ['Hide Map',
    proc{
    hide_map()
    m.entryconfigure('Display Map', :state=>:normal)
    m.entryconfigure('Hide Map', :state=>:disabled)
    },
    nil, nil,
    {:state=>:disabled}
    ]
    ]
    ]

    # add menubar and get the cascade menu object
    m = Tk.root.add_menubar(spec).entrycget('Option', :menu)

    Tk.mainloop
    ----------------------------------------------------------
    --
    Hidetoshi NAGAI ()
     
    Hidetoshi NAGAI, May 5, 2005
    #3
  4. Joe Van Dyk

    Joe Van Dyk Guest

    On 5/5/05, Hidetoshi NAGAI <> wrote:
    > From: Joe Van Dyk <>
    > Subject: Tkmenubar and checkbuttons
    > Date: Thu, 5 May 2005 17:10:29 +0900
    > Message-ID: <>
    > > I have this snippet as part of my Tk menu bar spec:
    > >
    > > ['Options',
    > > ['Display Map', proc {show_map()}],
    > > ['Hide Map', proc {hide_map()}]
    > >
    > > How can I combine those two menu options into a single checkbutton
    > > menu option? The API documentation on it wasn't clear at all.

    >
    > Maybe, do you want such like as the following?
    > ----------------------------------------------------------
    > require 'tk'
    >
    > m = nil # variable to pass the menu object to procedures
    >
    > spec = [
    > ['Option',
    >
    > # define a command entry by Hash
    > {
    > :type=>'command',
    > :label=>'Display Map',
    > :command=>proc{
    > show_map()
    > m.entryconfigure('Display Map', :state=>:disabled)
    > m.entryconfigure('Hide Map', :state=>:normal)
    > },
    > :state=>:normal
    > },
    >
    > # define a command entry by Array
    > ['Hide Map',
    > proc{
    > hide_map()
    > m.entryconfigure('Display Map', :state=>:normal)
    > m.entryconfigure('Hide Map', :state=>:disabled)
    > },
    > nil, nil,
    > {:state=>:disabled}
    > ]
    > ]
    > ]
    >
    > # add menubar and get the cascade menu object
    > m = Tk.root.add_menubar(spec).entrycget('Option', :menu)
    >
    > Tk.mainloop
    > ----------------------------------------------------------
    > --
    > Hidetoshi NAGAI ()
    >
    >


    Thanks for the example. That's close to what I originally wanted and
    will probably work well enough.

    Just to clarify, what I originally wanted was to be able to have a
    single menu entry in the menu. The menu entry should be a check
    button with the text "Display Map" next to it. When the menu checkbox
    is activated, it should call #show_map. When the checkbox is
    inactivated, it should call #hide_map.

    Thanks,
    Joe
     
    Joe Van Dyk, May 5, 2005
    #4
  5. Joe Van Dyk

    Joe Van Dyk Guest

    On 5/5/05, Joe Van Dyk <> wrote:
    > On 5/5/05, Hidetoshi NAGAI <> wrote:
    > > From: Joe Van Dyk <>
    > > Subject: Tkmenubar and checkbuttons
    > > Date: Thu, 5 May 2005 17:10:29 +0900
    > > Message-ID: <>
    > > > I have this snippet as part of my Tk menu bar spec:
    > > >
    > > > ['Options',
    > > > ['Display Map', proc {show_map()}],
    > > > ['Hide Map', proc {hide_map()}]
    > > >
    > > > How can I combine those two menu options into a single checkbutton
    > > > menu option? The API documentation on it wasn't clear at all.

    > >
    > > Maybe, do you want such like as the following?
    > > ----------------------------------------------------------
    > > require 'tk'
    > >
    > > m = nil # variable to pass the menu object to procedures
    > >
    > > spec = [
    > > ['Option',
    > >
    > > # define a command entry by Hash
    > > {
    > > :type=>'command',
    > > :label=>'Display Map',
    > > :command=>proc{
    > > show_map()
    > > m.entryconfigure('Display Map', :state=>:disabled)
    > > m.entryconfigure('Hide Map', :state=>:normal)
    > > },
    > > :state=>:normal
    > > },
    > >
    > > # define a command entry by Array
    > > ['Hide Map',
    > > proc{
    > > hide_map()
    > > m.entryconfigure('Display Map', :state=>:normal)
    > > m.entryconfigure('Hide Map', :state=>:disabled)
    > > },
    > > nil, nil,
    > > {:state=>:disabled}
    > > ]
    > > ]
    > > ]
    > >
    > > # add menubar and get the cascade menu object
    > > m = Tk.root.add_menubar(spec).entrycget('Option', :menu)
    > >
    > > Tk.mainloop
    > > ----------------------------------------------------------
    > > --
    > > Hidetoshi NAGAI ()
    > >
    > >

    >
    > Thanks for the example. That's close to what I originally wanted and
    > will probably work well enough.
    >
    > Just to clarify, what I originally wanted was to be able to have a
    > single menu entry in the menu. The menu entry should be a check
    > button with the text "Display Map" next to it. When the menu checkbox
    > is activated, it should call #show_map. When the checkbox is
    > inactivated, it should call #hide_map.
    >


    I got it to work the way I originally wanted, see below:

    require 'tk'

    m = nil # variable to pass the menu object to procedures

    map_st = TkVariable.new
    spec = [
    ['Option',

    ['Display Map', map_st, nil, nil,
    {:command => proc {if map_st.value=="1"
    show_map
    else
    hide_map
    end}}],
    ]
    ]

    def show_map
    puts "showing map"
    end

    def hide_map
    puts "hiding_map"
    end
    # add menubar and get the cascade menu object
    m = Tk.root.add_menubar(spec).entrycget('Option', :menu)

    Tk.mainloop



    Thanks to the both of you!
    Joe
     
    Joe Van Dyk, May 5, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank Chen
    Replies:
    3
    Views:
    518
    Cameron Laird
    Oct 8, 2003
  2. William Gill

    a list of Checkbuttons

    William Gill, Aug 30, 2005, in forum: Python
    Replies:
    0
    Views:
    292
    William Gill
    Aug 30, 2005
  3. Gigs_
    Replies:
    2
    Views:
    348
    Eric Brunel
    Feb 21, 2007
  4. matt
    Replies:
    6
    Views:
    174
    Hidetoshi NAGAI
    Jun 6, 2004
  5. David Douthitt
    Replies:
    8
    Views:
    276
    Hidetoshi NAGAI
    Jul 15, 2005
Loading...

Share This Page