Rumah >pembangunan bahagian belakang >Tutorial Python >用wxpython写的一个天气预报

用wxpython写的一个天气预报

巴扎黑
巴扎黑asal
2016-12-09 11:43:011628semak imbas

自学了半个多月python和wx,想写个天气预报练练手,可是写的时候遇到了很多问题,非常纠结,索性把东西上传给各位看看。东西很简单,就几十行代码,以后有空还会继续增加功能。现在上传的目的就是希望有经验的人指点指点。 

碰到的问题有这几个: 

1、怎么给panel设置背景图片,并实现半透明。 

2、使用png图片的时候,有的图片可以实现透明,有的不可以。 

3、staticText背景总是为frame的背景,怎么去掉。 

4、StaticBitmap透明的问题也很棘手。 

暂时就这几个,好像还有好多,想不起来了。 

代码写的不是很规范,凑合看吧。呵呵 

# -*- coding: utf-8 -*-
import  wx
import  wx.html 
import urllib
import thread
import re
import sys 
reload(sys) 
sys.setdefaultencoding('utf8') 
#----------------------------------------------------------------------
class TestFrame(wx.Frame):
    def __init__(self, parent,):
        wx.Frame.__init__(self, parent, -1, "天气预报",
                         style =
                           wx.FRAME_SHAPED
                         | wx.SIMPLE_BORDER
                         | wx.FRAME_NO_TASKBAR
                         )
        self.count=0
        self.weatherInfo=''#天气信息
        self.hasShape = False
        self.Bind(wx.EVT_LEFT_DOWN,self.OnLeftDown)
        self.Bind(wx.EVT_LEFT_UP,self.OnLeftUp)
        self.Bind(wx.EVT_MOTION,self.OnMouseMove)
        self.delta = (0,0)
        self.bmp=wx.Image('bg.png',wx.BITMAP_TYPE_PNG,-1).ConvertToBitmap()
        self.w, self.h = self.bmp.GetWidth(), self.bmp.GetHeight()#获取图片背景长宽
        self.bmp.SetMask(wx.Mask(self.bmp, wx.BLACK))   
        self.SetClientSize( (self.w,self.h) )
        self.SetWindowShape()
        self.html= wx.html.HtmlWindow(self,-1,(self.w-230,50),(200,150))#天气信息显示区域
        self.initCloseButton()#初始化关闭按键
        self.initimg()#初始化天气图片
        self.Bind(wx.EVT_PAINT,self.OnPaint)
        thread.start_new_thread(self.getWeatherInfo,(1,2))#开启获取天气线程
    def initCloseButton(self):
        self.closebmp=wx.Image('no.png',wx.BITMAP_TYPE_PNG,-1).ConvertToBitmap()
        self.closebmp.SetMask(wx.Mask(self.closebmp,wx.BLACK))
        button=wx.BitmapButton(self, -1,self.closebmp,style=wx.NO_BORDER,pos=(self.w-30,20))   
        button.SetDefault()
        self.Bind(wx.EVT_BUTTON, self.onButtonClick,button)
    def getWeatherInfo(self,no,interval):
            web=urllib.urlopen('http://m.weather.com.cn/data/101190101.html')
            self.weatherInfo=web.read()
            web.close()
            print self.weatherInfo
            self.updateText()
    def updateText(self):
        weatherinfo=eval(self.weatherInfo)['weatherinfo']
        for i in weatherinfo:
            print i,weatherinfo[i]
        city=weatherinfo['city'].decode('utf-8')
        print city
        strs=&#39;<font  color="#444444">城市——%s</font>    <font   color="red" size="1">%s</font><br> \
             <font color="#444444">气温:%s<br>天气:%s</font><br>\
             <font color="#444444">风向:%s</font><br><br>\
             <font color="#444444">  </font>&#39;%(weatherinfo[&#39;city&#39;].decode(&#39;utf-8&#39;)
                                               ,weatherinfo[&#39;week&#39;].decode(&#39;utf-8&#39;)
                                               ,weatherinfo[&#39;temp1&#39;].decode(&#39;utf-8&#39;)
                                               ,weatherinfo[&#39;weather1&#39;].decode(&#39;utf-8&#39;)
                                               ,weatherinfo[&#39;wind1&#39;].decode(&#39;utf-8&#39;))
        self.html.SetPage(strs)
        imgno= weatherinfo[&#39;img2&#39;]
        imgno=re.findall(r&#39;\d&#39;,imgno)
        print imgno[0]
        self.bmp2=wx.Image(&#39;images/w%s.png&#39;%imgno[0],wx.BITMAP_TYPE_PNG,-1).ConvertToBitmap()  #更改天气图片 
        self.Refresh()#刷新窗口
    def initimg(self):
        self.bmp2=wx.Image(&#39;images/w0.png&#39;,wx.BITMAP_TYPE_PNG,-1).ConvertToBitmap()
    def SetWindowShape(self, *evt):
        r = wx.RegionFromBitmap(self.bmp)
        self.hasShape = self.SetShape(r)
    def OnPaint(self, evt):
        dc = wx.PaintDC(self)
        dc.DrawBitmap(self.bmp, 0,0, True)
        dc.DrawBitmap(self.bmp2, 10, 20, True)
    def onButtonClick(self,evt):
        print &#39;按键点击&#39;
        self.Destroy()
 
   
    def OnLeftDown(self, evt):
        self.CaptureMouse()
        x, y = self.ClientToScreen(evt.GetPosition())
        originx, originy = self.GetPosition()
        dx = x - originx
        dy = y - originy
        self.delta = ((dx, dy))
    def OnLeftUp(self, evt):
        if self.HasCapture():
            self.ReleaseMouse()
    def OnMouseMove(self, evt):
        if evt.Dragging() and evt.LeftIsDown():
            x, y = self.ClientToScreen(evt.GetPosition())
            fp = (x - self.delta[0], y - self.delta[1])
            self.Move(fp)     
 
#----------------------------------------------------------------------
app=wx.PySimpleApp()
win = TestFrame(None)
win.Show(True)
app.MainLoop()

 


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn