pyqt timer的应用

主要内容


  • 主要了解PyQt程序的整体框架;
  • 涉及模块:QApplication, QLabel , QTimer

 

实例讲解


首先先看一个简单的PyQt程序:

1
2
3
4
5
6
7
8
9
10
import  sys
from  PyQt4.QtGui  import  *
from  PyQt4.QtCore  import  *
 
app  =  QApplication(sys.argv)
label  =  QLabel( "<font color=red size=128><b>Hello PyQT!</b></font>" )
label.setWindowFlags(Qt.SplashScreen)
label.show()
QTimer.singleShot( 10000 , app.quit)  # 设置10s后自动退出
app.exec_()

 先看导入内容,PyQt中与界面有关的模块一般在QtGui中,而QtCore中包含了很多基本组件。

 应用程序整体框架为:

app =  QApplication(sys.argv)
#...
app.exec_()

 中间省略的部分即为自己实现的界面部分,上面的小例子中,将会出现一个小窗口,显示“Hello PyQT!”,接下来一行一行分析。

第6行,我们声明了一个QLabel ,它支持类似于html的语法;

第7行,我们将窗口类型设置为闪屏,这种窗口没有边框,一般用于软件启动画面;若将这句话注释掉,会得到一个正常窗口;

第8行,我们把它的属性设置为可见;

第9行中,我们用QTimer设置定时,10s后退出程序。

 

简要总结:


  •  PyQt 程序整体框架, QApplication
  •  QLabel 标签组件:显示内容
  •  QTimer定时组件:完成时间调度任务

 

扩展知识:


QLabel是一种经常使用的的组件,下面的程序提供了更多的关于QLabel用法

主要涉及模块:QFont, QPixMap, QFontMetric, QPainter, QTextDocument, 

QLabel 方法:

label.setPixmap(pixmap)

label.setMask(pixmap.createMaskFromColor(Qt.red))

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import  sys
from  PyQt4.QtGui  import  *
from  PyQt4.QtCore  import  *
 
msg  =  "Hello PyQt!"
app  =  QApplication(sys.argv)
font  =  QFont( "Times New Rome" 36 , QFont.Bold)
fm  =  QFontMetrics(font)
pixmap  =  QPixmap(fm.width(msg)  +  5 , fm.height()  +  5 )
pixmap.fill(Qt.white)
painter  =  QPainter(pixmap)
document  =  QTextDocument()
document.setDefaultFont(font)
document.setHtml( "<font color=red>%s</font>"  %  msg)
document.drawContents(painter)
label  =  QLabel()
label.setPixmap(pixmap)
label.setMask(pixmap.createMaskFromColor(Qt.red))
#label.setWindowFlags(Qt.SplashScreen|Qt.FramelessWindowHint)
label.show()
QTimer.singleShot( 10000 , app.quit)  # 1 minute
app.exec_()

 

 
from PyQt4 import QtCore, QtGui
 
def xxx():
def __init__(self):
        self.timer=QtCore.QTimer()
        QtCore.QObject.connect(self.timer,QtCore.SIGNAL("timeout()"), self.OnTimer)
        self.timer.start( 1000 )

def OnTimer(self):
        print 'Get Timer'
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值