lunedì 27 agosto 2012

Esempio GUI con PyGTK/Glade

In questa versione dell'esempio di GUI l'interfaccia non e' stata creata da codice bensi' mediante il GUI Designer di GTK ovvero Glade
Per iniziare a creare l'interfaccia deve essere prima aggiunto una form (ovvero un TopLevel) a cui deve essere aggiunta una Box (in questo caso un VBox) per impacchettare i Widgets e successivamente si aggiungono i Widgets
Il nome della finestra di TopLevel e' stato definito come MainWindow (nome che servira' per la referenziazione)


In Properties/Signal si puo' definire l'evento che si vuole catturare

Il salvataggio dell'interfaccia puo' avvenire in due formati (GtkBuilder e Libglade). Nell'esempio riportato di seguito e' stato scelto LibGlade

Il programma in Python...il codice e' abbastanza autoesplicativo
---------------------------------------------------
 import sys
try:
    import pygtk
    pygtk.require("2.0")
except:
    pass
try:
    import gtk
    import gtk.glade
except:
    sys.exit(1)


class ProgressBar:

    def __init__(self):


      
        self.gladefile = "progressbar.glade"
        self.wTree = gtk.glade.XML(self.gladefile)
        self.window = self.wTree.get_widget("MainWindow")
        self.window.show()
        if (self.window):
            self.window.connect("destroy", gtk.main_quit)

        dic = { "on_hscale1_value_changed" : self.cambia_valore}
        self.wTree.signal_autoconnect(dic)
      
    def cambia_valore(self, widget):
        a = self.wTree.get_widget("hscale1").get_value()
        self.wTree.get_widget("label1").set_text(str(int(a)))
        self.wTree.get_widget("progressbar1").set_fraction(a/100)


      
if __name__ == "__main__":
    hwg = ProgressBar()
    gtk.main()

---------------------------------------------------
progressbar.glade
---------------------------------------------------
 <?xml version="1.0" encoding="UTF-8"?>
<glade-interface>
  <!-- interface-requires gtk+ 2.24 -->
  <!-- interface-naming-policy project-wide -->
  <widget class="GtkWindow" id="MainWindow">
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Progress Bar</property>
    <property name="default_width">250</property>
    <property name="default_height">80</property>
    <child>
      <widget class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <widget class="GtkHScale" id="hscale1">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="adjustment">0 0 110 1 1 10</property>
            <property name="lower_stepper_sensitivity">off</property>
            <property name="upper_stepper_sensitivity">off</property>
            <property name="restrict_to_fill_level">False</property>
            <property name="round_digits">1</property>
            <property name="digits">0</property>
            <property name="draw_value">False</property>
            <signal name="change_value" handler="on_hscale1_change_value" swapped="no"/>
            <signal name="value_changed" handler="on_hscale1_value_changed" swapped="no"/>
          </widget>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <widget class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">0</property>
          </widget>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <widget class="GtkProgressBar" id="progressbar1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
          </widget>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
      </widget>
    </child>
  </widget>
</glade-interface>

---------------------------------------------------