PyQt - 显示倒数计时器 [英] PyQt - showing countdown timer

查看:99
本文介绍了PyQt - 显示倒数计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PyQt 创建一个程序,该程序目前由 4 页的堆叠小部件组成.已实现计时器,并且在 3 秒后用户当前所在的页面切换到下一页.我目前正在尝试添加一个计时器,向用户显示他们在该页面上剩余的时间(最好以秒为单位).但是,我不知道如何将计时器连接到计数器,并且找不到任何有用的东西.

I am trying to create a program, using PyQt, which currently consists of a stacked widget of 4 pages. A timer has been implemented, and after 3 seconds the page the user is currently on switches to the next page. I am currently trying to add a timer which shows the user how much time on that page they have left (ideally in seconds). However, I have no idea how to connect the timer to a counter, and have not been able to find anything which would be helpful.

这是当前的代码,基本上就是定时器:

Here is the current code, which is basically just the timer:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from StackedWidget import *

app = QApplication(sys.argv)


window = QMainWindow()


ui = Ui_MainWindow()
ui.setupUi(window)


def NextTimer():
   ui.stackedWidget.setCurrentIndex((ui.stackedWidget.currentIndex() + 1))


myTimer = QTimer()
myTimer.timeout.connect(NextTimer)
myTimer.start(3000)

我正在尝试将计时器连接到 QLCDNumber 类的计数器,标记为 Counter.目前还没有涉及这个计数器的代码,它只是存在.

I am trying to connect the timer to a counter of the class QLCDNumber,labelled Counter. At the current point there is no code which involves this counter, it just exists.

感谢您的帮助.

推荐答案

以下是 PyQt5 中的一个工作示例,说明如何实现您所描述的功能:

Here is a working example in PyQt5 of how you can implement the functionality you described:

import sys
from PyQt5 import QtCore
from PyQt5 import QtWidgets

DURATION_INT = 3


class MyMainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        self.time_left_int = DURATION_INT
        self.widget_counter_int = 0

        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)
        vbox = QtWidgets.QVBoxLayout()
        central_widget.setLayout(vbox)

        self.pages_qsw = QtWidgets.QStackedWidget()
        vbox.addWidget(self.pages_qsw)
        self.time_passed_qll = QtWidgets.QLabel()
        vbox.addWidget(self.time_passed_qll)

        self.widget_one = QtWidgets.QLabel("This is widget one")
        self.pages_qsw.addWidget(self.widget_one)
        self.widget_two = QtWidgets.QLabel("This is widget two")
        self.pages_qsw.addWidget(self.widget_two)
        self.widget_three = QtWidgets.QLabel("This is widget three")
        self.pages_qsw.addWidget(self.widget_three)
        self.widget_four = QtWidgets.QLabel("This is widget four")
        self.pages_qsw.addWidget(self.widget_four)

        self.timer_start()
        self.update_gui()

    def timer_start(self):
        self.time_left_int = DURATION_INT

        self.my_qtimer = QtCore.QTimer(self)
        self.my_qtimer.timeout.connect(self.timer_timeout)
        self.my_qtimer.start(1000)

        self.update_gui()

    def timer_timeout(self):
        self.time_left_int -= 1

        if self.time_left_int == 0:
            self.widget_counter_int = (self.widget_counter_int + 1) % 4
            self.pages_qsw.setCurrentIndex(self.widget_counter_int)
            self.time_left_int = DURATION_INT

        self.update_gui()

    def update_gui(self):
        self.time_passed_qll.setText(str(self.time_left_int))


app = QtWidgets.QApplication(sys.argv)
main_window = MyMainWindow()
main_window.show()
sys.exit(app.exec_())

我希望这会有所帮助!

这篇关于PyQt - 显示倒数计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆