Creating a Vala + Gtk3 app. [1st part]

After first post I read a comment that it would be better to start with something more complex than "hello world", then I will make a sequence3 or 4) post creating a simple application (with vala + gtk 3).

The application will consist of a simple multiple choice question and answer game (Trivia type), in which answering 3 questions wrongly ends (game over), and the objective is to answer as many questions as possible, for each question you have a limited time to reply.

Integrated

Main design of our application will be:

app

Later we will put some buttons that will give us the option of 50% (eliminating two wrong answers), freeze time, pass a question. All of them can only be used once, being disabled once used.

Design a Custom code
As we can see in the design we can see the gtk elements that we will use:

Answers -> Button.

Question -> Label.

Time -> ProgressBar.

Wrong / correct points and Questions -> Label.

We can see that we have a vertical structure therefore we can use GBox vertically.

Custom code

int main (string [] args) {Gtk.init (ref args); var window = new Gtk.Window (); window.title = "app"; window.window_position = Gtk.WindowPosition.CENTER; window.set_default_size (300, 340); window.destroy.connect (Gtk.main_quit); window.set_border_width (10); // vertical box var box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0); box.set_spacing (10); // Label for the question var question = new Gtk.Label ("Question?"); // prograsive time bar var time_bar = new Gtk.ProgressBar (); time_bar.set_text ("Time"); time_bar.set_show_text (true); // Response buttons var response1 = new Gtk.Button.with_label ("Response 1"); var resposta2 = new Gtk.Button.with_label ("Reply 2"); var resposta3 = new Gtk.Button.with_label ("Reply 3"); var resposta4 = new Gtk.Button.with_label ("Reply 4"); // labels info var points = new Gtk.Label ("Points: 0"); box.pack_start (question); box.pack_start (time_bar); box.pack_start (reply1); box.pack_start (resposta2); box.pack_start (resposta3); box.pack_start (resposta4); box.pack_start (points); window.add (box); window.show_all (); Gtk.main (); return 0;}

Capture from 2013-11-02 21:44:14

To move the "time" we use GLib.Timeout where every 500 milliseconds it will be activated (it is a loop where the variable that contains the value of our bar will increase)

GLib.Timeout.add (500, () => {// Get the current progress: // (0.0 -> 0%; 1.0 -> 100%) double progress = time_bar.get_fraction (); // Update the bar: progress = progress + 0.01; time_bar.set_fraction (progress); // Repeat until 100% return progress <1.0;});

Links of interest 
http://www.valadoc.org/#!wiki=index (you can find all gtk elements with their methods ...)


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.   lozanotux said

    Very good, I will ask you how I can make a kind of TABS but vertically, although they do not necessarily have to be tabs, they can be buttons or ToggleButtons, but I need the behavior of a tab, that when touching 1 its content is seen and The other tab is disabled, I do not know if I explain myself, I leave an image to see if you can give me some information to investigate. Thank you!

    IMAGE: http://i.imm.io/1jURw.png

    1.    lolbimbo said

      The "layout" or the text change (for example)?

  2.   pandev92 said

    Catalan parla

  3.   stunned said

    This is very good in fact it is something similar to what I was looking for, a question, could this question be linked to the terminal in any way?
    For example: To start I take the time to answer I leave it in unlimited and that would be what I would like to do.

    What desktop environment do you want to install?
    KDE
    Gnome 3
    Cinnamon
    Unity

    And each of these that was linked to its respective installation command in the terminal, sudo apt-get install ubuntu-desktop

    1.    lolbimbo said

      Yes, you would just have to write Process.spawn_command_line_async ("apt-get install ubuntu-desktop");

      http://valadoc.org/#!api=glib-2.0/GLib.Process.spawn_command_line_sync

  4.   Saúl Uribe said

    Excellent, I like the post, I will be practicing (and I see to get ahead), greetings