?? wxgetweather.py
字號:
#coding:utf-8
#---objective: weather forecast----------------------
#---author: Binbin Jiang-----------------------------
#---email: jiangbinbin@asee.buaa.edu.cn -------------
#---prerequisites: python 2.5, wxpython--------------
import urllib
import re
import wx
import datetime
class MyApp(wx.App):
def OnInit(self):
self.width = 280
self.height = 360
self.today = datetime.date.today()
self.frame = wx.Frame(parent = None,
title = u'天氣預(yù)報',
size = (self.width, self.height))
self.panel = wx.Panel(self.frame)
self.statctxt = wx.StaticText(self.panel,
-1,
u'天氣預(yù)報:'+str(self.today)+'\n'+u'作者:蔣斌斌',
pos=(15,7))
self.text = wx.TextCtrl(self.panel,
-1,
pos = (10,70),
size = (self.width-30, self.height-120),
style = wx.HSCROLL|wx.TE_MULTILINE)
self.button_get = wx.Button(self.panel,
-1,
u'獲取天氣',
pos = (180,10))
self.Bind(wx.EVT_BUTTON, self.OnButton_get, self.button_get)
self.button_clr = wx.Button(self.panel,
-1,
u'清除內(nèi)容',
pos = (180,40))
self.Bind(wx.EVT_BUTTON, self.OnButton_clr, self.button_clr)
self.choice = wx.Choice(self.panel,
-1,
choices=[],
pos = (25,40))
self.frame.Show()
return True
def OnButton_get(self, event):
self.text.SetValue("\n")
addr = "http://weather.news.sina.com.cn/images/figureWeather/map/wholeNation.html"
data = urllib.urlopen(addr)
mydata = data.read()
firststep = re.compile(r'<MAP name=Map>(.+?)</MAP>',re.S|re.I|re.U)
allcitys = re.compile(r'drawcitys\(\'(.+?)shape=',re.S|re.I|re.U)
allweather = re.compile(r' *(.+?)<br>',re.S|re.I|re.U)
num=1
self.hashweather={}
self.hashcity={}
for i in firststep.findall(mydata):
for j in allcitys.findall(i):
self.text.AppendText(str(self.today)+"\n--------------------\n")
#print j
tempweather=''
var=1
for k in allweather.findall(j):
if var==1:
self.hashcity[str(num)]=k.decode("gbk")
#self.hashcity[k]=str(num)
self.choice.Append(k)
#print k
#print str(num)
var=var+1
tempweather=tempweather+k+'\n\n'
self.hashweather[str(num)]=tempweather.decode("gbk")
self.text.AppendText(k+"\n")
#self.text.AppendText(self.hashweather[str(num)])
num=num+1
self.text.AppendText("\n============\n")
self.Bind(wx.EVT_CHOICE, self.OnChoice, self.choice)
data.close()
def OnChoice(self, event):
#print 'hello'
#self.text.AppendText(self.hashcity[str(1)])
#self.text.AppendText(self.hashweather[str(1)])
#self.text.AppendText(event.GetString())
#self.text.AppendText(self.hashcity[str(1)])
self.text.SetValue(str(self.today)+'\n\n')
chcity=event.GetString()
weindex=self.hashcity.keys()
for t in self.hashcity.keys():
if chcity == self.hashcity[t]:
self.text.AppendText(self.hashweather[t])
def OnButton_clr(self, event):
self.text.SetValue(u"全國主要城市天氣預(yù)報\n從新浪天氣預(yù)報獲得數(shù)據(jù)")
app = MyApp()
app.MainLoop()
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -