How to code with Python and Glade (reloaded from Vala and Genie)

this post is a rather dumb translation, in python, of another dumb translation, for the genie language, of a tutorial written for his cousin language, vala.

the aim of the original tutorial (click here to read it) was to show how to use a glade user interface from a vala program. for this one the ideas are:

  1. install python GObjects;
  2. build a simple ui with glade (i.e. an xml-based description of the interface);
  3. write a vala genie python program to use this interface and perform some useful task (in this case, write a message to the standard error file descriptor using a gui widget value);

Install python GObjects is simple (on OSX with port, at least)

sudo port install py34-gobject3

The UI is the same from here (and save it to a file named sample.ui)

The program is simply a direct conversion

#! /usr/bin/env python3.4

import sys
import gi

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class Handler:
    def on_window1_destroy(self, *args):
        Gtk.main_quit(*args)

    def on_button1_clicked(self, button):
        button.label = "Clicked!"
        sys.stderr.write("Clicked! ---> ")

class MainWindow:

    entry = None

    def spool_text(self, widget):
        sys.stderr.write("%s\n" % (self.entry.get_text(),))

    def __init__(self):
        builder = Gtk.Builder()
    try:
        builder.add_from_file("sample.ui")
    except IOError as e:
        print("Error:", e)
        sys.exit(-1)

    builder.connect_signals(Handler())
    window = builder.get_object("window1")
    self.entry = builder.get_object ("entry1")
    button = builder.get_object ("button1")

    button.connect("clicked", self.spool_text)

    window.show_all()

if __name__ == "__main__":
    mw = MainWindow()
    Gtk.main()

It use the same class variable trick to do the job to print the entry text.

Thanks for your attention, have a nice day.

How to code with Genie and Glade (2nd part, with class)

this post is a rather dumb translation, for the genie language, of a tutorial written for his cousin language, vala.

the aim of the original tutorial (click here to read it) was to show how to use a glade user interface from a vala program. the ideas were:

  1. install vala and glade;
  2. build a simple ui with glade (i.e. an xml-based description of the interface);
  3. write a vala genie program to use this interface and perform some useful task (in this case, write a message to the standard error file descriptor using a gui widget value);
  4. build and run

we have discussed this case in another post: the problem there is that building the program

valac --pkg gtk+-3.0  sample_glade_gtk.gs 

sample_glade_gtk.gs:35.5-38.5: warning: deprecated syntax, use `connect' 
     method instead

Compilation succeeded - 1 warning(s)

we took a warning that advise us we are using a deprecated syntax (anonymous function) for the instruction

        button.clicked += def(button)
        stderr.printf ("%s\n", entry.get_text())

To use the connect method on a Button.clicked event… it seems simple but this is simply a button callback: it does not see the Entry widget, so it does not know what is the object we would like to get_text().

Solution? Not so simple, we need to rewrite the sample in a OOP fashion, to have a context where we can see the Entry widget get_text() value.

[indent=4]

uses
    Gtk

def on_button1_clicked (source : Button) : void
    source.label = "Clicked!"
    stderr.printf ("Clicked! --> ")

def on_window1_destroy (source: Window) : void
    /* When window close signal received */
    Gtk.main_quit()

init
    Gtk.init (ref args)
    new MainWindow ()
    Gtk.main ()

// main window definition

class MainWindow : Window

    // this class variable allow us to use its get_text() value
    entry : Entry

    def spool_text()
        stderr.printf ("%s\n", entry.get_text())

    // class constructor
    init

        var builder = new Builder()

        /* Getting the glade file */
        try
            builder.add_from_file ("sample.ui")
        except ex : GLib.Error
            print "Error: %s", ex.message
            Process.exit(-1)

        builder.connect_signals (null);

        var main_window = builder.get_object ("window1") as Window
        // "entry" Entry was already defined at class level
        entry = builder.get_object ("entry1") as Entry
        var button = builder.get_object ("button1") as Button

        /* connect the spool_text callback to the button's event */
        button.clicked.connect(spool_text)

        /* show the window */
        main_window.show_all ()

of course this tutorial is not intended to teach OOP, the callback on_button1_clicked outside of the scope of the MainWindow class but still related to its widget should be considered a bad programming sample.

Thanks for your attention, have a nice day.

How to code with Genie and Glade (first part)

this post is a rather dumb translation, for the genie language, of a tutorial written for his cousin language, vala.

the aim of the original tutorial (click here to read it) is to show how to use a glade user interface from a vala program. the ideas are:

  1. install vala and glade;
  2. build a simple ui with glade (i.e. an xml-based description of the interface);
  3. write a vala genie program to use this interface and perform some useful tasks (in this case, write a message to the standard error file descriptor);
  4. build and run

 

Very nice, and simple: let’s try to do the same with genie

the installation: on my OSX box (with port as package manager)

sudo port install vala glade

since vala and genie languages share the same translator to C, valac, no need for further installations.

the ui: let me shamelessly steal it  from here (and save it to a file named sample.ui)

the program: it is the one we have to rewrite in genie from vala. a breeze

[indent=4]

uses
    Gtk

def on_button1_clicked (source : Button) : void
    source.label = "Clicked!"
    stderr.printf ("Clicked! --> ")

def on_window1_destroy (source: Window) : void
    /* When window close signal received */
    Gtk.main_quit()

init

    Gtk.init (ref args)
    var builder = new Builder()

    /* Getting the glade file */
    try
        builder.add_from_file ("sample.ui")
    except ex : GLib.Error
        print "Error: %s", ex.message
        Process.exit(-1)

    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

    /* another way to do something when signal received */
    /* this will give a warning for deprecated syntax *|
    button.clicked += def(button)
        stderr.printf ("%s\n", entry.get_text())

    window.show_all ()
    Gtk.main ()

For a little improvement, I’ve added a trivial try…except block to control the ui file exists or exits the program.

finally build the program (save it to sample_glade_gtk.gs file)

valac --pkg gtk+-3.0  sample_glade_gtk.gs 

sample_glade_gtk.gs:35.5-38.5: warning: deprecated syntax, use `connect' 
     method instead

Compilation succeeded - 1 warning(s)

The program actually works: the residual problem we have is the warning that advise us we are using a deprecated syntax (anonymous function) for the instruction

    button.clicked += def(button)
        stderr.printf ("%s\n", entry.get_text())

 

Next time, we’ll see that to use the button.clicked.connect(some_callback) as suggested we’ll have to rewrite our sample in a OOP fashion.

 

Thanks for your attention, have a nice day.