【发布时间】:2018-05-29 18:47:21
【问题描述】:
我已经完成了以下代码。计时器在控制台中正常工作。任何人都可以更新我的代码,以便它出现在我创建的对话框屏幕上。
我在 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_())
【问题讨论】:
-
其中一个主要问题是从未在小部件中设置 self.vbox,因此设置为 self.vbox 的小部件不会出现,而只是将 self.time_passed_qll 设置为 self.vbox 所以它不会显示。你能看到标签吗?
-
您可以显示一张图片,在其中指明您希望放置文本的位置。
-
不,我看不到标签。我只能看到登录屏幕。单击登录按钮时,我在屏幕上看不到任何内容。在控制台上全部打印出来。
-
发生的情况是 QLabel 没有正确设置,我无法想象您希望它在哪里成为 QLabel,所以我要求您显示一张图像,指示您希望 QLabel 显示在哪里。可以吗?
-
是的,我想在我的对话框的空白处显示登录按钮下的计时器
标签: python python-3.x pyqt pyqt5