#!/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_width+$new_height"); $widget->update; return; }