Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Tk version Geek clock

by {NULE} (Hermit)
on Jan 02, 2003 at 00:02 UTC ( [id://223649]=note: print w/replies, xml ) Need Help??


in reply to Tk version Geek clock

Hi there,

pg is completely correct - look up Tk::after - using after or repeat is the way to do this. But on a side note, if your program is not going to be interactive you don't *need* to use MainLoop.

(I don't know why I'm mentioning this - I have a bad habit of bringing up the Wrong Way To Do It, just because I think it's interesting. I can feel the -- already.)

Instead of calling MainLoop you can write you own loop at the end that looks something like this:

while (1) { &refresh; $mw->update; sleep 1; }
But don't do that! The only good excuse that I can think of for doing something like this is if you want to popup a Tk status bar as your MainWindow while a script is doing something in the background (like parsing a really big file). In that case you don't need the interaction and the user is just sitting there and waiting for your script to stop running anyway.

By the way - I applaud your use of strict (and presumably warnings), but even with your my scoping your @blocks array is still being treated as a global. My preference is to pass a reference to you array around instead of just implying that you mean to use it globally.

What I like to do is create a $w hash and a $s hash and fill them with widgets and state information, then pass them to functions or Tk callbacks via reference. Of course, that is just a poor-persons form of OO, and works fine for simple scripts. This is what I mean:

use strict; use warnings; use Tk; my $widgets = {}; $widgets->{main} = MainWindow->new; $widgets->{button} = $widgets->{main}->Button( -text => "Whatever you do - don't push me", -command => [ \&callme, $widgets ] )->pack; MainLoop; sub callme { my $w = shift; $w->{main}->configure(-title => 'How dare you push me!'); # Anything else in the $widgets hash is available here # too - handy if you have lots of widgets you need to # control from your functions or methods. }
Anyway - just some food for thought. TMTOWTDI.
{NULE}
--
http://www.nule.org

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-25 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found