2015-11-18 13 views
-1

Редактировать *: Я не понимаю, почему у меня есть минус, но я не выпускник I.T. Я сам изучаю кодирование. Много терминов для программирования я тоже не понимаю. Пожалуйста, будьте добры.Пытается закрыть и открыть новое окно с помощью PyQT (закрыто)

Я пытаюсь создать окно входа в систему с помощью 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

Если вы хотите использовать custum диалоги, наследуйте 'QtGui.QDialog'. – Zety

+0

Я здесь noob .. Не могли бы вы объяснить мне больше о наследовании QtGui.QDialog? Я не понимаю .. Я не выпускник I.T .. – Yoshua

ответ

0

Inheritance используется для указания того, что класс получит все функции своего родительского класса. Когда вы пишете 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 (заменить login_window в setupUi и retranslateUi с self).

изменить свой код к этому:

#!/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_()) 
+0

Привет, zetysz, спасибо за ваш ответ. Таким образом, изменить объект к этому: класс Ui_login_window (QtGui.QDialog): Защиту __init __ (Я): QtGui.QDialog .__ INIT __ (Я) Защиту setupUi (я, login_window): блаблабла включают кнопку отмены, чтобы закрыть приложения, но она все еще не работает. @ QtCore.pyqtSignature ("on_cancel_btn_clicked()") def Cancel_btn (self): self.close() – Yoshua

+0

@Yoshua проверить новое изменение. – Zety

Смежные вопросы