Forum: Ruby tk_optionMenu in Ruby/Tk

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.
47df9cfb356c3ee0523cc3571b169730?d=identicon&s=25 Kenneth McDonald (Guest)
on 2008-12-03 17:12
(Received via mailing list)
Does anyone know of an example that shows the correct use of
tk_optionMenu in Ruby/Tk? All references I can find deal with popping
up a "normal" menu.

Thanks,
Ken
E4f967492dbd03c526cc9b397e68021d?d=identicon&s=25 Hidetoshi NAGAI (Guest)
on 2008-12-03 21:55
(Received via mailing list)
From: Kenneth McDonald <kenneth.m.mcdonald@sbcglobal.net>
Subject: tk_optionMenu in Ruby/Tk
Date: Thu, 4 Dec 2008 01:05:36 +0900
Message-ID: <01353FAB-8102-4C41-B542-6540128B5379@sbcglobal.net>
> Does anyone know of an example that shows the correct use of
> tk_optionMenu in Ruby/Tk? All references I can find deal with popping
> up a "normal" menu.

Please see line:559--567 of tk/menu.rb.
For example,
---------------------------------------------------------------------
require 'tk'

parent = Tk.root
v = TkVariable.new
TkLabel.new(parent, :textvariable=>v).pack
TkOptionMenubutton.new(parent, v, *%w(foo bar baz hoge fuga)).pack

# other style of arguments
TkOptionMenubutton.new(:parent=>parent, :variable=>v,
                       :values=>%w(FOO BAR BAZ HOGE FUGA)).pack
Tk.mainloop
---------------------------------------------------------------------
However, 2nd style doesn't work because of a bug of tk/menu.rb.
Please use the following patch. I'll commit it to ruby-svn.

Index: menu.rb
===================================================================
--- menu.rb  (revision 20349)
+++ menu.rb  (working copy)
@@ -569,7 +569,7 @@
     keys = _symbolkey2str(keys)

     parent = nil
-    if args[0].kind_of?(TkWindow) || args[0] == nil
+    if !args.empty? && (args[0].kind_of?(TkWindow) || args[0] == nil)
       keys.delete('parent') # ignore
       parent = args.shift
     else
@@ -577,7 +577,7 @@
     end

     @variable = nil
-    if args[0].kind_of?(TkVariable) || args[0] == nil
+    if !args.empty? && (args[0].kind_of?(TkVariable) || args[0] == nil)
       keys.delete('variable') # ignore
       @variable = args.shift
     else
47df9cfb356c3ee0523cc3571b169730?d=identicon&s=25 Kenneth McDonald (Guest)
on 2008-12-03 22:02
(Received via mailing list)
Thanks Hidetoshi!

Ken
This topic is locked and can not be replied to.