欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

pyqt4教程之实现半透明的天气预报界面示例

程序员文章站 2023-10-21 08:47:30
复制代码 代码如下:# -*- coding: cp936 -*-import sysimport urllib2import jsonfrom pyqt4 import...

复制代码 代码如下:

# -*- coding: cp936 -*-
import sys
import urllib2
import json
from pyqt4 import qtcore, qtgui
class mywindow( qtgui.qlcdnumber,qtgui.qwidget):
    def __init__(self, parent=none):
        super(mywindow,self).__init__(parent)

        self.setwindowtitle("weather")
        self.resize(100,40)
        self.setnumdigits(0)
        self.setwindowflags(qtcore.qt.framelesswindowhint)
        self.setwindowopacity(0.5)
        url ='http://m.weather.com.cn/data/101090502.html'
        re = urllib2.urlopen(url).read()
        we = json.loads(re)['weatherinfo']
        label1 = qtgui.qlabel( we['city'] )
        label2 = qtgui.qlabel( we['date'] )
        label3 = qtgui.qlabel( we['week'] )
        label4 = qtgui.qlabel( we['temp1'])
        label5 = qtgui.qlabel( we['weather1'] )
        #---------添加表格布局
        gridlayout = qtgui.qgridlayout()

        gridlayout.addwidget( label1 , 0, 0 )
        gridlayout.addwidget( label2 , 0, 1 )
        gridlayout.addwidget( label3 , 0, 2 )
        gridlayout.addwidget( label4 , 0, 3 )
        gridlayout.addwidget( label5 , 0, 4 )

        self.setlayout( gridlayout )
    def mousepressevent(self,event): 
        if event.button()==qtcore.qt.leftbutton: 
            self.dragposition=event.globalpos()-self.framegeometry().topleft() 
            event.accept() 
        if event.button()==qtcore.qt.rightbutton: 
            self.close() 

    def mousemoveevent(self,event): 
        if event.buttons() & qtcore.qt.leftbutton: 
            self.move(event.globalpos()-self.dragposition) 
            event.accept()  

app = qtgui.qapplication( sys.argv )
demo = mywindow()
demo.show()
app.exec_()