Back to Home

Perl and GUI. Widgets

perl · tkx · gui · tcl

Perl and GUI. Widgets

    This article will continue the review of widgets of the Tk (tkx) graphics library.

    image

    We will consider:
    Label
    Button
    Entry
    CheckButton
    RadioButton
    ComboBox
    Frame


    ttk :: button The
    widget is a regular button, when clicked, some command action 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 - the callback called when pressed (invoke).
    -width - length (calculated in characters), usually there is no need to use
    -state - state of the widget (it can be: normal, disabled)

    Parameter values ​​can be changed during program execution using the configure method:
    $ button-> configure (% args)

    A small example:
    Place two buttons on the form (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
    Regular label, you can use different fonts.

    Text can be specified in two ways, either through the -text option, 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 inscription
    -font, -background, -foreground - font, background color and text.
    -justify - text alignment, set like west-east, south-west (read more in Perl and Tkx. Widget packaging).
    -relief - label relief (flat, groove, raised, ridge, solid, sunken)
    -padding, -wraplength - set margins on the edges, and the maximum length of the line in pixels (followed by hyphenation);

    Let's embellish our first example:
    image

    ttk :: entry
    Text input field

    image

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

    - validate, -validatecommand, -invalidcommand - allow you to use validation of entered data.

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

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

    image

    The arguments
    -text, -textvariable - specify the label on the
    -onvalue button , -offvalue - values ​​when the option is checked or not. By default, these are 1 and 0.
    -variable is the variable in which the value is written.
    -command - action performed when clicked.

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


    ttk :: radiobutton
    image

    The device is as follows: each radiobutton is assigned a unique value (value), which is written into a variable (variable)
    When you need to combine, let's say two buttons in one group, we set the values ​​for each of them, and -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 details, see the Tcl / Tk

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

    image

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

    ttk :: frame
    When there are a lot of 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 ... The

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

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

    image

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

    image

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

    Previous Posts:
    Working with the menu Tkx
    widget
    packaging and streams

    References:
    Tcl8.5.7 / Tk8.5.7 Documentation

    Read Next