in reply to Grabbing Text widget's contents after MainLoop

I'm confused. Given the OP code, just how do you "exit from MainLoop"?? There's no "quit" button or anything else...

I presume that while the Text widget is on the screen, you intend to type something into it, and you just want to see whatever was typed there printed to stdout on exit. Is that it? Then maybe something like this:

use strict; use Tk; my $main = MainWindow->new( -title => "Enter options:" ); my $text = $main->Text()->pack(); $main->Button(-text => "Done/Exit", -command => sub { print $text->Contents; exit(0) }, )->pack(); MainLoop;

Replies are listed 'Best First'.
Re^2: Grabbing Text widget's contents after MainLoop
by GrandFather (Saint) on Jul 22, 2005 at 05:35 UTC

    Use the main windows close box. Works on Windows, probably elsewhere.

    Nope. This code fragment is used in a (slightly) larger app that generally knows what to do, but sometimes needs to ask for a little help. The exit (0) is not all that useful in this case :).


    Perl is Huffman encoded by design.
      Ah -- well, that's not what I would call an "exit from MainLoop"... I'd call that (i.e. hitting the "X" button on the outer frame of the window) something like "killing the process window".

      I thought there might be a way to attach a callback sub (like the one in my reply) to the "Destroy" event for the text widget -- but I just tried that on my mac (where the "quartz" window manager puts its own frame with a "close" button around every X-windows-based Perl/Tk app), using  $text->bind("<Destroy>", sub { ...} ); and it didn't work. :(

      update: So, if the rest of your larger script is not GUI-based, and you're just popping up a window when some user input is needed, then yeah, I'd say "$main->destroy" instead of "exit(0)" in my callback on the Button widget, for sure.

        I added a Cancel button that used exit so your post was by no means in vain. I also used anon subs, but that was my own independent mod of pg's code.

        Thanks for the help.


        Perl is Huffman encoded by design.