PyQt5开发暗黑风格的计时器

主要是学习多线程知识,使用的是QTime(),但是似乎用QThread()更多一些

(QThread()与QTimer()的优势不再赘述)

但是记下来总没错

QSS渲染使用的是qdarkstyle,因为我一直觉得这个暗黑风很帅

qdarkstyle在GitHub上一千多颗星,还是很受欢迎的

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
import qdarkstyle

global sec
sec = 0


class WorkThread(QThread):
    trigger = pyqtSignal()

    def __int__(self):
        super(WorkThread, self).__init__()

    def run(self):
        for i in range(2000000000):
            pass

        # 循环完毕后发出信号
        self.trigger.emit()

def countTime():
	global  sec
	sec += 1
	# LED显示数字+1
	lcdNumber.display(sec)


def work():
    # 计时器计时
    print("#30")
    timer.start(1000) # 将start写成statr居然不报错,但是找bug却找了半天
    # 计时开始
    workThread.start()
    # 当获得循环完毕的信号时,停止计时
    workThread.trigger.connect(timeStop)


def timeStop():
    print("#38")
    timer.stop()
    print("计时结束,共计用时:", lcdNumber.value())
    global sec
    sec = 0


if __name__ == "__main__":
    app = QApplication(sys.argv)
    top = QWidget()
    top.resize(300, 200)
    # 影藏边框
    top.setWindowFlags(QtCore.Qt.FramelessWindowHint)
    layout = QVBoxLayout(top)
    lcdNumber = QLCDNumber()
    layout.addWidget(lcdNumber)
    button = QPushButton("开始")
    layout.addWidget(button)

    timer = QTimer()
    workThread = WorkThread()
    button.clicked.connect(work)
    # 计时结束,触发countime
    timer.timeout.connect(countTime)
    # 进行渲染
    app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
    top.show()
    sys.exit(app.exec_())

运行之后如图

影藏了边框

展开阅读全文

没有更多推荐了,返回首页