自学了半个多月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,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=’城市——%s %s \
气温:%s天气:%s\
风向:%s\
‘%(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()