在PyQt5中屏幕计时器没有出现

时间:2018-05-29 07:44:52

标签: python python-3.x pyqt pyqt5

enter image description here enter image description here

我已完成以下代码。计时器在控制台中正常工作。任何人都可以更新我的代码,以便它出现在我创建的对话框屏幕上。

我在Windows中使用Python 3.6中的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, Qt
from PyQt5.QtCore import *
from PyQt5.QtGui import *


# from relogio import Ui_relogiocc

class Ui_Dialog(QtWidgets.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.timer.stop()
            sys.exit(1)
        self.updateTimerDisplay()

    def updateTimerDisplay(self):
        print("control to timer")
        text = "%d:%02d" % (self.inicio / 60, self.inicio % 60)
        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

        # self.central_widget = QtWidgets.QWidget(Dialog)
        # self.setCentralWidget(self.central_widget)
        self.vbox = QtWidgets.QVBoxLayout()
        # self.central_widget.addLayout(self.vbox)

        self.time_passed_qll = QtWidgets.QLabel()


        # self.timerStatus = QtWidgets.QGroupBox('time Status')
        # self.timerStatusLayout = QtWidgets.QGridLayout()
        # self.timerStatus.setLayout(self.timerStatusLayout)
        # self.timerStatusLayout.addWidget(self.time_passed_qll, 0, 0)
        # self.vbox.addWidget(self.time_passed_qll)

        self.username_label = QtWidgets.QLabel(Dialog)
        self.username_label.setGeometry(QtCore.QRect(27, 60, 91, 16))
        self.username_label.setObjectName("username_label")
        self.password_label = QtWidgets.QLabel(Dialog)
        self.password_label.setGeometry(QtCore.QRect(30, 110, 68, 17))
        self.password_label.setObjectName("password_label")
        self.password_text = QtWidgets.QLineEdit(Dialog)
        self.password_text.setGeometry(QtCore.QRect(190, 100, 241, 27))
        self.password_text.setText("")
        self.password_text.setEchoMode(QtWidgets.QLineEdit.Password)
        self.password_text.setObjectName("password_text")
        self.login_button = QtWidgets.QPushButton(Dialog)
        self.login_button.setGeometry(QtCore.QRect(190, 190, 99, 27))
        self.login_button.setObjectName("login_button")
        ###################################################
        self.login_button.clicked.connect(self.logincheck)

        self.reset_button = QtWidgets.QPushButton(Dialog)
        self.reset_button.setGeometry(QtCore.QRect(330, 190, 99, 27))
        self.reset_button.setObjectName("reset_button")
        self.username_text = QtWidgets.QLineEdit(Dialog)
        self.username_text.setGeometry(QtCore.QRect(200, 60, 241, 27))
        self.username_text.setText("")
        self.username_text.setObjectName("username_text")

        ################## to show widget in screen##########
        self.vbox.addWidget(Dialog)
        self.vbox.addWidget(self.time_passed_qll)

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

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        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__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

我改变了我的代码,就像下面那样有效。

import sys
from PyQt5 import QtGui, QtCore, QtWidgets


class Example(QtWidgets.QMainWindow):
    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):
        #self.qle = QtWidgets.QLineEdit(self)
        #self.qle.move(5, 5)  # re



        #self.lbl = QtWidgets.QLabel(self)
        #self.lbl.move(5, 55)
        #btn = QtWidgets.QPushButton("Ok", self)
        #btn.move(5, 30)
        self.username_label = QtWidgets.QLabel(self)
        self.username_label.setGeometry(QtCore.QRect(27, 60, 91, 16))
        self.username_label.setObjectName("username_label")
        self.username_label.setText('Username')
        self.password_label = QtWidgets.QLabel(self)
        self.password_label.setText('Password')
        self.password_label.setGeometry(QtCore.QRect(30, 110, 68, 17))
        self.password_label.setObjectName("password_label")
        self.password_text = QtWidgets.QLineEdit(self)
        self.password_text.setGeometry(QtCore.QRect(190, 100, 241, 27))
        self.password_text.setText("")
        self.password_text.setEchoMode(QtWidgets.QLineEdit.Password)
        self.password_text.setObjectName("password_text")
        self.login_button = QtWidgets.QPushButton('login',self)
        self.login_button.setGeometry(QtCore.QRect(190, 190, 99, 27))
        self.login_button.setObjectName("login_button")
        self.reset_button = QtWidgets.QPushButton('reset',self)
        self.reset_button.setGeometry(QtCore.QRect(330, 190, 99, 27))
        self.reset_button.setObjectName("reset_button")
        self.username_text = QtWidgets.QLineEdit(self)
        self.username_text.setGeometry(QtCore.QRect(200, 60, 241, 27))
        self.username_text.setText("")
        self.username_text.setObjectName("username_text")

        self.timepass_label = QtWidgets.QLabel(self)
        self.timepass_label.setGeometry(QtCore.QRect(227, 260, 91, 16))
        self.timepass_label.setObjectName("timepass_label")
        self.inicio=180




        #btn.clicked.connect(self.buttonClicked)

        ###################################################
        self.login_button.clicked.connect(self.logincheck)

        self.setGeometry(800, 800, 800, 800)
        self.show()

    #def buttonClicked(self, sometext):
        #sender = self.sender()
     #   self.lbl.setText(self.qle.text())  # calling .text() method to
        # get the text from QLineEdit

    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.lbl.setText('time')

        self.updateTimerDisplay()

    def timerTick(self):
        print("Control came")
        self.inicio -= 1
        if self.inicio <= 0:
            self.timer.stop()
            sys.exit(1)

        self.updateTimerDisplay()

    def updateTimerDisplay(self):
        text = "%d:%02d" % (self.inicio / 60, self.inicio % 60)
        self.timepass_label.setText(text)


def main():
    app = QtWidgets.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()