📜 ⬆️ ⬇️

Perl and GUI. Part 1

In the previous post, we sorted out the libraries for writing a Perl GUI. Therefore, you can get to work.

Today we will try to create a simple application (window and main menu).
I will use Tkx and ActivePerl.

To get started, you need to understand the basic principles.
')

Tkx is a layer between the Tcl language and the Tk library. That is, everything we write using Tkx is actually called in the Tcl environment.

Tcl is a command tool language. There is nothing complicated in it, the syntax is simple:
1 2 ..N
Also in the language there is support for packages (or modules as in Perl), the call will be as follows
:: 1 2 ..N

The basis of Tk is widgets. Each created widget has its own name.

Example
. - the main window.
.f - main frame.
.fb - button on the main frame.

Tkx provides an object-oriented approach, so you don’t need to worry about names. Tkx will do everything himself. The convention also applies to calls (this is the most important). Below are examples.

TCL: package require BWidget
Perl: Tkx::package_require('BWidget');

TCL: ttk::button .b -text "Hello World" -command exit
Perl: my $b = $main_window->new_ttk__button( -text => 'Hello World', -command => sub { exit; } );

TCL: tk_messageBox -message "Press OK"
Perl: Tkx::tk___messageBox( -message => 'Press OK' );

If we want to perform any action on an object that has already been created, then a shorter form can be used (the prefix g_ is used). For example, you need to set the window title

Tcl:
wm title . "Main Window"

Full form:
Tkx::wm_title( '.', 'Main Window' );

Brief:
$main_window->g_wm_title( 'Main Window' );

In other words, '::' in perl is '__' (two underscores), a space in compound commands is one underscore '_', and the underscore in Tcl is indicated by 3 underlines in Perl '___';

Each program must have a call to the main event loop (MainLoop), or
Tkx::MainLoop();

The Tcl / Tk command reference can be found here .

And now a working example.
image

Source:
  #! / usr / bin / perl
 use strict;
 use Tkx;

 our $ PROGNAME = 'MyProgram';
 our $ VERSION = '0.1';

 # create the main window
 my $ main_window = Tkx :: widget-> new ('.');

 # set the title
 $ main_window-> g_wm_title ('Main Window');

 # create and attach menu
 $ main_window-> configure (-menu => make_menu ($ main_window));


 # routine to create the main menu
 sub make_menu {
     my $ mw = shift;
    
     # disable the menu release mode (similar to GIMP)
     Tkx :: option_add ('* tearOff', 0);
    
     # depending on the OS, the Ctrl / Control button identifier may change
     my $ control = ($ ^ O eq "darwin")?  "Command": "Control";
     my $ ctrl = ($ ^ O eq "darwin")?  "Command-": "Ctrl +";
    
     # upper levels
     my $ menu = $ mw-> new_menu ();
     my $ menu_file = $ menu-> new_menu ();
     my $ menu_help = $ menu-> new_menu ();
    
     $ menu-> add_cascade (
         -label => 'File',
         -menu => $ menu_file,
     );
    
     $ menu-> add_cascade (
         -label => 'Help',
         -menu => $ menu_help,
     );
    
     # Add items to the File menu
     $ menu_file-> add_command (
         -label => 'Quit',
         -command => sub {$ mw-> g_destroy ();  },
     );
    
     # Help menu
     $ menu_help-> add_command (
         -label => 'About ...',
         -command => sub {
             Tkx :: tk ___ messageBox (
                 -title => 'About ...',
                 -message => "$ PROGNAME $ VERSION",
             );
         },
     );
    
     # return menu
     return $ menu;    
 }

 # run the main loop
 Tkx :: MainLoop (); 


in the next part, we will look at the main widgets, as well as some features in working on OSX.

Source: https://habr.com/ru/post/65466/


All Articles