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

Hi Monks, I have a script that runs fine on it's own. It loops and creates a window, after interacting with the user, it does it again. Sometimes looping several times creating several windows. When it is called from another script, which is a menu to run scripts, the loop creates the windows all at once and doesn't stop at any point for user interaction. Does this have anything to do with the Mainloop from the original gui? I am stumped as to why it functions independently and not when called.
#Loop through all of the drill layers GUI.... my $exitbutton = $mw->Button(-text=>'Exit',-command=> sub { &Exit },-f +ont=> $fontname,-activebackground => "red")->pack(-pady=> '0.1c',-sid +e => 'bottom'); $balloon->attach($exitbutton, -msg => "Exit the program",-balloonposit +ion => 'mouse'); my $helpbutton = $mw->Button(-text=>'Help',-command=> sub { &Help },-f +ont=> $fontname,-activebackground => "#999900")->pack(-side =>'bottom +',-pady=> '0.1c'); my $create = $mw->Button(-text=>' Create 9 hole ',-command=> sub { &ad +d_ldi},-font=> $fontname,-activebackground => $doitcolor)->pack(-side +=>'left',-expand=>1,-fill=>'none',-pady=> '0.1c',-padx=>'0.3c'); $balloon->attach($create, -msg => "Add LDI coupons to the checked +off layers",-balloonposition => 'mouse'); my $combine = $mw->Button(-text=>'Combine',-command=> sub { &combine_l +di},-font=> $fontname,-activebackground => $doitcolor)->pack(-expand= +>1,-fill=>'none',-side => 'right',-pady=> '0.1c'); $balloon->attach($combine, -msg => "Split the center hole to a mechani +cal drill layer, and option to add coupons to more layers",-balloonpo +sition => 'mouse'); my $skip = $mw->Button(-text=>'Skip',-command=> sub { &skip_drill},-fo +nt=> $fontname,-activebackground => "yellow")->pack(-expand=>1,-fill= +>'none',-side => 'right',-pady=> '0.1c'); $balloon->attach($skip, -msg => "Skip the current drill layer",-ba +lloonposition => 'mouse'); my $status=$mw->Button(-text=>'Status',-command=> sub { &status_gu +i},-font=> $fontname,-activebackground => "#9966CC")->pack(-expand=>1 +,-fill=>'none',-side => 'right',-pady=> '0.1c'); $balloon->attach($status, -msg => "View summary of script actions",-ba +lloonposition => 'mouse'); $mw->withdraw; #avoid the jumping window bug $mw->Popup; $drillinfoadded = "No"; MainLoop; #Exit the script if $feedback equals Destroy! exit 0 if $feedback eq "Destroy!"; } #The exit button in $mw was clicked sub Exit { $feedback = "Destroy!"; $mw->destroy(); } #The skip button in $mw was pushed sub skip_drill() { push(@statusinfo," "); push(@statusinfo,"$currentdrill Skipped"); $feedback = "Skip"; $mw->destroy(); }
I await knowledge...... Thanks muchly.

Replies are listed 'Best First'.
Re: Looping Window
by graff (Chancellor) on Jul 08, 2009 at 22:36 UTC
    Let me suggest that you try to reduce the question to some minimal amount of (portable) code, such that you can post two complete, runnable scripts: some simple thing that takes the place of your "menu program", and some other simple thing that can be run by itself, and also be invoked from the menu program.

    If this minimal pair of scripts will demonstrate the same weirdness, we'll have a better chance to see why. If it doesn't, it becomes a question of figuring out what part of the removed code is causing the problem...

Re: Looping Window
by GrandFather (Saint) on Jul 08, 2009 at 23:12 UTC

    How about you eliminate everything not pertinent to the actual problem, but include the problematic loop structure (which seems to be missing from your sample) and generate something that could run stand alone?

    As presented there is too much irrelevant cruft and too many missing important pieces to determine what your problem is.

    A few general tips though:

    don't use & to call subs. If you want to pass parameters through, do it explicitly

    Don't use global variables. In fact, combining this with the previous tip, -command => sub { &Exit } becomes -command => sub {Exit ($feedback, @_)} and in Exit you:

    sub Exit { $_[0] = "Destroy!"; $mw->destroy (); }

    Use perltidy.


    True laziness is hard work
Re: Looping Window
by zentara (Cardinal) on Jul 09, 2009 at 15:38 UTC
    I agree with the others. Make a simple running example, so we can test. I can't really understand what your problem is, but whenever you use destroy() in Tk, you need to know what it is really doing. In Tk, the MainLoop and the MainWindow are linked, so destrying the mainwindow almost always leads to glitches because the eventloop gets destroyed. Usually, if you need to cycle the same window over and over, make a single mainwindow, and withdraw it... that keeps the event loop running. Then popup new toplevel windows to cycle, preferably reusing them by withdrawing, rapacking and re-raising.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku