[Ruby/Tk] Cannot close window

S

Sven Bauhan

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
 
H

Hidetoshi NAGAI

Hi,

From: Sven Bauhan <[email protected]>
Subject: [Ruby/Tk] Cannot close window
Date: Fri, 28 Jan 2005 07:55:53 +0900
Message-ID: said:
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})
 
S

Sven Bauhan

Thanks,

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

Sven
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Ruby/Tk 4
Ruby/Tk 10
Ruby/tk Help Please 19
Ruby TK 2
A Tk window 20
Bug with Ruby/Tk encoding (ruby-1.9.1-rc1) 6
Ruby/Tk - simple task that I'm srtuck 4
Tk and Tile with Tk8.5 9

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top