E
E
Eugene2020-07-17 18:10:02
GTK+
Eugene, 2020-07-17 18:10:02

Get GtkLabel by its ID?

As I understood from the manuals, to change the text, you need to use the gtk_label_set_text function. But then a problem arose, she needed to pass this same GtkLabel, and it was with these that I had difficulties.
In the window creation function itself, using the builder, I load the xml layout of the window and fasten the necessary button handlers to it, I can immediately change the text of these same buttons and other fields without any problems.
But what if you need to change the text from the function that was called by this handler? After all, there are no longer those variables. Re-creating the builder, loading the layout and everything else - it doesn't sound very reasonable to change one single line, but how to get the element by its ID in other ways - I unfortunately can't find any information.

PS Yes, I heard that gtkmm is more suitable for C ++, but it so happened that I had to work with gtk :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tony, 2021-03-18
@TonyHunt

Use variables to work with data. A variable can be used almost anywhere in a program, subroutine.
Example in 4 languages.
6053a84e9a5cc869182286.png
Example with Gtk.Builder.

using Gtk;

public void on_button1_clicked (Button source) {
    source.label = "Thank you!";
}

public void on_button2_clicked (Button source) {
    source.label = "Thanks!";
}

int main (string[] args) {
    Gtk.init (ref args);

    try {
        // Если UI содержит кастомные виджеты,они должны хотя бы один раз проинициализированы
        // Type type = typeof(Foo.BarEntry);
        // assert(type != 0);
        var builder = new Builder ();
        builder.add_from_file ("sample.ui");
        builder.connect_signals (null);
        var window = builder.get_object ("window") as Window;
        window.show_all ();
        Gtk.main ();
    } catch (Error e) {
        stderr.printf ("Не получилось загрузить UI: %s\n", e.message);
        return 1;
    }

    return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question