在qt中关闭一个窗口并在python中打开另一个窗口

时间:2014-04-22 20:17:23

标签: python qt pyqt pyqt4

我试图关闭一个屏幕并打开另一个屏幕,但我一直收到错误:

AttributeError: 'Ui_MainMenu' object has no attribute 'show'

代码是:

class Something: 

    def Something(self):
        self.retranslateUi(Login)
        QtCore.QObject.connect(self.ExitButton, QtCore.SIGNAL(_fromUtf8("clicked()")), Login.close)
        QtCore.QObject.connect(self.LoginButton, QtCore.SIGNAL("clicked()"),self.get_value_from_user)
        QtCore.QMetaObject.connectSlotsByName(Login)
        Login.setTabOrder(self.LoginButton, self.UsernameEdit)
        Login.setTabOrder(self.UsernameEdit, self.PasswordEdit)
        Login.setTabOrder(self.PasswordEdit, self.ExitButton)


    def get_value_from_user(self):
        Correct_details = False
        while Correct_details==False:
            inputusername=self.UsernameEdit.text()
            inputpassword=self.PasswordEdit.text()
            cur.execute("SELECT password FROM tblStaff WHERE username='"+inputusername+"'")
            password=str(cur.fetchone())
            password=password[2:-3]
            cur.execute("SELECT firstname FROM tblStaff WHERE username='"+inputusername+"'")
            FirstName=str(cur.fetchone())
            FirstName=FirstName[2:-3]
            cur.execute("SELECT surname FROM tblStaff WHERE username='"+inputusername+"'")
            Surname=str(cur.fetchone())
            Surname=Surname[2:-3]
            if str(password) == str(inputpassword):
                self.msgBox1 = QMessageBox(QMessageBox.Information,'Successful', "Succesfully Logged in")
                self.msgBox1.show()
                Correct_details = True
            else:
                self.msgBox2 = QMessageBox(QMessageBox.Information, 'Warning', "The Username or Password you have entered is incorrect - Please try again (CASE SENSITIVE)")
                self.msgBox2.show()
                self.UsernameEdit.clear()
                self.PasswordEdit.clear()
                break

            self.msgBox1 = QMessageBox(QMessageBox.Information,'Successful', "Succesfully Logged in")
            self.msgBox1.show()
            time.sleep(3)
            self.run_mainmenu()

    def run_mainmenu(self):
        self.mainmenu= Ui_MainMenu()
        Login.close()
        self.mainmenu.show()

    def retranslateUi(self, Login):
        Login.setWindowTitle(_translate("Login", "Login", None))
        self.UsernameEdit.setPlaceholderText(_translate("Login", "Username", None))
        self.PasswordEdit.setPlaceholderText(_translate("Login", "Password", None))
        self.ExitButton.setText(_translate("Login", "Exit", None))
        self.LoginButton.setText(_translate("Login", "Login", None))
        self.LoginTitle.setText(_translate("Login", "Login", None))
        self.label.setText(_translate("Login", "<html><head/><body><p align=\"center\"><span style=\" font-size:10pt; font-weight:600;\">STATION</span></p><p align=\"center\"><span style=\" font-size:10pt; font-weight:600;\">JEWELLERS </span></p></body></html>", None))

class Ui_MainMenu(object):
    def setupUi(self, MainMenu):
        MainMenu.setObjectName(_fromUtf8("MainMenu"))
        MainMenu.resize(401, 223)
        MainMenu.setStyleSheet(_fromUtf8("background-color: rgb(79, 129, 189);"))
        MainMenu.setSizeGripEnabled(False)
        self.MainMenuTitle = QtGui.QLabel(MainMenu)
        self.MainMenuTitle.setGeometry(QtCore.QRect(80, 0, 271, 76))
        self.MainMenuTitle.setMinimumSize(QtCore.QSize(100, 10))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Palatino Linotype"))
        font.setPointSize(28)
        font.setBold(True)
        font.setWeight(75)
        self.MainMenuTitle.setFont(font)
        self.MainMenuTitle.setObjectName(_fromUtf8("MainMenuTitle"))
        self.logo = QtGui.QLabel(MainMenu)
        self.logo.setGeometry(QtCore.QRect(10, 10, 81, 51))
        self.logo.setStyleSheet(_fromUtf8("background-color: rgb(232, 232, 232);"))
        self.logo.setObjectName(_fromUtf8("logo"))
        self.LogOutButton = QtGui.QPushButton(MainMenu)
        self.LogOutButton.setGeometry(QtCore.QRect(340, 10, 51, 20))
        self.LogOutButton.setStyleSheet(_fromUtf8("background-color: rgb(255, 255, 255);"))
        self.LogOutButton.setObjectName(_fromUtf8("LogOutButton"))
        self.Changepassword = QtGui.QPushButton(MainMenu)
        self.Changepassword.setGeometry(QtCore.QRect(10, 90, 121, 41))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.Changepassword.setFont(font)
        self.Changepassword.setStyleSheet(_fromUtf8("background-color: rgb(255, 255, 255);"))
        self.Changepassword.setObjectName(_fromUtf8("Changepassword"))
        self.Staff = QtGui.QPushButton(MainMenu)
        self.Staff.setGeometry(QtCore.QRect(140, 90, 121, 41))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.Staff.setFont(font)
        self.Staff.setStyleSheet(_fromUtf8("background-color: rgb(255, 255, 255);"))
        self.Staff.setObjectName(_fromUtf8("Staff"))
        self.Customer = QtGui.QPushButton(MainMenu)
        self.Customer.setGeometry(QtCore.QRect(270, 90, 121, 41))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.Customer.setFont(font)
        self.Customer.setStyleSheet(_fromUtf8("background-color: rgb(255, 255, 255);"))
        self.Customer.setObjectName(_fromUtf8("Customer"))
        self.Repair = QtGui.QPushButton(MainMenu)
        self.Repair.setGeometry(QtCore.QRect(80, 150, 121, 41))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.Repair.setFont(font)
        self.Repair.setStyleSheet(_fromUtf8("background-color: rgb(255, 255, 255);"))
        self.Repair.setObjectName(_fromUtf8("Repair"))
        self.sale = QtGui.QPushButton(MainMenu)
        self.sale.setGeometry(QtCore.QRect(210, 150, 121, 41))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.sale.setFont(font)
        self.sale.setStyleSheet(_fromUtf8("background-color: rgb(255, 255, 255);"))
        self.sale.setObjectName(_fromUtf8("sale"))

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

    def retranslateUi(self, MainMenu):
        MainMenu.setWindowTitle(_translate("MainMenu", "MainMenu", None))
        self.MainMenuTitle.setText(_translate("MainMenu", "<html><head/><body><p align=\"center\">Main Menu</p></body></html>", None))
        self.logo.setText(_translate("MainMenu", "<html><head/><body><p align=\"center\"><span style=\" font-size:10pt; font-weight:600;\">STATION</span></p><p align=\"center\"><span style=\" font-size:10pt; font-weight:600;\">JEWELLERS </span></p></body></html>", None))
        self.LogOutButton.setText(_translate("MainMenu", "Logout", None))
        self.Changepassword.setText(_translate("MainMenu", "CHANGE PASSWORD", None))
        self.Staff.setText(_translate("MainMenu", "STAFF", None))
        self.Customer.setText(_translate("MainMenu", "CUSTOMERS", None))
        self.Repair.setText(_translate("MainMenu", "REPAIR", None))
        self.sale.setText(_translate("MainMenu", "SALE", None))

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Login = QtGui.QDialog()
    ui = Ui_Login()
    ui.setupUi(Login)
    Login.show()
    sys.exit(app.exec_())

2 个答案:

答案 0 :(得分:1)

您可以修改run_mainmenu上的Something并执行与在应用开始时初始化Login窗口时相同的操作。

def run_mainmenu(self):
    Login.close()
    self.ui_mainmenu= Ui_MainMenu()
    self.mainmenu = QWidget() #or QDialog?
    self.ui_mainmenu.setupUi(self.mainmenu)
    self.mainmenu.show()

另一种方法是创建一个新的类,如MainMenu,并继承Ui_MainMenuQWidget(是的,使用多继承!),并在类构造函数中执行类似的操作:

def __init__(self):
    self.setupUi(self)

然后你只需稍微修改你的功能:

def run_mainmenu(self):
    Login.close()
    self.ui_mainmenu= MainMenu()
    self.mainmenu.show()

请记住,Qt生成的Ui类是一个初始化你的ui的实用程序类,它也可以作为ui小部件的容器。 我更喜欢我提出的第一种方式,因为这样可以保持生成的代码被隔离。这两种方法都很常见,使用你感觉更舒服的方法。

答案 1 :(得分:0)

您从Ui_MainMenu派生object,这不是Qt小部件,因此它没有show()方法。从QWidgetQDialog或类似物品中获取。