Weather TKinter es un scrpit, que usa TKinter, que imprime en un cuadro de texto datos meteorológicos del código postal que pongas (En España). Utilizo Google Weather API, y no uso el módulo de XML de python, porque cuando escribí el programa, ni sabía que existía (Por eso me ha quedado poco claro el código )
Código
#!/usr/bin/env python # -*- coding: utf-8 -*- # By Sergio López # GPLv3 # http://daemonfreedom.blogspot.com/ try: from Tkinter import * #For GUI except ImportError: print "Weather Tkinter: No se encuentra python-tk.\nPara instalar el paquete en su equipo:\nsudo apt-get install python-tk\n\n" exit(1) import urllib2 #For urlopen class Application(Frame): """For GUI Aplication""" def __init__(self, master=None): Frame.__init__(self, master) self.grid() self.myvar =StringVar() self.createWidgets() def createWidgets(self): self.listfromfile =[] print "Weather Tkinter: createWidgest" self.cpLabel = Label(self, text="Código postal:") self.aboutLabel = Label(self, text="Weather\nTkinter") self.webLabel = Label(self, text="Mas app's: daemonfreedom.blogspot.com ") self.textEntry = Entry(self, textvariable=self.myvar) self.okButton = Button ( self, text='Ok', command=self.okAction ) self.impButton = Button ( self, text='Imprimir a archivo', command=self.imptofile) self.quitButton = Button ( self, text='Salir', command=self.quit ) self.textZone = Text(self, height=8, width=30) self.bmZone = Text(self, height=4, width=9) self.cpLabel.grid(row=1, column=1) self.aboutLabel.grid(row=3, column=3) self.webLabel.grid(row=2, column=1) self.textEntry.grid(row=1, column=2) self.okButton.grid(row=1, column=3) self.impButton.grid(row=2, column=2) self.quitButton.grid(row=2, column=3) self.textZone.grid(row=3, column=1) self.bmZone.grid(row=3, column=2) def okAction(self): print "Weather Tkinter: Ok" self.textZone.delete(1.0, END) self.myvar = self.textEntry.get() self.imp =self.genWeather(self.myvar) self.impList(self.imp) def genWeather(self, cp): try: print "Weather Tkinter: genWeather" self.cad="http://www.google.com/ig/api?weather="+cp+",spain&hl=es" self.lista = [] self.f = urllib2.urlopen(self.cad) self.down = self.f.read() self.f.close() for self.element in self.down: if self.element == "<": self.cadena ="" if self.element == ">": self.cadena=self.cadena+">" self.lista.append(self.cadena) self.cadena="" self.cadena =self.cadena+self.element self.final=[self.lista[4], self.lista[5], self.lista[13], self.lista[14], self.lista[15], self.lista[16], self.lista[18]] return self.final except IndexError: print "Weather Tkinter: Tipo nulo: genWeather" def impList(self, paramtolist): try: print "Weather Tkinter: impList" cont =0 self.state =0 for self.element in paramtolist: if cont ==3: self.textZone.insert(END, "Temperatura F: ") if cont ==4: self.textZone.insert(END, "Temperatura C: ") self.textZone.insert(END, self.retSplitXml(self.element)) self.textZone.insert(END, "\n") cont=cont+1 self.listfromfile.append(self.retSplitXml(self.element)) self.listfromfile.append("\n") print self.listfromfile except NoneType: print "Weather Tkinter: Tipo nulo: impList" def retSplitXml(self, splitcad): print "Weather Tkinter: retSplitXml" self.splitcad = splitcad.split("\"") return self.splitcad[1] def imptofile(self): print "Weather Tkinter: imptofile" wtfile = open("weather.txt", "w") wtfile.writelines(self.listfromfile) wtfile.close() def main(): print "Weather Tkinter: HELLO, This is Weather Tkinter! " app = Application() app.master.title("Weather Tkinter") app.mainloop() print "Weather Tkinter: Bye! " return 0 if __name__ == '__main__': main()
Otras versiones y apuntes: http://daemonfreedom.blogspot.com/2010/11/weather-tkinter.html