Help with Button Placements using TK

G

Graham Feeley

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;
 
B

Ben Morrow

Quoth "Graham Feeley said:
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
 
Z

zentara

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
 
G

Graham Feeley

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top