[Ruby/Tk] Cannot close window

Hi,

I'm quite new on Ruby. I tried to use Ruby/Tk to display messages in a
window. But the command on the close button does not work. It returns an
error "NoMethodError: undefinded method'iconify' for nil:NilClass".

How can I do this?
Here is the code:

require 'tk'

class NotifyDialog
def initialize
  @root = TkRoot.new { title "Notification" }
  TkLabel.new {
   text "Message"
   pack
  }
  TkButton.new {
   text "Close"
   command proc { @root.iconify() }
   pack
  }
end
end

if $0 == __FILE__
gui = Thread.new {
  NotifyDialog.new
  Tk.mainloop
}
gui.join
puts "Tk closed. Do something ..."
sleep( 1 )
puts "... done."
end

Can anyone teel me whats wrong?

Thanks,
Sven

Hi,

···

From: Sven Bauhan <svenbauhan@web.de>
Subject: [Ruby/Tk] Cannot close window
Date: Fri, 28 Jan 2005 07:55:53 +0900
Message-ID: <35t9qlF4o3ho1U1@individual.net>

class NotifyDialog
def initialize
  @root = TkRoot.new { title "Notification" }
  TkLabel.new {
   text "Message"
   pack
  }
  TkButton.new {
   text "Close"
   command proc { @root.iconify() }
   pack
  }
end
end

The block given to TkButton.new method is evaluated by instance_eval.
So, in the block, self is the created button widget object.
That is the reason of why @roo is undefined.

solution 1: use a local variable
  ------------------------
  def initialize
    root = @root = TkRoot.new { title "Notification" }
    TkLabel.new {
      text "Message"
      pack
    }
    TkButton.new {
      text "Close"
      command proc { root.iconify() }
      pack
    }
   end
  ------------------------

solution 2: call command method at out of the block
  ------------------------
  b = TkButton.new {
    text "Close"
    pack
  }
  b.command{ @root.iconify } # == b.command(proc{@root.iconify})
  ------------------------
or
  ------------------------
  TkButton.new {
    text "Close"
    pack
  }.command{ @root.iconify }
  ------------------------

solution 3: use commandline arguments
  ------------------------
  TkButton.new(:text=>'Close', :command=>proc{@root.iconify}).pack
  ------------------------

--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Thanks,

that works. For me coming from C++ as 'native' language using blocks is
unfamiliar.

Sven