Advertisement
Guest User

Sergio López

a guest
Nov 15th, 2010
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # By Sergio López
  5. # GPLv3
  6. # http://daemonfreedom.blogspot.com/
  7.  
  8. try:
  9.     from Tkinter import *       #For GUI
  10.    
  11. except ImportError:
  12.     print "Weather Tkinter: No se encuentra python-tk.\nPara instalar el paquete en su equipo:\nsudo apt-get install python-tk\n\n"
  13.     exit(1)
  14.  
  15. import urllib2              #For urlopen
  16.  
  17. class Application(Frame):
  18.     """For GUI Aplication"""
  19.    
  20.     def __init__(self, master=None):
  21.         Frame.__init__(self, master)
  22.         self.grid()
  23.         self.myvar =StringVar()
  24.         self.createWidgets()
  25.  
  26.     def createWidgets(self):
  27.         self.listfromfile =[]
  28.         print "Weather Tkinter: createWidgest"
  29.         self.cpLabel = Label(self, text="Código postal:")
  30.         self.aboutLabel = Label(self, text="Weather\nTkinter")
  31.         self.webLabel = Label(self, text="Mas app's: daemonfreedom.blogspot.com ")
  32.         self.textEntry = Entry(self, textvariable=self.myvar)
  33.         self.okButton = Button ( self, text='Ok', command=self.okAction )
  34.         self.impButton = Button ( self, text='Imprimir a archivo', command=self.imptofile)
  35.         self.quitButton = Button ( self, text='Salir', command=self.quit )
  36.         self.textZone = Text(self, height=8, width=30)
  37.         self.bmZone = Text(self, height=4, width=9)
  38.        
  39.         self.cpLabel.grid(row=1, column=1)
  40.         self.aboutLabel.grid(row=3, column=3)
  41.         self.webLabel.grid(row=2, column=1)
  42.         self.textEntry.grid(row=1, column=2)
  43.         self.okButton.grid(row=1, column=3)
  44.         self.impButton.grid(row=2, column=2)
  45.         self.quitButton.grid(row=2, column=3)
  46.         self.textZone.grid(row=3, column=1)
  47.         self.bmZone.grid(row=3, column=2)
  48.    
  49.     def okAction(self):
  50.         print "Weather Tkinter: Ok"
  51.         self.textZone.delete(1.0, END)
  52.         self.myvar = self.textEntry.get()
  53.         self.imp =self.genWeather(self.myvar)
  54.         self.impList(self.imp)
  55.  
  56.     def genWeather(self, cp):
  57.         try:
  58.             print "Weather Tkinter: genWeather"
  59.             self.cad="http://www.google.com/ig/api?weather="+cp+",spain&hl=es"
  60.             self.lista = []
  61.        
  62.             self.f = urllib2.urlopen(self.cad)
  63.             self.down = self.f.read()
  64.             self.f.close()
  65.        
  66.             for self.element in self.down:
  67.                 if self.element == "<":
  68.                     self.cadena =""
  69.                 if self.element == ">":
  70.                     self.cadena=self.cadena+">"
  71.                     self.lista.append(self.cadena)
  72.                     self.cadena=""
  73.                 self.cadena =self.cadena+self.element
  74.             self.final=[self.lista[4], self.lista[5], self.lista[13], self.lista[14], self.lista[15], self.lista[16], self.lista[18]]
  75.             return self.final
  76.         except IndexError:
  77.             print "Weather Tkinter: Tipo nulo: genWeather"
  78.        
  79.     def impList(self, paramtolist):
  80.         try:
  81.             print "Weather Tkinter: impList"
  82.             cont =0
  83.             self.state =0
  84.             for self.element in paramtolist:
  85.                 if cont ==3:
  86.                     self.textZone.insert(END, "Temperatura F: ")
  87.                 if cont ==4:
  88.                     self.textZone.insert(END, "Temperatura C: ")
  89.                 self.textZone.insert(END, self.retSplitXml(self.element))
  90.                 self.textZone.insert(END, "\n")
  91.                 cont=cont+1
  92.            
  93.                 self.listfromfile.append(self.retSplitXml(self.element))
  94.                 self.listfromfile.append("\n")
  95.             print self.listfromfile
  96.         except NoneType:
  97.             print "Weather Tkinter: Tipo nulo: impList"
  98.            
  99.            
  100.     def retSplitXml(self, splitcad):
  101.         print "Weather Tkinter: retSplitXml"
  102.         self.splitcad = splitcad.split("\"")
  103.         return self.splitcad[1]
  104.        
  105.     def imptofile(self):
  106.         print "Weather Tkinter: imptofile"
  107.         wtfile = open("weather.txt", "w")
  108.         wtfile.writelines(self.listfromfile)
  109.         wtfile.close()
  110.        
  111.        
  112. def main():    
  113.  
  114.     print "Weather Tkinter: HELLO, This is Weather Tkinter! "
  115.    
  116.     app = Application()
  117.     app.master.title("Weather Tkinter")
  118.     app.mainloop()
  119.    
  120.     print "Weather Tkinter: Bye! "
  121.     return 0
  122.  
  123. if __name__ == '__main__':
  124.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement