📜 ⬆️ ⬇️

Perl and GUI. Packaging Widgets

Before we consider the basic controls of the Tk library, you need to learn how to package widgets on a form.

There are several packers, it is pack, place, grid. Each of them is suitable for certain tasks, but the grid is the most efficient.

The grid is a table, and all widgets are located in its cells. Position is indicated by row and column, respectively, row and column.
')
you can call directly via Tkx: Tkx::grid( $, ... )
as well as through the parent (they can be any widget, usually a window or frame)
$->g_grid( ... )

One table can be nested in another, depending on the hierarchy.


Arguments are a hash array.
A widget does not have to be in only one cell; it can occupy both several rows and several columns (rowspan, columnspan). In addition to all this, we can set indents (both internal and external), as well as the “stickiness” settings, that is, stretching the widget in the right direction relative to the edges of the cell.

Row / column indices are numbered from 0..n

Consider an example:
 #! / usr / bin / perl
 use strict;
 use Tkx;

 my $ mw = Tkx :: widget-> new ('.');
 my $ b1 = $ mw-> new_ttk__button (-text => 'button 1');
 my $ b2 = $ mw-> new_ttk__button (-text => 'button 2');
 my $ b3 = $ mw-> new_ttk__button (-text => 'button 3');

 $ b1-> g_grid (-row => 0, -column => 0);
 $ b2-> g_grid (-row => 0, -column => 1);
 $ b3-> g_grid (-row => 1, -column => 1);

 Tkx :: MainLoop ();


image

Now let's try extending button 3 to two cells.
  $ b3-> g_grid (-row => 1, -column => 0, -columnspan => 2); 


image

"Stickiness"



All is well, but we wanted the button to occupy two cells, rather than being centered. To do this, we give it stickiness.

The following directions are used to indicate sticky:
image
stretching along all edges - "nsew"
north - n orth
South - s outh
east - e ast
west - w est

We need it to stretch along the left and right, then sticky - east-west, "ew"

  $ b3-> g_grid (-row => 1, -column => 0, -columnspan => 2, -sticky => "ew"); 


image

Now we will indent between the buttons. This can be done using the following options.
padx - indents on X axis
pady - Y-padding
ipadx - indent on the X axis (internal)
ipady - Y-offset (internal)


 $ b1-> g_grid (-row => 0, -column => 0, -padx => q / 10 5 /, -pady => q / 10 5 /);
 $ b2-> g_grid (-row => 0, -column => 1, -padx => q / 5 10 /);
 $ b3-> g_grid (-row => 1, -column => 0, -columnspan => 2, -sticky => "ew", -ipady => 10);


We get just such a picture:
image

Button 3 is expanded on Y, because we set the ipady inner space, and sticky requires stretching along all edges.

Resizing a widget when stretching a window.


grid is convenient in that it allows you to immediately set the parameters for a single row or column
grid_rowconfigure( , ... ) - for row
grid_columnconfigure( , ...) for column

Arguments:
minsize - the minimum allowable size
weight - weight

weight plays an important role, indicates in what proportion widgets should be when changing the size of the parent window (table).

Let's return to the first example:
image

And now, let's set the stickiness for the “new” buttons north-east-west, and the weight for column 0 = 1, column 1 = 1,
that is, when stretching, the ratio in the size along the X axis between two columns was 1: 1
 $ mw-> g_grid_columnconfigure (0, -weight => 1);
 $ mw-> g_grid_columnconfigure (1, -weight => 1);


image

If instead of -weight => 1 specify 2, then it will look like this: (2: 1)

image

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


All Articles