Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help with Button Placements using TK

Reply
Thread Tools

Help with Button Placements using TK

 
 
Graham Feeley
Guest
Posts: n/a
 
      07-31-2008
Hi I am a Newbie and trying to get a menu to work.
I have place 3 buttons so far on a form, however I would like to know how to
place them where I want them.
EG: on this form I would like the buttons to be on the bottom of the form
and in one row????
Any help would be appreciative!!
------------------
#!/usr/bin/perl -w
# Display Hello World program
use diagnostics;
use Tk;
use strict;
use warnings;
my $font1 = "arial 10 bold";
my $font2 = "courier 12 bold";

my $mw = MainWindow->new;
$mw->geometry("1024x512");
$mw->title("GUI Test");

my $frame1 = $mw->Frame(-width=>1024,
-height=>128,
-relief=>"raised")->grid();

my $btn1 = $mw->Button(-text=>"Exit",
-font=>$font2,
-width=>9, # width of the button in screen
units
-height=>3, # height of the button in screen
units
-relief=>"raised", # raised solid ridge sunken flat
groove
-state=>"normal", # normal active or disabled
-command =>sub{exit})
->grid( -pady=>10);

my $btn2 = $mw->Button(-text=>"Btn2",
-font=>$font2,
-width=>9, # width of the button in screen units
-height=>3, # height of the button in screen units
-relief=>"raised", # raised solid ridge sunken flat groove
-state=>"normal", # normal active or disabled
-command =>sub{exit})
->grid( -pady=>10);
my $btn3 = $mw->Button(-text=>"Btn3",
-font=>$font2,
-width=>9, # width of the button in screen units
-height=>3, # height of the button in screen units
-relief=>"raised", # raised solid ridge sunken flat groove
-state=>"normal", # normal active or disabled
-command =>sub{exit})
->grid( -pady=>10);

MainLoop;


 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      08-01-2008

Quoth "Graham Feeley" <(E-Mail Removed)>:
> Hi I am a Newbie and trying to get a menu to work.
> I have place 3 buttons so far on a form, however I would like to know how to
> place them where I want them.
> EG: on this form I would like the buttons to be on the bottom of the form
> and in one row????


I generally find it easier to use the 'pack' geometry manager than
'grid'. You also really need to sort out your indentation, and use
variables and loops to avoid saying the same thing over and over.
Something like this should get you started:

#!/usr/bin/perl -w
# Display Hello World program
use diagnostics;
use Tk;
use strict;
use warnings;

my $font2 = "courier 12 bold";

my $mw = MainWindow->new;
$mw->geometry("1024x512");
$mw->title("GUI Test");

my @BTN = (
-font=>$font2,
-width=>9, # width of the button in screen units
-height=>3, # height of the button in screen units
-relief=>"raised", # raised solid ridge sunken flat groove
-state=>"normal", # normal active or disabled
);

my @btns = (
[ Exit => sub { exit } ],
[ Btn2 => sub { exit } ],
[ Btn3 => sub { exit } ],
);

for (@btns) {
my ($txt, $sub) = @$_;
$mw->Button(
@BTN,
-text => $txt,
-command => $sub,
)->pack(
-side => 'left',
-padx => 10,
-pady => 10,
-anchor => 's',
);
}

MainLoop;

__END__

Ben

--
I touch the fire and it freezes me, [(E-Mail Removed)]
I look into it and it's black.
Why can't I feel? My skin should crack and peel---
I want the fire back... BtVS, 'Once More With Feeling'
 
Reply With Quote
 
 
 
 
zentara
Guest
Posts: n/a
 
      08-01-2008
On Fri, 1 Aug 2008 09:26:28 +1000, "Graham Feeley"
<(E-Mail Removed)> wrote:

>Hi I am a Newbie and trying to get a menu to work.
>I have place 3 buttons so far on a form, however I would like to know how to
>place them where I want them.
>EG: on this form I would like the buttons to be on the bottom of the form
>and in one row????
>Any help would be appreciative!!


As Ben Morrow said, pack is usually preferred because it resizes
nicely. Get in the habit of making separate frames and using the
-expand and -fill options. You can have frames nested in frames
for very complex packing behavior.
This is how I would do it, make a top and bottom frame, but only
let the topframe expand in the y direction. Make the buttons uniform,
by letting them expand, and padx them. (You don't need to specify
the -state => 'normal'....it's normal by default.)

#!/usr/bin/perl -w
# Display Hello World program
use diagnostics;
use Tk;
use strict;
use warnings;

my $font1 = "arial 10 bold";
my $font2 = "courier 12 bold";

my $mw = MainWindow->new;
$mw->geometry( "600x400" );
$mw->title( "GUI Test" );

my $tframe = $mw->Frame(
-relief => "raised"
)->pack(-fill=>'both',-expand => 1);

my $bframe = $mw->Frame(
-relief => "raised"
)->pack(-fill=>'x',-expand =>0);

my $text = $tframe->Text()
->pack(-fill=>'both',-expand => 1);

my $btn1 = $bframe->Button(
-text => "Exit",
-font => $font2,
-relief => "raised",
-command => sub { exit }
)->pack(-side => 'left', -fill=>'x',
-expand => 1, -pady => 5, -padx => 10 );

my $btn2 = $bframe->Button(
-text => "Btn2",
-font => $font2,
-relief => "raised",
-command => sub { exit }
)->pack(-side => 'left', -fill=>'x',
-expand => 1, -pady => 5, -padx => 10 );


my $btn3 = $bframe->Button(
-text => "Btn3",
-font => $font2,
-relief => "raised",
-command => sub { exit }
)->pack(-side => 'left', -fill=>'x',
-expand => 1,-pady => 5 , -padx => 10 );

MainLoop;
__END__


zentara

--
I'm not really a human, but I play one on earth.
http://zentara.net/Remember_How_Lucky_You_Are.html
 
Reply With Quote
 
Graham Feeley
Guest
Posts: n/a
 
      08-02-2008
Thanx for the replies guys.
Whenever I get help like this I praise the internet forumsand you guys whom
are just great.
I am working on these forms
Regards
Graham

"Graham Feeley" <(E-Mail Removed)> wrote in message
news:48924a2d$0$2271$(E-Mail Removed) ...
> Hi I am a Newbie and trying to get a menu to work.
> I have place 3 buttons so far on a form, however I would like to know how
> to place them where I want them.
> EG: on this form I would like the buttons to be on the bottom of the form
> and in one row????
> Any help would be appreciative!!
> ------------------
> #!/usr/bin/perl -w
> # Display Hello World program
> use diagnostics;
> use Tk;
> use strict;
> use warnings;
> my $font1 = "arial 10 bold";
> my $font2 = "courier 12 bold";
>
> my $mw = MainWindow->new;
> $mw->geometry("1024x512");
> $mw->title("GUI Test");
>
> my $frame1 = $mw->Frame(-width=>1024,
> -height=>128,
> -relief=>"raised")->grid();
>
> my $btn1 = $mw->Button(-text=>"Exit",
> -font=>$font2,
> -width=>9, # width of the button in screen
> units
> -height=>3, # height of the button in screen
> units
> -relief=>"raised", # raised solid ridge sunken flat
> groove
> -state=>"normal", # normal active or disabled
> -command =>sub{exit})
> ->grid( -pady=>10);
>
> my $btn2 = $mw->Button(-text=>"Btn2",
> -font=>$font2,
> -width=>9, # width of the button in screen units
> -height=>3, # height of the button in screen
> units
> -relief=>"raised", # raised solid ridge sunken flat
> groove
> -state=>"normal", # normal active or disabled
> -command =>sub{exit})
> ->grid( -pady=>10);
> my $btn3 = $mw->Button(-text=>"Btn3",
> -font=>$font2,
> -width=>9, # width of the button in screen units
> -height=>3, # height of the button in screen
> units
> -relief=>"raised", # raised solid ridge sunken flat
> groove
> -state=>"normal", # normal active or disabled
> -command =>sub{exit})
> ->grid( -pady=>10);
>
> MainLoop;
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Java Project with JOB Assurance" Are you a Fresher or Missed Campus Interview Don't Worry we'll guide you to get placements ssnjavaj2eej2me@gmail.com Java 0 06-07-2006 07:27 AM
ORACLE APPS TRAINING AND PLACEMENTS ise.apps Java 0 06-04-2006 04:09 AM
Java Training*Free* and Guaranteed placements Credent Java 3 05-23-2006 09:39 PM
Java Training*Free* and Guaranteed placements Credent Java 0 05-23-2006 05:32 PM
Java Training*Free* and Guaranteed placements Credent Java 0 05-23-2006 05:32 PM



Advertisments