use strict; use warnings; use Tk; my $mw = Tk::MainWindow->new(); $mw->Button( -text => "Exit", -command => sub { $mw->destroy(); } )->pack(); # or $mw->Button( -text => "Exit", -command => \&pressExitButton )->pack(); MainLoop(); sub pressExitButton { $mw->destroy(); } #### package MyGui; use strict; use warnings; use Tk; sub new { my $this = shift; my $class = ref($this) || $this; my $self = {}; bless $self,$class; $self->buildInterface(); return $self; } sub buildInterface { my($self) = @_; my $mw = $self->_mainWindow( Tk::MainWindow->new() ); $self->_exitButton( $mw->Button( -text => "Exit", -command => sub { $self->pressExitButton(@_); } ) )->pack(); return 1; } sub pressExitButton { my($self) = @_; $self->_mainWindow()->destroy(); } sub run { my($self) = @_; MainLoop(); } sub _mainWindow { my($self,$arg) = @_; if($arg) { $self->{'_mainWindow'} = $arg; } return $self->{'_mainWindow'}; } sub _exitButton { my($self,$arg) = @_; if($arg) { $self->{'_exitButton'} = $arg; } return $self->{'_exitButton'}; } 1; package main; MyGui->new()->run();