编辑*:我不明白为什么我得到一个减号,但我不是一个I.T毕业生。我正在自学编码..很多编程术语我也不明白..请善待......
我正在尝试使用Qt和PyQT构建一个登录窗口。 我从这里尝试了一个: https://www.youtube.com/watch?v=X_QBq2L1uzQ
但不知怎的,它不起作用。
我正在CentOS 6上构建我的应用程序。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'taco_login.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_login_window(object):
def setupUi(self, login_window):
login_window.setObjectName(_fromUtf8("login_window"))
login_window.resize(255, 150)
login_window.setMinimumSize(QtCore.QSize(255, 150))
login_window.setMaximumSize(QtCore.QSize(255, 150))
login_window.setStyleSheet(_fromUtf8("QDialog#login_window {\n"
" background-color: qlineargradient(spread:pad, x1:0.559, y1:0, x2:0.555, y2:1, stop:0.445455 rgba(48, 48, 48, 255), stop:0.859091 rgba(61, 61, 61, 255), stop:1 rgba(79, 79, 79, 255));\n"
"}"))
self.verticalLayout_2 = QtGui.QVBoxLayout(login_window)
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.widget = QtGui.QWidget(login_window)
self.widget.setObjectName(_fromUtf8("widget"))
self.password_lineEdit = QtGui.QLineEdit(self.widget)
self.password_lineEdit.setGeometry(QtCore.QRect(10, 60, 221, 22))
self.password_lineEdit.setStyleSheet(_fromUtf8("QWidget {border:none;}"))
self.password_lineEdit.setInputMask(_fromUtf8(""))
self.password_lineEdit.setEchoMode(QtGui.QLineEdit.Password)
self.password_lineEdit.setObjectName(_fromUtf8("password_lineEdit"))
self.login_btn = QtGui.QPushButton(self.widget)
self.login_btn.setGeometry(QtCore.QRect(10, 100, 91, 17))
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
self.login_btn.setPalette(palette)
self.login_btn.setAutoFillBackground(False)
self.login_btn.setStyleSheet(_fromUtf8("QPushButton#login_btn {\n"
" background-color: rgb(255, 255, 255);\n"
" border:none;\n"
"}"))
self.login_btn.setObjectName(_fromUtf8("login_btn"))
self.username_lineEdit = QtGui.QLineEdit(self.widget)
self.username_lineEdit.setGeometry(QtCore.QRect(10, 30, 221, 22))
self.username_lineEdit.setStyleSheet(_fromUtf8("QWidget {border:none;}"))
self.username_lineEdit.setInputMask(_fromUtf8(""))
self.username_lineEdit.setObjectName(_fromUtf8("username_lineEdit"))
self.text_1 = QtGui.QTextEdit(self.widget)
self.text_1.setGeometry(QtCore.QRect(6, 0, 141, 25))
self.text_1.setStyleSheet(_fromUtf8("QTextEdit#text_1{\n"
"border: none;\n"
" background-color: transparent;\n"
"}\n"
""))
self.text_1.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
self.text_1.setObjectName(_fromUtf8("text_1"))
self.cancel_btn = QtGui.QPushButton(self.widget)
self.cancel_btn.setGeometry(QtCore.QRect(140, 100, 91, 17))
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
self.cancel_btn.setPalette(palette)
self.cancel_btn.setAutoFillBackground(False)
self.cancel_btn.setStyleSheet(_fromUtf8("QPushButton#cancel_btn {\n"
" background-color: rgb(255, 255, 255);\n"
" border:none;\n"
"}"))
self.cancel_btn.setObjectName(_fromUtf8("cancel_btn"))
self.verticalLayout_2.addWidget(self.widget)
self.retranslateUi(login_window)
QtCore.QMetaObject.connectSlotsByName(login_window)
def retranslateUi(self, login_window):
login_window.setWindowTitle(_translate("login_window", "TACO", None))
self.password_lineEdit.setText(_translate("login_window", "Password", None))
self.login_btn.setText(_translate("login_window", "Login", None))
self.username_lineEdit.setText(_translate("login_window", "Username", None))
self.text_1.setHtml(_translate("login_window", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Sans\'; font-size:10pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:9pt; font-weight:600; color:#f0f0f0;\">Welcome to TACO!</span></p></body></html>", None))
self.cancel_btn.setText(_translate("login_window", "Cancel", None))
@QtCore.pyqtSignature("on_cancel_btn_clicked()")
def Cancel_btn(self):
self.close()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
login_window = QtGui.QDialog()
ui = Ui_login_window()
ui.setupUi(login_window)
login_window.show()
sys.exit(app.exec_())
起初我尝试使用以下方法关闭我的程序:
@QtCore.pyqtSignature("on_cancel_btn_clicked()")
def Cancel_btn(self):
self.close()
但不知怎的,它没有用。 我也试图打开一个新窗口,下面的代码会工作吗?
@QtCore.pyqtSignature("on_login_btn_clicked()")
def Login_btn(self):
username = 'usertest':
password = 'passtest':
if not username:
QtGui.QMessageBox.warning(self, 'Username Missing!')
elif not password:
QtGui.QMessageBox.warning(self, 'Password Missing!')
else:
self.AttemptLogin(username, password)
def AttemptLogin(self, username, password):
t = self.dbu.GetTable()
print (t)
for col in t:
if username == 'usertest':
if password == 'passtest':
QtGui.QMessageBox.information(self, 'Success!!')
self.close()
else:
QtGui.QMessageBox.warning(self, 'Incorrect Username or Password')
return
请帮帮我。感谢。
答案 0 :(得分:0)
继承用于表示某个类将获得其父类的所有功能。当您编写class child(parent)
时,您child
继承自parent
。
在您的代码中,只需执行以下操作:
class Ui_Login(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
我们致电QtGui.QDialog.__init__(self)
,以便Ui_Login()
的初始化方式与QtGui.QDialog()
在if
语句中,不需要login_window = QtGui.QDialog()
且setpUi()
不需要ui
引用,因为您应该使用self
(替换{ {1}} login_window
和setupUi
retranslateUi
中出现{1}}。
将您的代码更改为:
self