Hi all!
I have an application that pops up a window until someone
closes it, and then pops up another one until someone closes it.
But if I try to open a window the second time, this is what
happens (using Ruby 1.8.2, WinXP Pro SP2, one-click installer):
C:\tmp>irb
irb(main):001:0> require 'tk'
=> true
irb(main):002:0> t = TkRoot.new() { title "yo yo ma" }
=> #<TkRoot:0x2c8f238 @path=".">
irb(main):003:0> text = TkText.new(t)
=> #<TkText:0x2d8c288 @cmdtbl=[], @path=".w00000", @tags={}>
irb(main):004:0> Tk::mainloop
=> true
···
#
# Here, the window has popped up, and I click on the close button in the upper right...
#
irb(main):005:0> t = TkRoot.new() { title "yo yo ma" }
RuntimeError: can't invoke "wm" command: application has been destroyed
from c:/ruby/lib/ruby/1.8/tk.rb:1948:in `__invoke'
from c:/ruby/lib/ruby/1.8/tk.rb:1948:in `_invoke'
from c:/ruby/lib/ruby/1.8/tk.rb:1498:in `_ip_invoke_core'
from c:/ruby/lib/ruby/1.8/tk.rb:1532:in `_tk_call_core'
from c:/ruby/lib/ruby/1.8/tk.rb:1556:in `tk_call'
from c:/ruby/lib/ruby/1.8/tk/wm.rb:271:in `title'
from (irb):5
from (irb):5:in `instance_eval'
from c:/ruby/lib/ruby/1.8/tk/root.rb:48:in `instance_eval'
from c:/ruby/lib/ruby/1.8/tk/root.rb:48:in `new'
from (irb):5
irb(main):006:0>
Obviously, I'm doing something silly. Can anyone tell me what that is?
Thanks!
-- Glenn
Message-ID: <sTY5f.1640$Hs.640@tornado.socal.rr.com>
irb(main):004:0> Tk::mainloop
=> true
You closed the root window here, didn't you?.
When the root window is destroyed, the Tk interpreter looses
all Tk functions. So, you got the following error.
irb(main):005:0> t = TkRoot.new() { title "yo yo ma" }
RuntimeError: can't invoke "wm" command: application has been destroyed
If you want to create widgets again, you have to re-initialize
the Tk interpreter.
Please try 'Tk.restart'.
Although it possibly works, I don't recommned it because I cannot
assure you that it can completely reset the interpreter.
# If you want to hide the window, 'TkRoot#withdraw' is better.
···
From: "Glenn M. Lewis" <noSpam@noSpam.com>
Subject: Ruby/Tk question
Date: Fri, 21 Oct 2005 12:07:00 +0900
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
Yes indeed... you are exactly right. Thank you!
I found Tk.restart, and tried it out, and it worked
for the most part, but when the window was rebuilt,
the scrollbars no longer worked.
Do you know of another workaround?
Thanks again!
-- Glenn
Hidetoshi NAGAI wrote:
···
From: "Glenn M. Lewis" <noSpam@noSpam.com>
Subject: Ruby/Tk question
Date: Fri, 21 Oct 2005 12:07:00 +0900
Message-ID: <sTY5f.1640$Hs.640@tornado.socal.rr.com>
irb(main):004:0> Tk::mainloop
=> true
You closed the root window here, didn't you?.
When the root window is destroyed, the Tk interpreter looses all Tk functions. So, you got the following error.
irb(main):005:0> t = TkRoot.new() { title "yo yo ma" }
RuntimeError: can't invoke "wm" command: application has been destroyed
If you want to create widgets again, you have to re-initialize the Tk interpreter. Please try 'Tk.restart'. Although it possibly works, I don't recommned it because I cannot assure you that it can completely reset the interpreter.
# If you want to hide the window, 'TkRoot#withdraw' is better.
Message-ID: <AFA6f.1977$Jo3.327@tornado.socal.rr.com>
Yes indeed... you are exactly right. Thank you!
I found Tk.restart, and tried it out, and it worked
for the most part, but when the window was rebuilt,
the scrollbars no longer worked.
Hmm... Probably, some part of the interpreter will not be
able to initialize.
"Tk.restart" calls Tcl/Tk's C API "Tk_Init".
If the C function fails to work, it may be hard to rescue
the trouble on re-initialization.
Do you know of another workaround?
If you want to some modal windows, you should use toplevel
widgets (And hide the root widget by Tk.root.withdraw, if
you need it).
Or, if you want protect the window from close operation by
user, please use TkRoot#protocol method. For example,
···
From: "Glenn M. Lewis" <noSpam@noSpam.com>
Subject: Re: Ruby/Tk question
Date: Sun, 23 Oct 2005 09:22:00 +0900
--------------------------------------------------------
Tk.root.protocol('WM_DELETE_WINDOW'){
# operation to manage WM_DELETE_WINDOW protocol
exit if Tk.messageBox(:type=>'okcancel', :message=>'Realy?') == 'ok'
}
--------------------------------------------------------
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)