Binding or executing a command with a TkOptionMenubutton

Discussion in 'Ruby' started by Alex DeCaria, Mar 16, 2007.

  1. Alex DeCaria

    Alex DeCaria Guest

    I want my program to execute a command whenever a new selection is made
    from a TkOptionMenubutton. The -command option isn't allowed with
    TkOptionMenubuttons, so I've been experimenting with various bindings,
    but can't find an appropriate binding event.

    I've tried the following simple example which should print the value of
    the selection, but it only works if the mouse is directly over the
    button itself, and not over the expanded options.

    Any ideas how to do this?

    #------------------------------------
    require 'tk'
    root = TkRoot.new.title("MenuOptionbutton")

    list = ["1", "2", "3", "4", "5"]
    var = TkVariable.new
    button = TkOptionMenubutton.new(root,var,*list).pack
    button.bind('ButtonRelease-1') do
    print var, "\n"
    end

    Tk.mainloop

    #--------------------------------------------

    --
    Posted via http://www.ruby-forum.com/.
    Alex DeCaria, Mar 16, 2007
    #1
    1. Advertising

  2. From: Alex DeCaria <>
    Subject: Binding or executing a command with a TkOptionMenubutton
    Date: Fri, 16 Mar 2007 22:13:22 +0900
    Message-ID: <>
    > I've tried the following simple example which should print the value of
    > the selection, but it only works if the mouse is directly over the
    > button itself, and not over the expanded options.
    >
    > Any ideas how to do this?


    For example,
    -----------------------------------------------------------------
    require 'tk'
    root = TkRoot.new.title("MenuOptionbutton")

    list = ["1", "2", "3", "4", "5"]
    var = TkVariable.new
    button = TkOptionMenubutton.new(root,var,*list).pack
    menu = button.menu
    menu.bind('ButtonRelease-1', '@%y'){|y|
    value = menu.entrycget(y, :value)
    print var, ' -> ', value, "\n"
    }
    # OR,
    #menu.bind('ButtonRelease-1', '%y'){|y|
    # value = menu.entrycget("@#{y}", :value)
    # print var, ' -> ', value, "\n"
    #}

    Tk.mainloop
    -----------------------------------------------------------------
    --
    Hidetoshi NAGAI ()
    Hidetoshi NAGAI, Mar 16, 2007
    #2
    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. simon
    Replies:
    0
    Views:
    641
    simon
    May 24, 2005
  2. Carlos

    executing remote command

    Carlos, Jul 27, 2004, in forum: Java
    Replies:
    4
    Views:
    597
    zoopy
    Jul 28, 2004
  3. Arun
    Replies:
    4
    Views:
    325
    anonymous
    Jan 3, 2005
  4. Sharp Tool
    Replies:
    7
    Views:
    19,581
    Andrew Thompson
    Jul 20, 2005
  5. Irmen de Jong
    Replies:
    2
    Views:
    427
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=
    Aug 12, 2003
Loading...

Share This Page