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

Hey guys, I somehow screwed up this code. When it runs, it compiles fine, but then it just opens a blank command prompt window. Tk is installed properly, and I can get a basic hello world program running perfectly. Pretty much everything inside sub push_button is from an old program, and I know it works fine. The arrays normally have much more stuff in them.

#!/usr/bin/perl use strict; use warnings; use diagnostics; use Tk; srand; my $timelimits = 1; my @numbers = (1..10); my @building = ("Building"); my @adverb = ("Adverb"); my @verb = ("Verb"); my $mw = new MainWindow; my $title = $mw -> Label(-text=>"Starcraft Strategy Generator") -> gri +d(); my $howmany = $mw -> Label(-text=>"How many strategies?") -> grid(); my $ent = $mw -> Entry() -> grid(); my $button = $mw -> Button(-text=>"Generate Strategies", -command => \ +&push_button) -> grid(); my $checkminutes = $mw -> Checkbutton(-text=>"Use time limits?", -vari +able=>\$timelimits) -> grid(); $checkminutes -> deselect(); my $output = $mw -> Text(-width=>500, -height=> 240) -> pack(); MainLoop; sub push_button { my $builds = $ent -> get(); while(0<$builds) { if($timelimits == 1) { my $numbersrand = rand @numbers; my $buildingrand = rand @building; my $adverbrand = rand @adverb; my $verbrand = rand @verb; my $numbersout = $numbers[$numbersrand]; my $minuteout = "Minute"; my $buildingout = $building[$buildingrand]; my $adverbout = $adverb[$adverbrand]; my $verbout = $verb[$verbrand]; $output -> insert('end',"$numbersout $minuteout $buildingout $adverbou +t $verbout\n"); $builds --; } else { my $numbersrand = rand @numbers; my $buildingrand = rand @building; my $adverbrand = rand @adverb; my $verbrand = rand @verb; my $numbersout = $numbers[$numbersrand]; my $buildingout = $building[$buildingrand]; my $adverbout = $adverb[$adverbrand]; my $verbout = $verb[$verbrand]; $output -> insert("$numbersout $buildingout $adverbout $verbout\n"); $builds --; } } $output -> insert("Done\n"); }

Replies are listed 'Best First'.
Re: Tk problem
by kcott (Archbishop) on Sep 01, 2011 at 04:37 UTC

    Your problem is mixing the geometry managers Tk::grid and Tk::pack in the one frame (in this case, the special frame Tk::MainWindow).

    If you want to mix them, put the first five widgets in their own Tk::Frame.

    I changed the lone pack() to grid() and your reported problem was resolved, i.e. a window now appears.

    my $output = $mw -> Text(-width=>500, -height=> 240) -> grid();

    I didn't check any functionality beyond this.

    -- Ken

      Thanks, I had no idea you had to stick to one, I thought you could just mix and match pack and grid.

Re: Tk problem
by Khen1950fx (Canon) on Sep 01, 2011 at 08:15 UTC
    From what I can see, you're a little confused about the difference between pack and grid. In your snippet, you were using the pack manager. I wish that we could just switch managers by switching words, but we can't just yet.

    Here's how it works with pack. I pared back a little. The push_button wasn't much help, so I left the callbacks out.

    #!/usr/bin/perl use strict; use warnings; use Tk; my $mw = MainWindow->new; $mw->Label( -text => 'Starcraft Strategy Generator' )->pack; $mw->Button( -text => 'Generate Strategies' )->pack; $mw->Entry->pack; $mw->Checkbutton( -text => 'Use time limits?' )->pack; $mw->Entry->pack; $mw->Text( -width => 20, -height=> 10 )->pack; MainLoop;
    If you can post some more specific info, I can help you set it up using the grid manager.