Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: Problems with Tk::SplashScreen and Tk::Splash

by JediWizard (Deacon)
on Jun 15, 2005 at 18:26 UTC ( [id://467003]=note: print w/replies, xml ) Need Help??


in reply to Re: Problems with Tk::SplashScreen and Tk::Splash
in thread Problems with Tk::SplashScreen and Tk::Splash

Thank you for your help. Unfortunately this does not quite achieve what I want. For some (slightly frustrating) reason, execution of the program is halted while the splash screen is displayed (the screen does not appear until the call to destroy, which is a blocking call (see below)). I want the splash screen to display while initialization continues in the background.
Yarr!

#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Splashscreen; my $mw = MainWindow->new; $mw->Button( -text => 'Exit', -command => sub {exit} )->pack; my $sp = $mw->Splashscreen; $sp->Label(-text => 'Tk... Ick!', -font => [-size => 250])->pack; $sp->Splash; #nothing displayed yet sleep(10); # Still no splash screen $sp->Destroy(5000); # Now we see the splash screen # But execution of the program is on hold until # the screen goes away... Boo! MainLoop;

They say that time changes things, but you actually have to change them yourself.

—Andy Warhol

Replies are listed 'Best First'.
Re^3: Problems with Tk::SplashScreen and Tk::Splash
by thundergnat (Deacon) on Jun 15, 2005 at 19:13 UTC

    You need to specify the time in the ->Splash call and do an update before you start your initialization. That way the splash screen will be forced to display before it starts.

    here's another example. Uncomment the ->withdraw line to hide the main window during the splash.

    #!/usr/local/bin/perl -w use strict; use vars qw($mw $sp); use Tk; BEGIN{ require Tk::Splashscreen; use Tk; $mw = MainWindow->new(-title=>'text'); $sp = $mw->Splashscreen(); $sp->Label( -text => 'Starting up', -width => 20, -height => 10, -font => [-size => 50] )->pack(); $sp->Splash(5000); # init splash screen $sp->update; # and display it #$mw->withdraw; # hide main widow during init for my $x(0..20) # junk { for my $y (0..20) { $mw->Label( -text => "$x:$y", )->grid( -row => $x, -column => $y ); } } sleep 3; # and a little wait if desired $sp->Destroy(); # get rid of Splash $mw->deiconify; # and show main window } MainLoop;

      You got it. Thank you much. thundergnat++


      They say that time changes things, but you actually have to change them yourself.

      —Andy Warhol

Re^3: Problems with Tk::SplashScreen and Tk::Splash
by zentara (Archbishop) on Jun 16, 2005 at 11:04 UTC
    NEVER use "sleep" in a GUI event loop, or you will block your gui from functioning the way you expect. Tk has a non-blocking sleep :
    #instead of sleep(10); #use $mw->after(10);
    and your code example will run.

    In GUI programing always think in terms of the "event loop". The event loop dosn't start to run until you get to the MainLoop line. But if you sleep(10), it takes 10 seconds to start.


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

      I used sleep simply to illustrate a point in my example. My real application does several select statemnets froma database, and populates widgets based on them, during initialization. This takes time, hence the need for a splash screen.


      They say that time changes things, but you actually have to change them yourself.

      —Andy Warhol

        Sorry, I realize you may have used sleep for a quick demo, but I just have to "pick this bone" whenever I see sleep used in GUI code, so that newbies won't think that it is okay to throw sleep into the Tk or gtk2 scripts. The only place sleep should ever be seen in a Tk app is in a thread or after a fork-and-exec. You did notice that your code ran fine without the sleep statements? So it wasn't a problem with the splash screen, it was a problem with sleep in your example. And probably a design problem, where you are trying to use the splash screen as a "please-wait-loading" indicator for your data.

        Sleep in a GUI actually STOPS the GUI event loop from working at all, and using it to demonstrate that some GUI function isn't working, is never the right thing to do. Its like pulling the plug, to demonstrate that the toaster isn't working.

        Sleep stops the "execution pointer" from moving, and in a GUI it must be kept moving. Otherwise the splash will not be destroyed at the desired time, because it's counter was'nt running due to the sleep command.

        Now in your particular case, the sleep demonstrated the problem, but for all the wrong reasons, which is totally misleading. Once again sorry, if I tried to beat you over the head with this. :-)


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://467003]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 17:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found