Issue
Edit*: I don't understand why I got a minus but I'm not an I.T graduate. I'm learning coding by myself.. A lot of terms for programming I don't understand also.. Please be kind..
I'm trying to build a login window using Qt and PyQT. I tried the one from here: https://www.youtube.com/watch?v=X_QBq2L1uzQ
But somehow it's not working.
I'm building my apps on 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_())
At first I tried to close my program using:
@QtCore.pyqtSignature("on_cancel_btn_clicked()")
def Cancel_btn(self):
self.close()
But somehow it didn't work. I also trying to open a new window, will the code below working?
@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
Please help me. Thanks.
Solution
Inheritance is used to indicate that a class will get all features of its parent class. When you write class child(parent)
you make child
inherit from parent
.
In your code just do this:
class Ui_Login(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
We call QtGui.QDialog.__init__(self)
so that Ui_Login()
will be initialized in the same way as QtGui.QDialog()
In the if
statement, login_window = QtGui.QDialog()
is not needed and setpUi()
don't need ui
reference because you should use self
(replace login_window
occurences in setupUi
and retranslateUi
with self
).
Change Your code to this:
#!/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(QtGui.QDialog):
def setupUi(self):
self.setObjectName(_fromUtf8("login_window"))
self.resize(255, 150)
self.setMinimumSize(QtCore.QSize(255, 150))
self.setMaximumSize(QtCore.QSize(255, 150))
self.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(self)
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.widget = QtGui.QWidget(self)
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()
QtCore.QMetaObject.connectSlotsByName(self)
def retranslateUi(self):
self.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)
ui = Ui_login_window()
ui.setupUi()
ui.show()
sys.exit(app.exec_())
Answered By - Kenly
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.