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:
- install python GObjects;
- build a simple ui with glade (i.e. an xml-based description of the interface);
- write a
valageniepython 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.