First steps [Vala + Gtk 3]: Hello World !!

We are going to see in this little tutorial how to take our first steps with Vala and GTK3. Let's get started:

Installing the necessary tools

sudo apt-get install valac libgtk-3-dev

The IDE:

We can find a great variety of editors which we can use / configure as IDE. Example of them are Scratch, SublimeText, geany … In my case I'm going to use Scratch (which we can find in elementary os).

Scratch

Let's see the typical example of Hello World!, Which would be as follows:

Capture from 2013-10-31 23:33:12

And the code would look like this:

Capture from 2013-10-31 23:34:48

Now let's see the details of the code. We have a class which is a Gtk window.

# We initialize Gtk with arguments Gtk.init (ref args); # We create the application. Application app = new Application (); # We put the application in the center of the screen. app.window_position = Gtk.WindowPosition.CENTER; # When closing we destroy the app. app.destroy.connect (Gtk.main_quit); # We measure the window app.set_default_size (100, 50); # We create a button and connect the button click event and print a hello! var button = new Gtk.Button.with_label ("Say Hello"); button.clicked.connect (() => {print ("Hello! \ n");}); # Add the button to the app app.add (button); # We show the App / Window app.show_all ();

By clicking on the button we will get as output:

Capture from 2013-10-31 23:35:58

We compile and execute:

$ valac -v lol.vala --pkg gtk + -3.0 $ ./lol

Now I leave you some links of interest:

http://elementaryos.org/docs/code/the-basic-setup


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   pandev92 said

    The problem with not having an ide that draws windows is that you must know all the methods of the class to create a window, something primitive, really.

    1.    nuanced said

      If you did a good job in the design stage, you would have no reason to use an IDE when programming.

      1.    pandev92 said

        You did not understand me, I am talking about having an idea that will do it for you, the windows, without having to type code in principle:

        http://imagebin.org/275532

        That saves you from knowing what is the method of a button, etc etc

        1.    marianogaudix said

          With an IDE you only save time …… But if we talk about functionality when using an IDE or a TERMINAL when compiling, they both fulfill the same functions.
          It is a matter of taste and comfort.

        2.    artus said

          Well you have Glade (https://glade.gnome.org/), is a RAD tool that allows you to quickly design application windows with simple mouse clicks.

          Once the interface is created, it generates a file in xml format that you can invoke from programming languages ​​such as C, C ++, Python, Perl, Vala, Java, etc.

          I hope this answers your question.

        3.    lolbimbo said

          If there is an idea that integrates a window creator, Anjuta IDE.

  2.   Juan Pablo said

    Hello Lolbimbo:
    Some time ago I decided to use Vala as my main language before D and Golang for x reasons, but I am stuck in a problem, the code I have compiled very well, but now I get the following error:

    /media/…/vala/nn.vala.c: In function 'main':
    /media/…/vala/nn.vala.c:155:2: warning: 'g_type_init' is deprecated (declared at /usr/include/glib-2.0/gobject/gtype.h:669) [-Wdeprecated-declarations]
    g_type_init();

    if it's too much trouble I'll understand, thanks in advance and congratulations on the blog.

    1.    lolbimbo said

      Hello friend, I'm starting with vala, but pass me the code you have on line 155, anyway it is a warning, not an error, if it works for you, nothing happens since it may be a vala bug.

      1.    Juan Pablo said

        Hello Lolimbo, I celebrate your enthusiasm since I felt lonely in vala, actually the error gives me in any code as simple as: using Gtk;

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

        var window = newWindow();
        window.title = "First GTK + Program";
        window.border_width = 10;
        window.window_position = WindowPosition.CENTER;
        window.set_default_size(350, 70);
        window.destroy.connect(Gtk.main_quit);

        var button = new Button.with_label ("Click me!");
        button.clicked.connect (() => {
        button.label = "Thank you";
        });
        try {
        // Either directly from a file ...
        window.icon = new Gdk.Pixbuf.from_file ("my-app.png");
        //… or from the theme
        window.icon = IconTheme.get_default () .load_icon ("my-app", 48, 0);
        } catch (Error e) {
        stderr.printf ("Could not load application icon:% s \ n", e.message);
        }
        window.add(button);
        window.show_all();

        Gtk.main();
        0 return;
        }

        But between working with Windows and at home I don't have time to hunt down the error, it seems to me that it is a problem with versions of vala, I don't know.

        1.    lolbimbo said

          Updating vala should remove the warning.

  3.   uribes said

    The tutorial is well explained and I like it, just that I have always wondered, why always start with the "hello world" program, for beginners it would be fine, but what of those who already have programming experience? Why not indicate how to make an interface with several elements (buttons, labels, etc)?

    I like Vala and I want to learn it, but it would be more constructive to start a little more "in between", right?

    1.    lolbimbo said

      Soon are another post with an intermediate level.

  4.   juanra20 said

    Java?

    Nice tutorial well explained and it all actually made me consider putting vala and gtk on my to-learn list.

  5.   -ik- said

    Excellent, now I suggest a Seed (javascript) + Gtk tutorial, it would be quite interesting, as this is the official option that the Gnome project has selected.