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

yipeiwu_com5年前Python基础

复制代码 代码如下:

# -*- 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_()

相关文章

使用python将最新的测试报告以附件的形式发到指定邮箱

使用python将最新的测试报告以附件的形式发到指定邮箱

具体代码如下所示: import smtplib, email, os, time from email.mime.multipart import MIMEMultipart fr...

Python中使用PIL库实现图片高斯模糊实例

Python中使用PIL库实现图片高斯模糊实例

一、安装PIL PIL是Python Imaging Library简称,用于处理图片。PIL中已经有图片高斯模糊处理类,但有个bug(目前最新的1.1.7bug还存在),就是模糊半径写...

Python中的map()函数和reduce()函数的用法

Python中的map()函数和reduce()函数的用法

Python内建了map()和reduce()函数。 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Processing on Lar...

go语言计算两个时间的时间差方法

本文实例讲述了go语言计算两个时间的时间差方法。分享给大家供大家参考。具体分析如下: go语言计算两个时间的时间差,代码很简单,返回1天前、1周前还是1月前的时间 package m...

详解Python对JSON中的特殊类型进行Encoder

Python 处理 JSON 数据时,dumps 函数是经常用到的,当 JSON 数据中有特殊类型时,往往是比较头疼的,因为经常会报这样一个错误。 自定义编码类 #!/usr/bi...