http://qs321.pair.com?node_id=878670

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

I have this code:
#!/usr/bin/perl use strict; use Tk; my $mw = MainWindow->new; $mw->title("Hi"); my $title = $mw->Entry( -text => '', -width => 20, -font => 'Courier 12 bold', -background => 'Orange', )->pack( -ipadx => 35 ); $title->focus(); $mw->Button( -text => 'Ok', -font => 'Courier 12 bold', -background => 'Orange', -command => sub{ Hello($title) }, )->pack( -side => 'left', -ipadx => 40 ); $mw->Button( -text => 'Exit', -font => 'Courier 12 bold', -background => 'Orange', -command => sub { exit } )->pack( -side => 'right', -ipadx => 40 ); MainLoop; sub Hello { my $title_val = $title->get; print "Hello - $title_val\n"; }
I have been trying to do the following:
  • How do I tell it to position the GUI at a specific location in the screen?
  • After entering something in the text, if I press "Enter", I would like it to have the same effect as pressing the "Ok" button.
  • Also, if I press "ESC" key, I would like it to exit.

    Can someone point me to the proper documentation for this?

    Thanks in advance.
  • Replies are listed 'Best First'.
    Re: Perl TK Questions
    by bichonfrise74 (Vicar) on Dec 22, 2010 at 21:25 UTC
      I think you can add this to simulate pressing the Enter / Escape keys:
      $mw->bind('<KeyRelease-Return>' => \&Hello); $mw->bind('<KeyRelease-Escape>' => sub{ exit });
      And to position in the screen:
      $mw->geometry('100x200+1500+1500'); # which represents the size + position

        I guess it depends on your screen. To centre the main window on my laptop

        $mw->geometry('200x200+550+250');

        works for me.

          If your window has 400x300 dimensions then
          $mw->geometry('400x300+'.int(($mw->screenwidth-400)/2).'+'.int(($mw->s +creenheight-300)/2));
          will help you.

          But I see in some systems geometry works strange.
          1. If I use KDE4.5.2 without 3d then all works fine.
          2. If I use Compiz on Kubuntu 10.10 then some time windows position is random. In old versions of Kubuntu all was fine. May be this is temporary.
          3. Some problems was on Windows with StrawberyPerl.
    Re: Perl TK Questions
    by zentara (Archbishop) on Dec 23, 2010 at 14:37 UTC
      Binding to just plain Return is enough.
      #!/usr/bin/perl use strict; use Tk; my $mw = MainWindow->new; $mw->title("Hi"); my $title = $mw->Entry( -text => '', -width => 20, -font => 'Courier 12 bold', -background => 'Orange', )->pack( -ipadx => 35 ); ########################################### $title->bind('<Return>', \&Hello); ###########################################3 $title->focus(); $mw->Button( -text => 'Ok', -font => 'Courier 12 bold', -background => 'Orange', -command => sub{ Hello($title) }, )->pack( -side => 'left', -ipadx => 40 ); $mw->Button( -text => 'Exit', -font => 'Courier 12 bold', -background => 'Orange', -command => sub { exit } )->pack( -side => 'right', -ipadx => 40 ); MainLoop; sub Hello { my $title_val = $title->get; print "Hello - $title_val\n"; }

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh
        You can use this code to center your widget
        #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new; $mw->title("Hi"); my $title = $mw->Entry( -text => '', -width => 20, -font => 'Courier 12 bold', -background => 'Orange', )->pack( -ipadx => 35 ); $title->focus(); $mw->Button( -text => 'Ok', -font => 'Courier 12 bold', -background => 'Orange', -command => sub{ Hello($title) }, )->pack( -side => 'left', -ipadx => 40 ); $mw->Button( -text => 'Exit', -font => 'Courier 12 bold', -background => 'Orange', -command => sub { exit } )->pack( -side => 'right', -ipadx => 40 ); # center your widget center_widget($mw); MainLoop; sub Hello { my $title_val = $title->get; print "Hello - $title_val\n"; } #================================================ # But : Center your widget #================================================ sub center_widget { my ( $widget ) = @_; # Height and width of the screen my $width_screen = $widget->screenwidth(); my $height_screen = $widget->screenheight(); # update le widget pour recuperer les vraies dimensions $widget->update; my $width_widget = $widget->width; my $height_widget = $widget->height; # On centre le widget en fonction de la taille de l'ecran my $new_width = int( ( $width_screen - $width_widget ) / 2 ); my $new_height = int( ( $height_screen - $height_widget ) / 2 ); $widget->geometry($width_widget . 'x' . $height_widget . "+$new_widt +h+$new_height"); $widget->update; return; }