You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyQt5中屏幕计时器不显示问题求助

解决PyQt5登录对话框倒计时不显示的问题

我帮你排查了代码里的几个关键问题,修改后就能让倒计时正常显示在界面上了,先看修复后的完整代码:

# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'login.ui'
#
# Created by: PyQt5 UI code generator 5.5.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys  # 新增:导入sys模块,因为倒计时结束逻辑用到了sys.exit

class Ui_Dialog(QtWidgets.QDialog):  # 修改:继承QDialog而非QMainWindow,和实例化的窗口类型匹配
    def logincheck(self):
        print("loged")
        self.my_qtimer = QtCore.QTimer(self)
        print("timer created")
        self.my_qtimer.timeout.connect(self.timerTick)
        self.my_qtimer.start(1000)
        self.updateTimerDisplay()

    def timerTick(self):
        print("Control came")
        self.inicio -= 1
        if self.inicio <= 0:
            self.my_qtimer.stop()  # 修改:变量名统一为my_qtimer,原代码里的timer未定义
            sys.exit(1)
        self.updateTimerDisplay()

    def updateTimerDisplay(self):
        print("control to timer")
        minutes = self.inicio // 60  # 修改:用整数除法避免浮点数输出
        seconds = self.inicio % 60
        text = "%d:%02d" % (minutes, seconds)
        print("value is ", text)
        self.time_passed_qll.setText(str(text))

    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(564, 461)
        self.inicio = 180

        # 重构布局:正确创建主布局并绑定到Dialog
        self.vbox = QtWidgets.QVBoxLayout(Dialog)
        Dialog.setLayout(self.vbox)

        # 登录输入区域用表单布局,让界面更规整
        login_form = QtWidgets.QFormLayout()
        self.username_label = QtWidgets.QLabel("User_Name")
        self.username_text = QtWidgets.QLineEdit()
        self.password_label = QtWidgets.QLabel("Password")
        self.password_text = QtWidgets.QLineEdit()
        self.password_text.setEchoMode(QtWidgets.QLineEdit.Password)
        
        login_form.addRow(self.username_label, self.username_text)
        login_form.addRow(self.password_label, self.password_text)

        # 按钮区域用水平布局
        button_layout = QtWidgets.QHBoxLayout()
        self.login_button = QtWidgets.QPushButton("Login")
        self.reset_button = QtWidgets.QPushButton("Reset")
        button_layout.addWidget(self.login_button)
        button_layout.addWidget(self.reset_button)

        # 创建倒计时标签并设置样式(可选)
        self.time_passed_qll = QtWidgets.QLabel("3:00")
        self.time_passed_qll.setAlignment(QtCore.Qt.AlignCenter)
        self.time_passed_qll.setStyleSheet("font-size: 24px; font-weight: bold;")

        # 将所有组件添加到主垂直布局
        self.vbox.addLayout(login_form)
        self.vbox.addLayout(button_layout)
        self.vbox.addWidget(self.time_passed_qll)

        # 绑定登录按钮点击事件
        self.login_button.clicked.connect(self.logincheck)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Login"))
        self.username_label.setText(_translate("Dialog", "User_Name"))
        self.password_label.setText(_translate("Dialog", "Password"))
        self.login_button.setText(_translate("Dialog", "Login"))
        self.reset_button.setText(_translate("Dialog", "Reset"))

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

核心修改说明:

  • 类继承修正:把Ui_Dialog的父类从QMainWindow改为QDialog,和你实例化的窗口类型保持一致,避免布局绑定失败。
  • 布局逻辑重构:原代码里的布局操作混乱(比如错误地把Dialog添加到布局里),现在重新梳理了布局层级,确保倒计时标签被正确添加到对话框界面中。
  • 变量名统一:修正了timerTick方法里的未定义变量错误,把self.timer改为实际创建的self.my_qtimer。
  • 细节补全:添加了缺失的sys模块导入,用整数除法修正分钟数的计算逻辑,还给倒计时标签设置了初始文本和样式,提升界面体验。

现在点击登录按钮后,倒计时就会和控制台输出同步,正常显示在界面上啦!

内容的提问来源于stack exchange,提问作者Samudranil Roy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:54:02