GTK2 and Perl GUI – simple example

Често ми се налага да правя GUI-та за малки програмки на perl, ruby, python и за това ще постна простичък интерфейс, който показва основните неща:

#!/usr/bin/perl -w
 
use utf8;
use Gtk2 '-init';
use strict;
 
my $buffer;
my $wm   = Gtk2::Window->new;
   $wm->set_title ('GTK2 + Perl GUI');
my $vbox = Gtk2::VBox->new;
my $hbox = Gtk2::HBox->new( 0, 0 );
my $sw   = Gtk2::ScrolledWindow->new( undef, undef );
my $tw   = Gtk2::TextView->new();
my $button = Gtk2::Button->new( "Click me" );
my $entry = Gtk2::Entry->new_with_max_length( 50 );
 
sub create_widgets {
    $wm->signal_connect (destroy => sub { Gtk2->main_quit; });
 
    $wm->set_default_size( 260, 150 );
    $sw->add( $tw );
    $sw->set_policy( 'automatic', 'automatic' );
 
    $button->signal_connect( clicked => sub { \&button_click() } );
 
    $vbox->pack_start($hbox,0,0,0);
    $hbox->set_border_width(2);
 
    $hbox->pack_start( $entry, 0, 0, 0 );
    $hbox->pack_start( $button, 0, 0, 0 );
    $vbox->pack_start( $sw, 1, 1, 0 );
 
    $wm->add( $vbox );
    $wm->show_all();
}
 
create_widgets();
 
sub button_click{
    my $str = $entry->get_text();
    my $buffer = $tw->get_buffer();
    # append text:
    # $buffer->insert ($buffer->get_end_iter, "$str\n");
    $buffer->set_text($str);
}
 
Gtk2->main;

Резултат:
GTK2 Perl Simple Example

Ако все пак нямате инсталиран GTK2 на вашата дистрибуция, можете да го инсталирате така (за Debian базирани):

sudo apt-get install libgtk2.0-dev
sudo apt-get install libgtk2-perl

Leave a Reply

Your email address will not be published. Required fields are marked *

*