定時器:
QTimer:提供了重複輪詢和單詞的的定時器
先創建QTimer的實例,將其timeout的信號連接到對應的槽,並調用start()。
常用方法
start(milliseconds) |
啓動或重新啓動定時器,時間間隔爲毫秒,如果定時器已經啓動,將被停止並重新啓動,若sigleShot爲True,則只被激活一次 |
stop() |
停止定時器 |
常用信號
singleShot |
在給定的時間間隔後,調用一個槽函數發射此信號 |
timeout |
當定時器超時發射此信號 |
#QTimer
# 表格控件實例
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class WinForm(QWidget):
def __init__(self,parent=None):
super(WinForm, self).__init__(parent)
self.setWindowTitle("QTimer")
self.listFile=QListWidget()
self.lable=QLabel("顯示當前時間")
self.startBtn=QPushButton("start")
self.endBtn = QPushButton("end")
layout=QGridLayout()
#初始化一個定時器
self.timer=QTimer(self)
#show Time方法
self.timer.timeout.connect(self.showTime)
layout.addWidget(self.lable,0,0,1,2)
layout.addWidget(self.startBtn,1,0)
layout.addWidget(self.endBtn,1,1)
self.startBtn.clicked.connect(self.startTimer)
self.endBtn.clicked.connect(self.endTimer)
self.setLayout(layout)
def showTime(self):
#獲取系統時間
time=QDateTime.currentDateTime()
#設置系統時間格式
timeDisplay=time.toString("yyyy-MM-dd hh:mm:ss dddd")
#在標籤上顯示時間
self.lable.setText(timeDisplay)
def startTimer(self):
#設置時間間隔並啓動定時器
self.timer.start(1000)
self.startBtn.setEnabled(False)
self.endBtn.setEnabled(True)
def endTimer(self):
self.timer.stop()
self.endBtn.setEnabled(False)
self.startBtn.setEnabled(True)
self.lable.setText("")
if __name__=="__main__":
app=QApplication(sys.argv)
#test 1
# demp=WinForm()
# demp.show()
#test 2
# lable=QLabel("<font color=red size=128><b>hello pyqt 10s quit </b></font>")
# #無邊框窗口
# lable.setWindowFlags(Qt.SplashScreen|Qt.FramelessWindowHint)
# lable.show()
# QTimer.singleShot(10000,app.quit)
sys.exit(app.exec_())