Tutorial: How to code with Vala and Glade? Simple!

Today, I’m going to show you how to code a simple and small Glade interface application in Vala using Gtk.builder.

Screenshot from 2013-01-30 05:18:52

Special thanks for my friend Ahmed Shams. He helps me a lot 🙂

Every Glade program consists of two files: the glade UI file and the program (the code) itself.

Glade uses a simple method to connect with the code while Glade uses signals to tell the code that the user interacted with a widget (clicked, hovered, selected etc…)

For example: when we create a button, In glade side we attach a signal to it, ex:”clicked”, in the code side we will use something like “when signal button_clicked received: do something” Got it? 😀

First, we will use Glade to build our simple UI that will be used in our program.

If you haven’t installed glade yet, install it using:

 sudo apt-get install glade

Then run it and start designing your UI, Firstly start with a top-level container.

In our case we will create a simple window that contains one button and a Text-Entry, We will make the button to return (when clicked) the text which typed in the text-entry widget.

Now, Open up glade and create your window:

Create a toplevel Window

Then, Edit the window’s destroy signal from signals tab in widgets properties section, type a name for signal that you can use in the code, in our case we will name our signal “on_window1_destroy”

create the windoe destroy signal

Then, We are going to insert a box (container) to add widgets within it.

create a container for the widgets

Then, We’ll insert our widgets:

insert button and text entry widgets

You can change the properties of a widget through the widget proprieties section at right (change its label etc..)

Then, We are going to edit the “clicked” signal and name it “on_button1_clicked”

Create the click signal

And we’re done! 🙂 now save the glade file in the project’s directory, name it “sample.ui”.

Now your glade file should be something looking like this:

http://paste.ubuntu.com/5574595/

Sorry for that, wordpress doesn’t allow typing XML codes in posts.

Now, it’s the code’s time!, try to understand this code, then save it in the project’s directory as “example.vala” and compile it using:

valac --pkg gtk+-3.0 --pkg gmodule-2.0 example.vala

and here’s our code:

using Gtk;
/* When button click signal received */
public void on_button1_clicked (Button source) {
    /* change button label to clicked! */
    source.label = "Clicked!";
    stderr.printf ("Clicked! --> ");
}

public void on_window1_destroy (Window source) {
    /* When window close signal received */
    Gtk.main_quit ();
}

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

    var builder = new Builder ();
    /* Getting the glade file */
    builder.add_from_file ("sample.ui");
    builder.connect_signals (null);
    var window = builder.get_object ("window1") as Window;
    var entry = builder.get_object ("entry1") as Entry;
    var button = builder.get_object ("button1") as Button;
    /* thats another way to do something when signal received */
    button.clicked.connect (() => {
        stderr.printf ("%s\n", entry.get_text ());
    });
    window.show_all ();
    Gtk.main ();

    return 0;
}

Now you are ready to try your first Vala/Glade app, just run it using

./example

Thanks! Hope this tutorial be helpful for you 🙂

12 thoughts on “Tutorial: How to code with Vala and Glade? Simple!

  1. Hey I was wondering how I could implement an about window, I have it fully designed in Glade, it’s ID is aboutdialog so I added a void that would hook into it but I have been unsuccessful in getting it to work that way, I then tried to create a var like we did in the main loop for the window1 window but I can’t figure that out either, anyway you would be able to help here is the code I have for reference:

    using Gtk;

    public void upload_clicked (Button source) {
    //add program stuff when ui is done!
    }

    public void help_clicked (Button source) {
    /* When window close signal received */
    Gtk.main_quit ();
    }

    //This is the problem, area!
    public void about_clicked (Button source) {
    /* When window close signal received */
    aboutdialog();
    }

    public void on_window1_destroy (Window source) {
    /* When window close signal received */
    Gtk.main_quit ();
    }

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

    try {
    // If the UI contains custom widgets, their types must’ve been instantiated once
    // Type type = typeof(Foo.BarEntry);
    // assert(type != 0);
    var builder = new Builder ();
    builder.add_from_file (“espflasher.ui”);
    builder.connect_signals (null);
    var window = builder.get_object (“window1”) as Window;
    var aboutdialog = builder.get_object (“aboutdialog”) as Gtkaboutdialog; //Tired this aswell…
    window.show_all ();
    Gtk.main ();
    } catch (Error e) {
    stderr.printf (“Could not load UI: %s\n”, e.message);
    return 1;
    }

    return 0;
    }

  2. Pingback: How to code with Python and Glade (reloaded from Vala and Genie) | ockham's razorback

  3. Pingback: How to code with Genie and Glade (2nd part, with class) | ockham's razorback

  4. Pingback: How to code with Genie and Glade (first part) | ockham's razorback

  5. Thank you, thank you, thank you, thank you !!!!
    So little information on vala/genie available and you have just given me EXACTLY what I’ve been looking for.
    This opens a huge vista in which I shall be able to experiment.

    Must encourage you to do more …

    Most grateful to you.

Leave a Reply :)