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

Hello Guys, I have a problem using Perl TK. I wanna arrange my buttons and entry's using "pack". I would like to have my button under the "third" entry not after all my entry lines. Is using "pack" the way to go? And also how can i call another perl script in the GUI and save the outputs inside a variable. qx() command is not working. I am new to this language and i would appreciate some advices/solutions. Thank you in advance!

#!/usr/local/bin/perl -w use strict; use Tk; use Tk::ProgressBar; # Main Window my $mw = new MainWindow; $mw->geometry("1600x900"); #GUI Building Area my $label = $mw ->Label (-text => "first", -foreground => "blue", -fon +t => "Arial 7") ->pack(-fill => 'x'); $label = $mw ->Label (-text => "second", -foreground => "blue", -fo +nt => "Arial 12") ->pack(-fill => 'x'); $label = $mw ->Label (-text => "third", -foreground => "blue", -fon +t=> "Arial 10") ->pack (-side => 'left', -anchor => 'nw', -ipadx => ' +50', -fill => 'x'); $label = $mw ->Label (-text => "forth", -foreground => "blue", -fon +t=> "Arial 10")->pack (-side => 'left', -anchor => 'nw', -ipadx => '1 +50', -fill => 'x'); $label = $mw ->Label (-text => "fifth", -foreground => "blue", -fon +t => "Arial 10")->pack (-side => 'left', -anchor => 'nw', -ipadx => ' +150', -fill => 'x'); $label = $mw ->Label (-text => "last", -foreground => "blue", -font +=> "Arial 10")->pack (-side => 'left', -anchor => 'nw', -ipadx => '15 +0', -fill => 'x'); ############################################### # # Creating the buttons # ############################################### my $dir; my $path_dir; # Create the car folders button my $cf = $mw->Button( -text => 'Create car folders', -bac => 'lightblue', -width => '17', -command => \&push_button)->pack(-side => 'left'); MainLoop;

Replies are listed 'Best First'.
Re: Perl TK GUI!
by zentara (Cardinal) on Feb 13, 2014 at 12:24 UTC
    First, your pack problem. Generally the way you would organize the screen, is to nest frames inside one another, to whatever depth you need to achieve your desired effect. You must remember to set your packing options though, like expand, fill, and whatever. Generally the idea is to arrange it, so that as the GUI Mainwindow is resized, it all stays together in a nice way.

    For your given code, you are not using a very good approach, but for the sake of teaching you, I will slightly modify it to show the principle.

    #!/usr/bin/perl -w use strict; use Tk; use Tk::ProgressBar; # Main Window my $mw = new MainWindow; # take out your geometry line to see what is happening, # see how it resizes after removing the following line # $mw->geometry("1600x900"); #GUI Building Area #make the 2 header frames my $topframe = $mw->Frame()->pack(-expand=>0, -fill=>'x'); my $secframe = $mw->Frame()->pack(-expand=>0, -fill=>'x'); my $label = $topframe ->Label (-text => "first", -foreground => "blue" +, -font => "Arial 7") ->pack(-fill => 'x'); $label = $secframe ->Label (-text => "second", -foreground => "blue", -font => "Arial 12") ->pack(); # notice no fill +on this button my $mainframe = $mw->Frame->pack(-expand=>1, -fill=>'both'); # make subframes for the rest, so you can add buttons my %frames; foreach my $pos('third','forth','fifth','rest'){ $frames{$pos} = $mainframe->Frame(-bg=>'white')->pack(-side=>'left', -expand=>1, -fill=>'both'); my $label = $frames{$pos} ->Label (-text => $pos, -foreground => "bl +ue", -font=> "Arial 10") ->pack (-side => 'top', -anchor => 'nw', -ipa +dx => '50', -fill => 'x'); ############################################### # # Creating the button my $button = $frames{$pos}->Button( -text => 'Create car folders '. $pos, -bac => 'lightblue', -width => '17', -command => \&push_button)->pack(-side => 'top', -anchor=>'nw'); } MainLoop;
    as to your IPC problem, you ususally need to use fileevent to read an external socket or process. See tk-ps or the following code:
    #!/usr/bin/perl use warnings; use strict; use Tk; use IPC::Open3; $|=1; my $pid=open3(\*IN,\*OUT,0,'/bin/sh'); my $mw = new MainWindow(-title=>'Left Click->Update Right Click->Clo +se'); $mw->fontCreate('medium', -family=>'courier', -weight=>'bold', -size=>int(-14*14/10)); my $text = $mw->Scrolled('Text', -bg => 'black', -fg => 'lightsteelblue', -width => 130, -height => 30, -font => 'medium', )->pack; $mw->fileevent(\*OUT,'readable',\&get_from); $mw->bind('<ButtonPress-1>' => sub{ $text->delete('1.0','end'); $text->update; print IN "ps auxww\n"; }); $mw->bind('<ButtonPress-3>' => sub{ exit }); print IN "ps auxww\n"; MainLoop; sub get_from { my $pstext = (<OUT>); $text->insert('end',$pstext); $text->see('1.0'); } __END__

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

      I appreciate this very much.

      It really helps me.

      Thank you one more time. Have a great day

Re: Perl TK GUI!
by Anonymous Monk on Feb 13, 2014 at 11:47 UTC

    hi

    Odd, I can't see a button at all (i got win32)

    what I would do is pack some Tk::Pane , and pack some labels/buttons inside them each pane

    big boxes first, little boxes inside

    qx() command is not working

    can you show that code

      Hi, Really odd, you should see the button....i use linux.

      So somewhere in my GUI script i call another script in a subfunction, and it's looking like that:

      my $name = $ent -> get(); my $log = qx(perl /xx/bgt/structure.pl $path_dir $name);

      The problem is that in my $log variable i can't store the output of the script. Do you know an alternative to call a script and store the output into a variable? Thank's. Both of my variables that are used in the script call are defined, the call of the script is good, just that i can;t store the output into a variable

        The reason the button is missing is the odd $mw size he has, in addition to the bizzare padding values. :-) Take out the geometry line and you may see it.

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