Aquests dies he estat jugant amb el Python. Ja feia mesos que volia posar-m’hi però no tenia gents de temps. Si es el llenguatge preferit de Google serà per alguna cosa (Això ha sonat força sensacionalista). A part també me l’havia recomanat força l’Oriol Rius.
De passada també he conegut el famós Glade, que també m’ha agradat força.
En aquest exemple podem veure un “Hello World” en text i un de gràfic. A més de veure com es defineixen les classes en Python. Te pinta de ser un llenguatge molt àgil i portable. Ideal per fer scripts i petites aplicacions per coses molt concretes.
import pygtk
import gtk
from gtk import glade
class p1:
def __init__(self):
print(“Classe Instanciada”)
return
def helloText(self):
print(“HelloWorld”)
return
def helloGrafic(self):
d1= glade.XML( “hello.glade”, “HelloWorld” )
window=d1.get_widget(“HelloWorld”)
signals = {
“on_button1_clicked” : self.quit
}
d1.signal_autoconnect(signals)
window.show()
gtk.main()
return
def quit(self,w):
print “tancar”
gtk.main_quit()
return
p = p1()
p.helloText()
p.helloGrafic()
L’exemple crida el fitxer hello.glade que l’he generat amb l’aplicació Glade
<!DOCTYPE glade-interface SYSTEM “glade-2.0.dtd”>
<!–Generated with glade3 3.0.2 on Tue Apr 3 18:22:32 2007 by [email protected]–>
<glade-interface>
<widget class=”GtkWindow” id=”HelloWorld”>
<property name=”default_width”>475</property>
<property name=”default_height”>250</property>
<property name=”icon”>.</property>
<child>
<widget class=”GtkLayout” id=”layout1″>
<property name=”visible”>True</property>
<property name=”width”>0</property>
<property name=”height”>0</property>
<child>
<widget class=”GtkLabel” id=”label1″>
<property name=”width_request”>177</property>
<property name=”height_request”>28</property>
<property name=”visible”>True</property>
<property name=”label” translatable=”yes”>HelloWorld</property>
</widget>
<packing>
<property name=”x”>121</property>
<property name=”y”>69</property>
</packing>
</child>
<child>
<widget class=”GtkButton” id=”button1″>
<property name=”width_request”>129</property>
<property name=”height_request”>38</property>
<property name=”visible”>True</property>
<property name=”label” translatable=”yes”>Tencar</property>
<signal name=”clicked” handler=”on_button1_clicked”/>
<signal name=”activate” handler=”on_button1_activate”/>
</widget>
<packing>
<property name=”x”>144</property>
<property name=”y”>147</property>
</packing>
</child>
</widget>
</child>
</widget>
</glade-interface>