Creating GUI Application using Gtk & Vala

Quick Read : Creating a GUI application using Gtk & Vala.

What is GTK ?

GTK+, or the GIMP Toolkit, is a multi-platform toolkit for creating graphical user interfaces. Offering a complete set of widgets, GTK+ is suitable for projects ranging from small one-off tools to complete application suites.

[myinlink sub=”How to Install Vala Compiler in Ubuntu” link=””]Click Here to Read[/myinlink]

In the Previous article, we saw about installing vala in our Ubuntu System. So now we have to do something quite useful for daily usage application. We use our laptop/PC for a long time in a day. Our system probably uses caches to speed up the process & operation time. PC/Laptop also saves the Recently visited folders as Recent Documents.

Those Recent Document folder Contains recently visited folders. Those folders will use very large number of space in your system root folder. So we have to clear those folders in the Correct interval of time to get high working experience.

gui application using Gtk vala


Now we’re going to make a Simple GUI Application using Gtk & Vala to clear Recent Documents in our PC.

To Open Terminal :

In Ubuntu:

Press [keybt]Ctrl[/keybt] + [keybt]Alt[/keybt] + [keybt]T[/keybt]

In Elementary OS:

Press [keybt]WinKey[/keybt] + [keybt]T[/keybt]

To Create Application:
Paste this code in Terminal & Hit Enter
gedit guiapp.vala

It’ll Open a Gedit with a file name guiapp.vala

gui application using Gtk vala

Now paste the Below Code in that gedit file.

using Gtk;

class MainWindow : Window  {

  public static int main (string[] args) 
    Gtk.init(ref args);  //Gtk intialization

    var window = new MainWindow (); //Create a window
    window.destroy.connect (Gtk.main_quit); //Quit app after window is closed
    window.show_all (); //Makes all widgets visible

    Gtk.main(); //Start the main loop

    return 0;

  public MainWindow()
    this.title = "My First App";
    this.border_width = 10;
    this.set_default_size (250, 60);
    this.window_position = WindowPosition.CENTER;
    this.destroy.connect(Gtk.main_quit); //Quit app when window is closed

    var btnClear = new Button.with_label ("Clear Recent Documents");
    btnClear.clicked.connect (clear_history); //Connect event with function
    add(btnClear);  //Add button to window

  public void clear_history () 
    string HOME = Environment.get_home_dir ();

    try {
      Process.spawn_command_line_sync (@"rm $HOME/.local/share/recently-used.xbel");
      Process.spawn_command_line_sync (@"touch $HOME/.local/share/recently-used.xbel");
      messagebox_show ("Success", "Your recent documents history has been cleared");
    catch (Error e){
      string msg = e.message;
      messagebox_show ("Error", msg);

  public void messagebox_show(string title, string message)
    var dialog = new Gtk.MessageDialog(

Click File -> Save or Press [keybt]Ctrl[/keybt] + [keybt]S[/keybt] to Save. Then Close Gedit

gui application using Gtk vala
Screenshot Captured using Shutter
[myinlink sub=”How to Install Shutter in Ubuntu/Elementary OS” link=””]Click Here to Read[/myinlink]
To Compile :

Open terminal and paste the below code then press enter

valac --pkg gtk+-3.0 "guiapp.vala" -o guiapp
gui application using Gtk vala
To Run the Program :

Run this command line in the terminal to run the program.


gui application using Gtk vala

That’s all you have successfully Created your first GUI Application.
gui application using Gtk vala