Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Tk mainloop placement in sequence of calls

by gsd4me (Beadle)
on Mar 31, 2018 at 19:07 UTC ( [id://1212084]=perlquestion: print w/replies, xml ) Need Help??

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

Dear all,

This is probably a simple question but one that I have failed to get correct despite several attempts. I have a series of projects that I have developed to do some tasks on my laptop - no web or published modules involved. All the projects have some common areas - asking the user to specify an input file for example.

So I have a module that contains all these common procedures as ... well... procedures that access globally common data.

The problem I am having is the timing of the GUI aspects, interactions and actions that these procedures carry out.

So I have a module that upon being called by each project, initialises some global variables and also creates a new MainWindow from the tK package. There is also a defined procedure that asks what file the user wishes to open as this is done at least once (if not more) by each project So in pseudo-code:
sub myGlobalInit ... do things ... and ... $globalInitialDir = a value; $globalSelectedDir = a value; $globalMyWindow = Tk::MainWindow->new; end sub sub myGlobalaskforFile ... if defined ($globalInitialDir) # user not specified any preference - use our default localInitDir = globalInitialDir filename = globalMyWindow -> getOpenFile (-initialdir => $localInitDir + ) else if defined ($globalSelectedDir) # use what the user has selected before as our 'base' localInitDir = globalSelectedDir filename = globalMyWindow -> getOpenFile (-initialdir => $localInitDir + ) return filename end sub In project modules ... myGlobalInit(); # set up a default for globalInitialDir that differs from the default +set up in the above call $globalInitialDir = some value that I specify file = myGlobalaskForFile () ...

my Question is where do I need to place the MainLoop call for this sequence of events? after the initial main window creation or when I ask for the file or in each project's code? There are other GUI interactions required with the projects but if I can get a nice answer from this posting, then I can use that as a basis for everything else to get the sequence interaction correct.

Note that each project *will* call myGlobalaskForFile more than once during its operation to get a subset of filenames required so the call to that procedure is not a 'once only' request.

I have tried putting the mainLoop call in both these procedures but the sequence of events doesn't occur in the *correct* sequence that I wish - usually I get a blank widget after the globalInit call, I *do* get asked for the filename, but at times that I would regard as being *not* the right time but what I need to know is where *should* I putthe mainloop call to ensure that all is OK? I did read once somewhere that there should only ever be one and only one call to mainloop so putting it into "myGlobalaskForFile" seems to break that 'rule' but in my testing my code seems to work, albeit imperfectly due to the operational sequence of my code

Thanks in advance

ADB

Replies are listed 'Best First'.
Re: Tk mainloop placement in sequence of calls
by zentara (Archbishop) on Apr 01, 2018 at 13:36 UTC
    Hi, your question and code are rather vague, but it seems to me like you are trying to run 1 Tk MainLoop process, have it sleep when desired, then activate it when needed. You are right, there can only be 1 MainLoop in a Tk program because Tk is an event-loop program, and MainLoop() starts the event-sequence processing running. It would be helpful for you to better explain what you are trying to do, in the meantime, here are ways to avoid the MainLoop() call and force the event-sequence processing to run on demand.

    A simple solution is just to initiate a separate Tk MainLoop everytime you need a file. Just pop a Tk mainwindow with a file dialog, retreive the file name, then allow the Tk program to close.

    #!/usr/bin/perl use Tk; my $mw = tkinit; $mw->withdraw; # hide the empty mainwindow my $filename = $mw->getOpenFile(); # or # my $filename1 = $mw->getSaveFile(); #if you want a filename for reading or writing respectively. print "$filename\n"; use Tk::Event qw(DONT_WAIT); #MainLoop;

    Otherwise here are a few old code samples for you to study, they essentially create a manually pumped event sequence forcing one loop at a time in a while(1) loop.

    #!/usr/bin/perl # untested my $old = *{Tk::MainLoop}{CODE}; *Tk::MainLoop = sub { ... }; #Or use DoOneEvent() instead of MainLoop(), #to run only one event loop at a time. #An even better idea would be to look at #the test suite for Tk, and see how they handled it...
    #!/usr/bin/perl # by joost of perlmonks , original node number lost use Tk::Event qw(DONT_WAIT); while (1) { if ($self->run) { # test if we need to process $self->process_a_bit() # process a tiny bit } return if $self->quit; # test if quit status was set DoOneEvent(DONT_WAIT); # do normal Tk event select undef,undef,undef,0.0001; # wait for a bit } # I did this, because my processing was not easily adjusted # into the normal Tk::Eventloop processing and it needed to # be called a lot ("live" 44Kz audio stream processing, # with 100 samples per process_a_bit call) # If you want to go this route, you want to adjust the timing # of the select() and process_a_bit() calls until your GUI # and processing run fast enough, and it doesn't lock up # the whole machine when there is no processing being done :-) # Alternatively, if you're able to run your processing based # on file events or signals, you can take a look at the # Tk::Eventloop documentation for registering extra events # in the normal MainLoop.


    I'm not really a human, but I play one on earth. ..... an animated JAPH

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1212084]
Approved by Discipulus
Front-paged by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-18 19:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found