Exemplo usando Python e GTK


Definição da Interface


glade-helloworld.png

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
<!--Generated with glade3 3.4.3 on Wed Oct 29 09:19:42 2008 -->
<glade-interface>
  <widget class="GtkWindow" id="window1">
    <property name="default_width">200</property>
    <property name="default_height">200</property>
    <signal name="delete_event" handler="sai"/>
    <child>
      <widget class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <child>
          <widget class="GtkComboBox" id="combobox1">
            <property name="visible">True</property>
            <property name="items" translatable="yes">Normal
Big</property>
            <signal name="changed" handler="muda"/>
          </widget>
        </child>
        <child>
          <widget class="GtkLabel" id="label1">
            <property name="visible">True</property>
          </widget>
          <packing>
            <property name="position">1</property>
          </packing>
        </child>
      </widget>
    </child>
  </widget>
</glade-interface>

Código Python


python-helloworld.png

#!/usr/bin/env python
#File testPyGTK.py by Beco, 29/102008
 
import sys
#try to import correct versions of GTK libraries
try:
     import pygtk
      pygtk.require("2.0")
except:
      pass
try:
    import gtk, gtk.glade
except:
    sys.exit(1)
 
 
class helloWorld:
    """Hellow World using Python and GTK"""
 
    def __init__(self):
 
        #Input Glade XML file
        self.wTree = gtk.glade.XML('comboGTK.glade')
        #Dictionary of Signals to autoconnect
        dicSignal = {    "muda" : self.muda,
                        "sai" : self.sai }
        self.wTree.signal_autoconnect(dicSignal)
 
        #Import necessary widget
        self.janela1 = self.wTree.get_widget('window1')
        self.rotulo1 = self.wTree.get_widget('label1')
        self.combo1 = self.wTree.get_widget('combobox1')
        self.combo1.set_active(0)
        self.janela1.show_all()
 
    def muda(self, widget):
        if widget.get_active_text() == 'Normal':
            self.rotulo1.set_markup('Normal Hello World.')
        elif widget.get_active_text() == 'Big':
            self.rotulo1.set_markup('<big>Big Hello World.</big>')
 
    def sai(self, widget, data):
        gtk.main_quit()
 
#Start block
if __name__ == "__main__":
    app = helloWorld()
    gtk.main()