⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wxgetweather.py

📁 用python编写的天气预报程序
💻 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'天气预报',
                              size = (self.width, self.height))
        self.panel = wx.Panel(self.frame)
        self.statctxt = wx.StaticText(self.panel,
                                      -1,
                                      u'天气预报:'+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'清除内容',
                                    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"全国主要城市天气预报\n从新浪天气预报获得数据")
        
app = MyApp()
app.MainLoop()
                    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -