πŸ“œ ⬆️ ⬇️

Perl and GUI. Widgets

This article will be a continuation of the review widgets graphic library Tk (tkx).

image

We'll consider:
Label
Button
Entry
Checkbutton
Radiobutton
Combobox
Frame
')

ttk :: button
The widget is a normal button, when clicked, an action (command) is performed.

button is in the ttk namespace, so using the Tkx module, the constructor will look like this:
my $ button = $ parent-> new_ttk__button (% args);

The most commonly used arguments are:
-text - the inscription on the button
-command - callback called when pressed (invoke).
-width - length (calculated in characters), usually there is no need to use
-state - the state of the widget (can be: normal, disabled)

Parameter values ​​can be changed at runtime using the configure method:
$ button-> configure (% args)

A small example:
Place on the form two buttons (first active, second inactive), when you press one, the other is activated.

image

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

 my $ mw = Tkx :: widget-> new ('.');
    $ mw-> g_wm_minsize (250,100);

 my $ button_1 = $ mw-> new_ttk__button (-text => 'Button 1');
 my $ button_2 = $ mw-> new_ttk__button (-text => 'Button 2', -state => 'disabled');

 $ button_1-> configure (
     -command => sub {
         $ button_1-> configure (-state => 'disabled');
         $ button_2-> configure (-state => 'normal');
     },
 );

 $ button_2-> configure (
     -command => sub {
         $ button_2-> configure (-state => 'disabled');
         $ button_1-> configure (-state => 'normal');
     },
 );

 Tkx :: grid ($ button_1, -row => 0, -column => 0, -padx => 10, -pady => 10);
 Tkx :: grid ($ button_2, -row => 0, -column => 1, -padx => 10, -pady => 10);

 Tkx :: MainLoop ();


ttk :: label
The usual inscription, you can use different fonts.

The text can be set in two ways, either through the option -text, or -textvariable (pointer to a variable).
my $ label = $ parent-> new_ttk__label (-text => 'Static text');
or
my $ textval = 'something';
my $ label = $ parent-> new_ttk__label (-textvariable => \ $ textval);

Options:
-text - text.
-textvariable - a variable that stores the text of the label
-font, -background, -foreground β€” font, background colors and captions.
-justify - text alignment, set like west-east, south-west (read more in Perl and Tkx. Packaging of widgets).
-relief - relief lettering (flat, groove, raised, ridge, solid, sunken)
-padding, -wraplength - set the indents at the edges, and the maximum length of the line in pixels (after which the transfer follows);

Let's embellish our first example:
image

ttk :: entry
Text box

image

Standard arguments:
-textvariable - a variable in which the value of the field will be stored
-state - state (normal, disabled, readonly)
-justify - alignment (left, center, right)

-validate, -validatecommand, -invalidcommand - allow you to use input validation.

When there are many such widgets, it is convenient for textvariable to use a hash (for example, text-variable => \ $ UI {login}).

ttk :: checkbutton
A button that has several states. The state is written to -variable.
When pressed, like ttk :: button, a procedure can be called.

image

Arguments
-text, -textvariable - set the label on the button
-onvalue, -offvalue - values ​​when the option is checked or not. The default is 1 and 0.
-variable - the variable in which the value is written.
-command - the action performed when you click.

 my $ checkbutton = $ mw-> new_ttk__checkbutton (
     -text => 'enable bla-bla feature',
     -variable => \ $ UI {enable_blabla},
     -command => sub {
         # Updating other widgets
     },
 );


ttk :: radiobutton
image

The device is as follows: each radiobutton is assigned a unique value, which is written to the variable
When it is necessary to combine, let's say two buttons in one group, then we set the values ​​for each of them, and the -variable will be common.

 my $ radiobutton_1 = $ mw-> new_ttk__radiobutton (-text => 'Option 1', -value => 1, -variable => \ $ UI {radio});
 my $ radiobutton_2 = $ mw-> new_ttk__radiobutton (-text => 'Option 2', -value => 2, -variable => \ $ UI {radio});


Options:
-text, -textvariable, -width, -underline, -style etc. For more information, see the Tcl / Tk documentation.

ttk :: combobox
Input box with drop-down list. When the state is readonly, it works only as a list.

image

Options:
-textvariable - a variable in which the value of the text field will be stored.
-state - state (normal, readonly, disabled)
-values ​​- anonymous array, list.
-postcommand - the command that is executed after selecting an item from the list.
-justify, -height, -width - widget geometry.

ttk :: frame
When there are many objects on the form, it is convenient to group them using frames.
for example
my $ frame = $ mw-> new_ttk__frame ();
my $ button = $ frame-> new_ttk_button ();
...
One frame can be in another frame and so on ...

Arguments are:
-borderwidth, -relief - thickness of the border, flat relief, groove, raised, ridge, solid, sunken)
-width, -height - length, width
-padding - indent around the edges.

ttk :: labelframe
This is the same frame with header only (-text)

image

ttk :: progressbar
Progressbar, you can set a range of values ​​(by default 0..100)

image

Options:
-orient - orientation (horizontal and vertical)
-length - length in pixels
-maximum - maximum value (100)
-value - current value
-variable - the variable storing the value.

Previous posts:
Menu operation
Packaging Widgets
Tkx and streams

Literature:
Tcl8.5.7 / Tk8.5.7 Documentation

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


All Articles