python GUI開發之計時器
1. 前言
pyqt5框架練手,實現一個計時器GUI。
2. 需求分析
實現計時器窗口。
功能需求:
- 可以倒計時也可以正計時;
- 以win平台可執行文件形式發布。
界面需求:
- 簡潔美觀;
- 計時器界面大小可調;
3. 詳細設計
開發語言:python
框架:pyqt5,qtdesigner
3.1. 界面設計
界面樣式:

3.2. singals/slot
singals/slot列表。

4. 開發過程
- qtdesigner設計界面;
- 建立信號及槽之間的聯系;
- 保存文件為xxx.ui;
- pyuic生成xxx.py文件,即為主窗口界面文件;
- 新建time_clock.py做邏輯處理;
- 根據各項菜單,按鈕的對應功能/槽函數逐一實現方法代碼;
- 運行/調試。
5. 使用說明
5.1. 菜單
File:
- start:同功能按鈕“開始/暫停”;
- reset:同功能按鈕“重置”;
- quit:退出。
Help:
- About:程序說明。
5.2. 功能按鈕
開始/暫停按鈕:開始計時,如果spinbox值為0則正向計時;如果給定值大於0則倒計時;
開始計時后可暫停;暫停后可繼續計時。
重置:計時器狀態重置。
6. 主要代碼
6.1. 邏輯處理代碼
time_clock.py
#!/usr/bin/env python
#coding:utf-8
"""
----------------------------------------
description:
author: sss
date:
----------------------------------------
change:
----------------------------------------
"""
__author__ = 'sss'
import sys
from functools import partial
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from timer_clock import *
class TimerClock(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(TimerClock, self).__init__()
self.setupUi(self)
self.statusBar().showMessage("Ready")
# status_running表示是否在計時狀態中
# 0 初始態; 1 運行態; 2 暫停態
self.status_running = 0
self.counter = QTime()
self.timer = QTimer()
# 固定QTimer的輪詢參數,指定輪詢間隔為5ms
self.timer_start = partial(self.timer.start, 5)
self.elasped_time = 0
self.target_time = None
self.timer.timeout.connect(self.timer_timeout)
# 開始/暫停按鈕功能實現
def click_start_button(self):
if self.status_running == 0:
self.target_time = int(self.spinBox.text()) * 60
self.target_time = None if self.target_time == 0 else self.target_time
self.status_running = 1
self.pushButton.setText("暫停")
self.counter.restart()
self.timer_start() # 開始計時
elif self.status_running == 1:
self.status_running = 2
self.pushButton.setText("繼續")
self.elasped_time = self.elasped_time + self.counter.elapsed() // 1000
if self.target_time is not None:
self.target_time -= self.counter.elapsed() // 1000
self.timer.stop()
else:
self.status_running = 1
self.pushButton.setText("暫停")
self.counter.start()
self.timer_start() # 開始計時
# 重置按鈕功能實現
def click_reset_button(self):
# 停止循環和計時
self.timer.stop()
#self.counter.restart()
# 參數重置,組件狀態重置
self.status_running = 0
self.target_time = None
self.elasped_time = 0
self.pushButton.setText("開始")
self.lineEdit.setText("00:{:02}:00".format(int(self.spinBox.text())))
# 計時器輪詢
def timer_timeout(self):
t_secs = self.counter.elapsed()//1000
if self.target_time is not None:
t_secs = self.target_time - t_secs
else:
t_secs += self.elasped_time
# 計時結束
if t_secs < 0:
msBox = QMessageBox(QMessageBox.NoIcon, "提示", "計時結束!")
msBox.exec_()
self.timer.stop()
self.pushButton.setText("開始")
t_str = "{:02}:{:02}:{:02}".format((t_secs//60)//60,(t_secs//60)%60, (t_secs)%60)
self.lineEdit.setText(t_str)
# spinbox值改變
def spinBox_valuechanged(self, value):
self.lineEdit.setText("00:{:02}:00".format(int(self.spinBox.text())))
def show_help_message(self):
msBox = QMessageBox(QMessageBox.NoIcon, "關於", "計時器\r\n Version:0.1\r\n 作者:SSS")
msBox.exec_()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ui = TimerClock()
ui.show()
sys.exit(app.exec_())
