Forum: Ruby Binding or executing a command with a TkOptionMenubutton

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
56c8db2891b2d2714ff22be005859285?d=identicon&s=25 Alex DeCaria (deke)
on 2007-03-16 14:13
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

#--------------------------------------------
E4f967492dbd03c526cc9b397e68021d?d=identicon&s=25 Hidetoshi NAGAI (Guest)
on 2007-03-16 15:10
(Received via mailing list)
From: Alex DeCaria <alex.decaria@millersville.edu>
Subject: Binding or executing a command with a TkOptionMenubutton
Date: Fri, 16 Mar 2007 22:13:22 +0900
Message-ID: <a6b20465799fb0ab3481671426ff9d65@ruby-forum.com>
> 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
This topic is locked and can not be replied to.