Perl and GUI. Work with the menu
Overview of Tk widgets, I will start by reviewing the main menu taking into account the features of various OSs.
First, create a blank for our application:
Everything is as usual, File, Edit, Help. (cascading menu). A new object is created using the new_menu () method; (like a container)
Adding is done through add_cascade (), where
-label is the menu title.
-menu - widget
-underline - underscore letter index. It is necessary for quick navigation using the Alt button + letter (numbering goes from 0),
we look at what happened.

There is no sense from an empty menu, so we’ll “stuff” it with commands
On Quit, we made a hotkey (Ctrl + Q) using the bind method.
To indicate an “accelerator”, -accelerator is used , and when the event is pressed, the -command
command accepts a pointer to a subprogram, and if you need to pass parameters to it, then we use the anonymous array
[\ & function, parameter1, parameter2]
File, Edit is. Remained item - Help.
X11 has its own system name ( 'help' ) for the Help item. OSX also has a 'apple' menu .
Given this:

I will add screenshots with Linux / OSX later.
First, create a blank for our application:
#! / usr / bin / perl -w use strict; use Tkx; our $ PROGNAME = 'app'; our $ VERSION = '0.1';my $ windowingsystem = Tkx :: tk_windowingsystem (); my $ IS_AQUA = ($ windowingsystem eq 'aqua'); # Given that the names of some buttons may vary, # in OSX this is Control, in Win / X11 it's Ctrl, add a few more variables # (useful for hotkeys) my $ plat_acc_ctrl = ($ ^ O eq 'darwin')? ('Control-'): ('Ctrl +'); my $ plat_evt_ctrl = ($ ^ O eq 'darwin')? ('Control-'): ('Control-'); # Main window. my $ mw = Tkx :: widget-> new ('.'); Tkx :: tk (appname => $ PROGNAME); Tkx :: wm_minsize ($ mw => qw (320,200)); # a new menu is attached using the -menu option $ mw-> configure ( -menu => make_menu ($ mw), ); Tkx :: MainLoop; 1; sub on_quit { Tkx :: tk ___ messageBox (-message => 'Goodbye;]'); exit } sub show_about { Tkx :: tk ___ messageBox (-message => "$ PROGNAME $ VERSION"); } sub make_menu { my $ mw = shift || return # By default, all menus look similar in GIMP, they can be detached # Therefore, turn off this option Tkx :: option_add ('* Menu.tearOff', 0) # ... continued below return $ m; }
Everything is as usual, File, Edit, Help. (cascading menu). A new object is created using the new_menu () method; (like a container)
my $ m = $ mw-> new_menu (); # Top level my $ fm = $ m-> new_menu (); # File my $ em = $ m-> new_menu (); # Edit # Now, add our items to the top level $ m-> add_cascade (-label => 'File', -menu => $ fm, -underline => 0); $ m-> add_cascade (-label => 'Edit', -menu => $ em, -underline => 0);
Adding is done through add_cascade (), where
-label is the menu title.
-menu - widget
-underline - underscore letter index. It is necessary for quick navigation using the Alt button + letter (numbering goes from 0),
we look at what happened.

There is no sense from an empty menu, so we’ll “stuff” it with commands
# File menu $ fm-> add_command ( -label => 'Do something', -underline => 0, -command => sub {}, ); # delimiter $ fm-> add_separator (); $ fm-> add_command ( -label => 'Quit', -underline => 0, -accelerator => "$ {plat_acc_ctrl} Q", -command => [\ & on_quit], ); Tkx :: bind (all => "<$ {plat_evt_ctrl} q>" => [\ & on_quit]); # Edit menu $ em-> add_command ( -label => 'Cut', -underline => 2, -accelerator => "$ {plat_acc_ctrl} X", -command => sub {Tkx :: event_generate (Tkx :: focus (), '<habracut > ')}, ); $ em-> add_command ( -label => 'Copy', -underline => 0, -accelerator => "$ {plat_acc_ctrl} C", -command => sub {Tkx :: event_generate (Tkx :: focus (), '<> ')}, ); $ em-> add_command ( -label => 'Paste', -underline => 0, -accelerator => "$ {plat_acc_ctrl} V", -command => sub {Tkx :: event_generate (Tkx :: focus (), '< > ')}, );
On Quit, we made a hotkey (Ctrl + Q) using the bind method.
To indicate an “accelerator”, -accelerator is used , and when the event is pressed, the -command
command accepts a pointer to a subprogram, and if you need to pass parameters to it, then we use the anonymous array
[\ & function, parameter1, parameter2]
File, Edit is. Remained item - Help.
X11 has its own system name ( 'help' ) for the Help item. OSX also has a 'apple' menu .
Given this:
my $ hname = ($ IS_AQUA? 'nothelp': 'help'); my $ hm = $ m-> new_menu (-name => $ hname); $ m-> add_cascade ( -label => 'Help', -menu => $ hm, -underline => 0, ); # .. add help, homepage, etc. if (! $ IS_AQUA) { $ hm-> add_command ( -label => 'About', -underline => 0, -command => [\ & show_about], ); } else { my $ apple_menu = $ m-> new_menu (-name => 'apple'); $ m-> add_cascade ( -label => $ PROGNAME, -menu => $ apple_menu, ); $ m-> add_command ( -label => "About $ PROGNAME", -command => [\ & show_about], ); }

I will add screenshots with Linux / OSX later.