美文网首页
倒计时可暂停

倒计时可暂停

作者: Python_Camp | 来源:发表于2022-07-16 22:38 被阅读0次
  1. 创建计数变量和标志,以了解计数器是否停止或运行。
  2. 为每个按钮添加动作
  3. 在获取第二个按钮的动作中,使用输入对话框获取第二个值,并使标志为假。
  4. 在开始动作中,使标志为真,但如果计数为零,则为假。
  5. 在暂停动作中,使标志为假
  6. 在复位动作中,使标志为假,将计数值设为零,并将文本设为标签。
  7. 制作一个定时器对象,每隔100毫秒调用其方法。
  8. 8.在定时器动作中检查标志,然后递减计数的值,并将文本设置为标签。

# 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("Codeye Countdown ")

        # setting geometry
        self.setGeometry(100, 100, 400, 600)

        # calling method
        self.UiComponents()

        # showing all the widgets
        self.show()

    # method for widgets
    def UiComponents(self):

        # variables
        # count variable
        self.count = 0

        # start flag
        self.start = False

        # creating push button to get time in seconds
        button = QPushButton("Set time(s)", self)

        # setting geometry to the push button
        button.setGeometry(125, 100, 150, 50)

        # adding action to the button
        button.clicked.connect(self.get_seconds)

        # creating label to show the seconds
        self.label = QLabel("//TIMER//", self)

        # setting geometry of label
        self.label.setGeometry(100, 200, 200, 50)

        # setting border to the label
        self.label.setStyleSheet("border : 3px solid black")

        # setting font to the label
        self.label.setFont(QFont('Times', 15))

        # setting alignment ot the label
        self.label.setAlignment(Qt.AlignCenter)

        # creating start button
        start_button = QPushButton("Start", self)

        # setting geometry to the button
        start_button.setGeometry(125, 350, 150, 40)

        # adding action to the button
        start_button.clicked.connect(self.start_action)

        # creating pause button
        pause_button = QPushButton("Pause", self)

        # setting geometry to the button
        pause_button.setGeometry(125, 400, 150, 40)

        # adding action to the button
        pause_button.clicked.connect(self.pause_action)

        # creating reset button
        reset_button = QPushButton("Reset", self)

        # setting geometry to the button
        reset_button.setGeometry(125, 450, 150, 40)

        # adding action to the button
        reset_button.clicked.connect(self.reset_action)

        # 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.start:
            # incrementing the counter
            self.count -= 1

            # timer is completed
            if self.count == 0:

                # making flag false
                self.start = False

                # setting text to the label
                self.label.setText("Completed !!!! ")

        if self.start:
            # getting text from count
            text = str(self.count / 10) + " s"

            # showing text
            self.label.setText(text)


    # method called by the push button
    def get_seconds(self):

        # making flag false
        self.start = False

        # getting seconds and flag
        second, done = QInputDialog.getInt(self, 'Seconds', 'Enter Seconds:')

        # if flag is true
        if done:
            # changing the value of count
            self.count = second * 10

            # setting text to the label
            self.label.setText(str(second))

    def start_action(self):
        # making flag true
        self.start = True

        # count = 0
        if self.count == 0:
            self.start = False

    def pause_action(self):

        # making flag false
        self.start = False

    def reset_action(self):

        # making flag false
        self.start = False

        # setting count value to 0
        self.count = 0

        # setting label text
        self.label.setText("//TIMER//")


# create pyqt5 app
App = QApplication(sys.argv)

# create the instance of our Window
window = Window()

# start the app
sys.exit(App.exec())

how to convert exe later?

相关文章

  • 倒计时可暂停

    创建计数变量和标志,以了解计数器是否停止或运行。 为每个按钮添加动作 在获取第二个按钮的动作中,使用输入对话框获取...

  • iOS 短信验证码倒计时按钮

    解决了App进入后台暂停倒计时的问题

  • van-count-down 倒计时相关问题

    1. 倒计时重置问题 背景:业务需要在开启倒计时、暂停、恢复倒计时实时判断是否展示倒计时,并重置倒计时 问题:直接...

  • iOS开发之UITableViewCell可暂停倒计时

    前言 最近开发中,用到了UITableViewCell倒计时功能,这里将这部分功能分离出来,供大家参考。 1.原理...

  • 停更通知

    考研倒计时仅剩25天。 小说暂停更~ 愿君体谅~

  • 倒计时~13

    木君 棒棒糖的倒计时在今天出现了暂停!停三天!这样的暂停,意味着棒棒糖的倒计时要加上三天!如果不是夭折是顺利还在的...

  • 2018-08-08

    倒计时1788天。暂停诵经准备面试,以后补回。祝你平安。

  • 2018-07-23

    倒计时1803天。暂停诵经准备面试,以后补回。祝你平安。

  • 2018-07-30

    倒计时1796天。暂停诵经准备面试,以后补回。祝你平安。

  • 2018-07-29

    倒计时1797天。暂停诵经准备面试,以后补回。祝你平安。

网友评论

      本文标题:倒计时可暂停

      本文链接:https://www.haomeiwen.com/subject/wnskirtx.html