• 欢迎访问少将全栈,学会感恩,乐于付出,珍惜缘份,成就彼此、推荐使用最新版火狐浏览器和Chrome浏览器访问本网站。
  • 吐槽,投稿,删稿,交个朋友,商务沟通v:ai_draw
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏少将全栈吧

用wxpython写的一个天气预报

点滴 admin 10年前 (2014-03-10) 1516次浏览 已收录 0个评论 扫描二维码

自学了半个多月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=’font  color="#444444"城市??%s/fontnbsp;nbsp;nbsp;nbsp;font   color="red" size="1"%s/fontbr 
             font color="#444444"气温:%sbr天气:%s/fontbr
             font color="#444444"风向:%s/fontbrbr
             font color="#444444"  /font’%(weatherinfo[’city’].decode(’utf-8’)
                                               ,weatherinfo[’week’].decode(’utf-8’)
                                               ,weatherinfo[’temp1’].decode(’utf-8’)
                                               ,weatherinfo[’weather1’].decode(’utf-8’)
                                               ,weatherinfo[’wind1’].decode(’utf-8’))
        self.html.SetPage(strs)
        imgno= weatherinfo[’img2’]
        imgno=re.findall(r’d’,imgno)
        print imgno[0]
        self.bmp2=wx.Image(’images/w%s.png’%imgno[0],wx.BITMAP_TYPE_PNG,-1).ConvertToBitmap()  #更改天气图片 
        self.Refresh()#刷新窗口
    def initimg(self):
        self.bmp2=wx.Image(’images/w0.png’,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 ’按键点击’
        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()
 

喜欢 (0)
[🍬谢谢你请我吃糖果🍬🍬~]
分享 (0)
关于作者:
少将,关注Web全栈开发、项目管理,持续不断的学习、努力成为一个更棒的开发,做最好的自己,让世界因你不同。
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址