in reply to Re^4: Perl/Tk code structure
in thread Perl/Tk code structure

Okay. I'm going to break this down into chunks.

Now to the "black magic" :)

By simply calling MyGUI::gui(), you've established bidirectional communications between the main program and your gui thread, without the main code needing to know any different.

And there it is, the "black magic" explained (I hope:). Loosely, skipping over much detail, but I hope with sufficiently clarity to explain the basic mechanisms. For the details, consult the appropriate documentation.

And if anything still alludes you, ask.

I'll respond to the "utf problem" and "Tk/threads caution" in a separate reply once I've had a thunk and tried a few things out, but my basic response is that the problem is not a "Tk & threads" problem, but "Tk and Unicode" problem.

(Actually I think it is more a "perl & Unicode" problem, but I'll spare you that diatribe.)


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Replies are listed 'Best First'.
Re^6: Perl/Tk code structure
by elef (Friar) on Apr 06, 2012 at 10:16 UTC
    I've been working on this project on and off based on BrowserUk's excellent advice since this thread was made, and now it's pretty much done.
    One issue I have is that when the GUI window is closed, the other thread keeps running. I've hidden the console window, so the only way to close it is via the task manager. Is there a way to automatically end the other process when the GUI window is closed or crashes?

      The simplest mechanism is to just add exit after the MainLoop(). When you exit the gui, MailLoop() returns and the program terminates with cleanup:

      sub gui { require Tk; my $mw = Tk::MainWindow->new; ... Tk::MainLoop(); exit( 0 ); ### Add this }

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

        That is indeed simple. Thank you!
      You might try something like this untested code. Also see controlling threads with Tk: while loop vs. signals
      # catch window close button to clean up threads $mw->protocol('WM_DELETE_WINDOW' => sub { &clean_exit }); sub clean_exit{ my @running_threads = threads->list; if (scalar(@running_threads) < 1){print "\nFinished\n";exit} else{ # $die = 1; # a shared var I usually use # to tell threads to return foreach my $thr (@running_threads){ $thr->join; } exit; } }

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh