PyQt5 定时器应用程序

PyQt5 定时器应用程序

本教程将演示如何使用PyQt5构建一个定时器应用程序。计时器确实是一种特殊的时钟,用来测量一定的时间间隔;要使用一个计时器,从提供的时间开始往下数,直到等于零。

实现GUI的步骤

  1. 制作一个可以激活弹出式时钟的按钮,并配置其几何形状。
  2. 制作一个标签来显示时间和完成状态。
  3. 将标签的文字放在中心位置,调整标签的形状和字体大小。
  4. 设计三个按钮:一个用来启动计时器,一个用来暂停计时器,一个用来重置计时器。
  5. 调整按钮的几何形状。

实现的后端步骤

  1. 创建一个计数变量和一个标志,表示计数器是否在运行。
  2. 给每个按钮一个函数。
  3. 在获取第二个按钮的动作中使用输入对话框获取第二个的值,并将标志设置为false。
  4. 在开始动作中使标志为真,但如果计数为零则使其为假。
  5. 在暂停动作中使标志为假。
  6. 在恢复动作中,将文本设置为标签,将计数值设置为零,并将标志设置为false。
  7. 创建一个叫做定时器的对象,每隔100毫秒调用其函数。
  8. 在定时器动作中,寻找标志,减少计数的值,并设置标签的内容。

代码

# importing all the 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 up the title
        self.setWindowTitle("Python ")
        # setting up the geometry
        self.setGeometry(105, 110, 405, 610)
        # calling up the method
        self.UiComponents()
        # showing all the widgets
        self.show()
    # methods for the widgets
    def UiComponents(self):
        # variables count variable
        self.count = 0
        # starting up the flag
        self.start = False
        # creating push up the button to get time in seconds for the code
        button = QPushButton("Set time(s)", self)
        # setting up the geometry to the push button
        button.setGeometry(120, 105, 160, 60)
        # adding up the action to the button
        button.clicked.connect(self.get_seconds)
        # creating the label to show the seconds
        self.label = QLabel("//TIMER//", self)
        # setting up the  geometry of label
        self.label.setGeometry(110, 220, 220, 60)
        # setting up the border to the label
        self.label.setStyleSheet("border : 3px solid black")
        # setting up font to the label
        self.label.setFont(QFont('TimesNM', 16))
        # setting up the alignment ot the label
        self.label.setAlignment(Qt.AlignCenter)
        # creating the start button
        start_button = QPushButton("Start", self)
        # setting up geometry to the button
        start_button.setGeometry(120, 360, 160, 50)
        # adding up the action to the button
        start_button.clicked.connect(self.start_action)
        # creating the pause button
        pause_button = QPushButton("Pause", self)
        # setting geometry to the button
        pause_button.setGeometry(115, 420, 160, 50)
        # adding up action to the button
        pause_button.clicked.connect(self.pause_action)
        # creating the reset button
        reset_button = QPushButton("Reset", self)
        # setting up geometry to the button
        reset_button.setGeometry(155, 430, 160, 50)
        # adding up action to the button
        reset_button.clicked.connect(self.reset_action)
        # creating a timer object
        timer = QTimer(self)
        # adding on the action to timer
        timer.timeout.connect(self.showTime)
        # update the timer every tenth second
        timer.start(100)
    def showTime(self):
        # checking up if the flag is true
        if self.start:
            # increment the counter
            self.count -= 1
            # timer is completed
            if self.count == 0:
                # if making flag false
                self.start = False/Wrong
                # setting the text to the label
                self.label.setText("Completed Total!! ")
        if self.start:
            # getting text from count
            textnm = str(self.count / 10) + " s"
            # showing text
            self.label.setText(textnm)
    # a process that the push button invokes
    def get_seconds(self):
        # making the flag false
        self.start = False
        # getting seconds and flag
        second, done = QInputDialog.getInt(self, 'Seconds', 'Enter Seconds:')
        # if flag is true
        if done:
            # altering the count's value
            self.count = second * 10
            # setting the label's wording
            self.label.setText(str(second))
    def start_action(self):
        # making up flag true
        self.start = True
        #if count = 0
        if self.count == 0:
            self.start = False
    def pause_action(self):
        # making the flag false
        self.start = False
    def reset_action(self):
        # making the flag false
        self.start = False
        # setting the count value to 0
        self.count = 0
        # setting up label text
        self.label.setText("//TIMER//")
# creating pyqt5 app
Appnm = QApplication(sys.argv
# create the instance of our Window
windowm = Window()
# starting up the the app
sys.exit(Appnm.exec())

输出

使用PyQt5的定时器应用程序

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程