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

In reply to Re: Tk mainloop placement in sequence of calls by zentara
in thread Tk mainloop placement in sequence of calls by gsd4me

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.