Tk - Restart after mainloop exits?

Ralf Fassel wrote:

…I think the TCL-idiom to prevent
windows/applications being destroyed by the decoration buttons is to
use the `wm’ command, like this:

wm protocol {.} WM_DELETE_WINDOW my_deletion_prevention_routine

Binding to the destroy' event is too late, things have happened already when this triggers. See the wm’ manpage for details.
Tk Built-In Commands - wm manual page

Great; that leads here:

require ‘tk’
top = TkRoot.new()
canvas = TkCanvas.new(top) {width(400);height(300) }
TkcText.new(canvas, 100, 200) { text ‘hello world’ }
canvas.grid

top.protocol(‘WM_DELETE_WINDOW’, proc{puts ‘huh?’})

Tk.mainloop
canvas = TkCanvas.new() {width(400); height(300) }
TkcText.new(canvas, 200, 100) { text ‘hello world’ }
canvas.grid
Tk.mainloop

But what’s “my_deletion_prevention_routine”? The documentation lists several
“Tk.quit” and “callback_break” routines. Tk.quit does not seem to exist (in
Ruby), and callback_break creates the error “Tk callback returns ‘break’
status”

···


Phlip
http://www.c2.com/cgi/wiki?TestFirstUserInterfaces

But what’s “my_deletion_prevention_routine”?

It’s just any function you like to get called when the user presses
the button which causes the DELETE_WINDOW request (it’s the `puts’ in
your example). Usually it is some cleanup routine (eg, the same as
you would have under File->Exit), but it might also
be just (again, in Tk notation)
wm protocol {.} WM_DELETE_WINDOW { # do nothing }
wm protocol {.} WM_DELETE_WINDOW { wm withdraw {.} }
In these examples I assume you know what you’re doing there, and that
your users have some other means of terminating the application :wink:

R’

Ralf Fassel wrote:

But what’s “my_deletion_prevention_routine”?

It’s just any function you like to get called when the user presses
the button which causes the DELETE_WINDOW request (it’s the `puts’ in
your example). Usually it is some cleanup routine (eg, the same as
you would have under File->Exit), but it might also
be just (again, in Tk notation)
wm protocol {.} WM_DELETE_WINDOW { # do nothing }
wm protocol {.} WM_DELETE_WINDOW { wm withdraw {.} }

I’m not asking how callbacks work.

I’m asking, specifically, what to write in that location that makes this
code not crash:

require ‘tk’
top = TkRoot.new()
canvas = TkCanvas.new(top) {width(400);height(300) }
TkcText.new(canvas, 100, 200) { text ‘hello world’ }
canvas.grid

top.protocol(‘WM_DELETE_WINDOW’, proc{puts ‘huh?’})

Tk.mainloop
canvas = TkCanvas.new() {width(400); height(300) }
TkcText.new(canvas, 200, 100) { text ‘hello world’ }
canvas.grid
Tk.mainloop

The way it currently works, you see a window, you close it, Tk tries to
create another window, and crashes.

Someone wrote “my_deletion_prevention_routine” because they assumed I knew
how to prevent deletion.

In these examples I assume you know what you’re doing there, and that
your users have some other means of terminating the application :wink:

It’s not an application. But, with the current control flow, merely adding a
handler to the event does not prevent the event from propagating to the
window, destroying it, halting the Tk mainloop, and destroying the Tk
singleton.

I don’t want the Tk singleton destroyed. I want to be able to create a new
window after Tk.mainloop exits.

Some have offered, “create two toplevel windows, so the other one stays up
and keeps Tk.mainloop going”. This also does not work, because I need
Tk.mainloop to exit and not halt forever.

The documentation says to use either quit or callback_break there. Both
crash.

···


Phlip
http://www.c2.com/cgi/wiki?TestFirstUserInterfaces

Hi,

···

From: “Phlip” phlipcpp@yahoo.com
Subject: Re: Tk - Restart after mainloop exits?
Date: Thu, 29 May 2003 13:19:12 +0900
Message-ID: bb41md$7rh@dispatch.concentric.net

I don’t want the Tk singleton destroyed. I want to be able to create a new
window after Tk.mainloop exits.

If your Ruby/Tk supports Tk.restart, please try it.

                              Hidetoshi Nagai (nagai@ai.kyutech.ac.jp)

top = TkRoot.new()
canvas = TkCanvas.new(top) {width(400);height(300) }
^^^
TkcText.new(canvas, 100, 200) { text ‘hello world’ }
canvas.grid

top.protocol(‘WM_DELETE_WINDOW’, proc{puts ‘huh?’})

Tk.mainloop
canvas = TkCanvas.new() {width(400); height(300) }
^
Shouldn’t there be something in there?

Maybe you need to set up a new `top’ after Tk.mainloop returns?
But I don’t really know that specific programming language you’re
using there, so no further input from me I’m afraid :-/

R’

Hidetoshi NAGAI wrote:

I don’t want the Tk singleton destroyed. I want to be able to create a
new
window after Tk.mainloop exits.

If your Ruby/Tk supports Tk.restart, please try it.

YEEEHAW! Hot like Wasabi eye drops!

Thanks, Hidetoshi! And no more f***ing callbacks!

(In my context, stopping and restarting Tk is better style than rescuing and
re-using the previous one.)

···


Phlip
greencheese.org - This website is for sale! - greencheese Resources and Information.
“I must decline your invitation owing to a subsequent engagement.” –
Oscar Wilde