cavalive has asked for the wisdom of the Perl Monks concerning the following question:

I cant get it to make a gui in a thread with perl 5.8... Is the bug with perl??
#!/usr/bin/perl -w use strict; use Tk; use threads; # Create main window sub gui { my $main = MainWindow->new; # Add a Label and a Button to main window $main->Label(-text => 'Hello, world!')->pack; $main->Button(-text => 'Quit', -command => [$main => 'destroy'] )->pack; # Spin the message loop MainLoop; } my $server_thread = threads->create("gui"); while (1) { sleep(1); print "cav\n"; }

janitored by ybiC: Balanced <code> tags around code block, as per Monastery convention

Replies are listed 'Best First'.
Re: Making a TK GUI in a thread
by PodMaster (Abbot) on Sep 16, 2003 at 06:26 UTC
    Unless a package is reported as thread safe you must assume it is not thread safe. As such, you cannot use Tk outside your sub. The following works fine (no free to wrong pool errors).
    #!/usr/bin/perl -w use strict; use threads; # Create main window sub gui { require Tk; my $main = MainWindow->new; # Add a Label and a Button to main window $main->Label(-text => 'Hello, world!')->pack; $main->Button(-text => 'Quit', -command => [ $main => 'destroy'] )->pack; # Spin the message loop Tk->MainLoop; } my $server_thread = threads->create("gui"); while (1) { sleep(1); print "cav\n"; }
    Seeing how the above works, I believe Tk would be considered thread friendly. You may wish to consider using Thread::Use.

    PS - Please read the About the PerlMonks FAQ (the section on formatting your post).

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Thanks for the help... actualy I continue to have problems... what I really wanted to do was display a loading screen while my program did its various
      operations, then finally display the main gui. My plan was showing an initial gui, loading the stuff, destroying
      the gui and loading the final gui, but I continue to get
      spool errors... check it out

      use strict;
      use Thread::Use;
      use threads;
      use threads::shared;
      my $u; share ($u);

      my $load_gui = threads->create("loading_screen");

      my $counter;

      #Simulate program loading
      while ($counter < 3) {
      sleep($counter);
      $counter++;
      }
      $u = 1;#destroy loading screen
      $load_gui->join;

      #Load the final gui
      my $load_gui2 = threads->create("loadGui");

      #keep program fom ending
      while (1) {
      sleep(1);
      print "cav\n";
      }


      ##GUIS###########################

      sub loading_screen {
      useit Tk;
      my $main = MainWindow->new;
      $main->repeat(100 => sub {if ($u){$main->destroy}});

      # Add a Label and a Button to main window
      $main->Label(-text => 'Loading!')->pack;
      Tk->MainLoop;
      }

      sub loadGui {
      useit Tk;
      my $main = MainWindow->new;

      # Add a Label and a Button to main window
      $main->Label(-text => 'Main Gui')->pack;

      # Spin the message loop
      Tk->MainLoop;
      }
        You don't need threads to throw up a splash screen. See Tk::Splashscreen - display a Splashscreen during program initialization.

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Making a TK GUI in a thread
by jdtoronto (Prior) on Sep 16, 2003 at 03:38 UTC
    I think the bug is a combination thing. Threads have changed in 5.8 and Tk is reportedly not thread safe. I can't find the actual words, but I am sure I have seen it.

    This is one point where the Tcl advocates seem to have a point - Tk in Perl is not thread safe, in Tcl it is supposed to be thread safe.

    You might want to try fork() - in 5.8 that is supposed to have matured quite a lot, it may do what you need, I haven't had a chance to try it.

    jdtoronto

Re: Making a TK GUI in a thread
by batkins (Chaplain) on Sep 16, 2003 at 02:24 UTC
    Well, Tk isn't supposed to be thread-safe, but I see no reason why your code wouldn't work, since all of the Tk calls are happening in the same thread. What problem are you having? I'd test it here but my perl wasn't configured with threading.
    If I were a terrorist I'd mainly be afraid of polar bears. Because at the moment, I'm mainly afraid of polar bears, and I can't really see why that would change.
    - slashdot