elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Sigue las noticias más importantes de seguridad informática en el Twitter! de elhacker.NET


+  Foro de elhacker.net
|-+  Programación
| |-+  Scripting
| | |-+  Weather tkinter (Python)
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Weather tkinter (Python)  (Leído 3,470 veces)
@synthesize
Wiki

Desconectado Desconectado

Mensajes: 640


Another Brick in the Wall


Ver Perfil WWW
Weather tkinter (Python)
« en: 6 Marzo 2011, 20:07 pm »

Bueno, ya que hace tiempo que no me paso por aquí, voy a dejaros algunos programillas simples que he hecho.

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
  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()

Otras versiones y apuntes: http://daemonfreedom.blogspot.com/2010/11/weather-tkinter.html


En línea

Marot77

Desconectado Desconectado

Mensajes: 34



Ver Perfil
Re: Weather tkinter (Python)
« Respuesta #1 en: 9 Marzo 2011, 17:20 pm »

Buen aporte Serch,yo estoy aprendiendo,sigo buscando tutoriales que enseñen más específicos ¿De donde aprendistes tu python?


En línea





Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines