Help with Button Placements using TK

Discussion in 'Perl Misc' started by Graham Feeley, Aug 1, 2008.

  1. 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;
     
    Graham Feeley, Aug 1, 2008
    #1
    1. Advertising

  2. Graham Feeley

    Ben Morrow Guest

    Quoth "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????


    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, []
    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'
     
    Ben Morrow, Aug 1, 2008
    #2
    1. Advertising

  3. Graham Feeley

    zentara Guest

    On Fri, 1 Aug 2008 09:26:28 +1000, "Graham Feeley"
    <> 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
     
    zentara, Aug 1, 2008
    #3
  4. 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" <> wrote in message
    news:48924a2d$0$2271$...
    > 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;
    >
    >
     
    Graham Feeley, Aug 2, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Credent
    Replies:
    3
    Views:
    347
    ducnbyu
    May 23, 2006
  2. Credent
    Replies:
    0
    Views:
    329
    Credent
    May 23, 2006
  3. Credent
    Replies:
    0
    Views:
    276
    Credent
    May 23, 2006
  4. ise.apps
    Replies:
    0
    Views:
    930
    ise.apps
    Jun 4, 2006
  5. vikasvikas191

    Placements

    vikasvikas191, Dec 5, 2006, in forum: Java
    Replies:
    0
    Views:
    439
    vikasvikas191
    Dec 5, 2006
Loading...

Share This Page