On 11 Mar 2003 at 10:26, Daniel Carrera wrote:

> I want to make a dialog using Tk (and I don't want to use TkDialog 
> because it would be more difficult to explain).  I'm having trouble 
> destroying a TkToplevel window.  My entire code is included below.
> 
> I have a main window with a button called 'Callback'.  If you click on 
> it you get the dialog, which has a 'Cancel' button, which simply calls 
> the 'destroy' method on the TkToplevel window.  When you click on it 
> though, I get an error pop-up saying:
> 
>    Error: invalid command name
>    ".w0005.w0006"
> 
> Does anyoone know what I'm doing wrong?  Or is there an easier way to do 
> what I want?  Here is my code:

Hmm, this is an interesting puzzle. The direct cause of the error 
message you're seeing is that the Tk widget ID (".w0005.w0006") is 
out of scope at the time the command is invoked. But exactly *why* 
it's out of scope is another question, which I can't answer.

The good news is that I have a solution for you:

class Dialog < TkToplevel
    def initialize(master, &block)
        super
        button_quit = TkButton.new(dialog)   { text "Cancel"  }
        # Generally you should use the 'command' option for Button
        # widgets. You can still use a custom binding if you need 
        # special behavior.
        button_quit.configure("command" => Proc.new { destroy })
        button_quit.pack("side" => "bottom")
    end
end

# 'master' argument should be the root window
def callback(master)
    Dialog.new master { title "Add Contact" }
end


I wish I could explain better what's going on, but I haven't been 
doing much Ruby lately, and it's dinnertime. Hope this helps.

--
Matt Gushee
Englewood, CO USA