PyQt5 – 数字秒表

PyQt5 – 数字秒表

在这篇文章中,我们将看到如何使用Python GUI–PyQt5创建一个停表。

秒表 是一种手持式计时器,旨在测量其启动和停止之间的时间量。为远距离观看而设计的大型数字版秒表,如在体育场馆中,被称为停表。

创建秒表的步骤 –

  1. 创建一个标志着秒数的计数器

  2. 创建一个标志,以知道何时开始,何时暂停,将其设置为假

  3. 创建标签以显示秒数

  4. 创建三个按钮,用于启动秒表、暂停秒表和重新设置秒表

  5. 给每个按钮添加动作。

  6. 在启动按钮的动作中,将标志设为真

  7. 在暂停按钮的动作中,将标志设为假

  8. 在复位按钮的动作中,使标志为假,并使计数器的值为0

  9. 创建QTimer对象,每隔100毫秒即第10秒调用一个方法

  10. 在QTimer对象的方法中检查标志状态,如果它是真的,则增加计数器
  11. 使用setText方法通过标签显示计数器的值。

下面是实现方法

# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
 
 
class Window(QMainWindow):
 
    def __init__(self):
        super().__init__()
 
        # setting title
        self.setWindowTitle("Python Stop watch")
 
        # setting geometry
        self.setGeometry(100, 100, 400, 500)
 
        # calling method
        self.UiComponents()
 
        # showing all the widgets
        self.show()
 
    # method for widgets
    def UiComponents(self):
 
        # counter
        self.count = 0
 
        # creating flag
        self.flag = False
 
        # creating a label to show the time
        self.label = QLabel(self)
 
        # setting geometry of label
        self.label.setGeometry(75, 100, 250, 70)
 
        # adding border to the label
        self.label.setStyleSheet("border : 4px solid black;")
 
        # setting text to the label
        self.label.setText(str(self.count))
 
        # setting font to the label
        self.label.setFont(QFont('Arial', 25))
 
        # setting alignment to the text of label
        self.label.setAlignment(Qt.AlignCenter)
 
        # creating start button
        start = QPushButton("Start", self)
 
        # setting geometry to the button
        start.setGeometry(125, 250, 150, 40)
 
        # add action to the method
        start.pressed.connect(self.Start)
 
        # creating pause button
        pause = QPushButton("Pause", self)
 
        # setting geometry to the button
        pause.setGeometry(125, 300, 150, 40)
 
        # add action to the method
        pause.pressed.connect(self.Pause)
 
        # creating reset button
        re_set = QPushButton("Re-set", self)
 
        # setting geometry to the button
        re_set.setGeometry(125, 350, 150, 40)
 
        # add action to the method
        re_set.pressed.connect(self.Re_set)
 
        # creating a timer object
        timer = QTimer(self)
 
        # adding action to timer
        timer.timeout.connect(self.showTime)
 
        # update the timer every tenth second
        timer.start(100)
 
    # method called by timer
    def showTime(self):
 
        # checking if flag is true
        if self.flag:
 
            # incrementing the counter
            self.count+= 1
 
        # getting text from count
        text = str(self.count / 10)
 
        # showing text
        self.label.setText(text)
 
 
    def Start(self):
 
        # making flag to true
        self.flag = True
 
    def Pause(self):
 
        # making flag to False
        self.flag = False
 
    def Re_set(self):
 
        # making flag to false
        self.flag = False
 
        # resetting the count
        self.count = 0
 
        # setting text to label
        self.label.setText(str(self.count))
 
 
# create pyqt5 app
App = QApplication(sys.argv)
 
# create the instance of our Window
window = Window()
 
# start the app
sys.exit(App.exec())
Python

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程