如何使用PyQT4从另一个Qt窗口打开Qt窗口?

时间:2016-05-08 21:01:43

标签: python windows qt python-2.7 pyqt

我需要创建一个具有注册功能的登录程序,但是我无法从主窗口打开注册窗口。我的目标是从主窗口代码中获取寄存器功能以打开寄存器窗口,以便我可以使用它。

我不知道为什么;我已经仔细搜索了这个地方的深处寻找答案但是我没有运气。

主窗口

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'mainwindow.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
from BlankSignup import SignUp

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 Login(object):
    def __init__(QtGui):
        usernames = ['a']
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.setEnabled(True)
        MainWindow.resize(243, 252)
        palette = QtGui.QPalette()
        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(0, 0, 0))
        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.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
        MainWindow.setPalette(palette)
        MainWindow.setTabShape(QtGui.QTabWidget.Triangular)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.gridLayoutWidget = QtGui.QWidget(self.centralwidget)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 10, 223, 189))
        self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
        self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.login_button = QtGui.QPushButton(self.gridLayoutWidget)
        palette = QtGui.QPalette()
        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(0, 0, 0))
        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.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
        self.login_button.setPalette(palette)
        self.login_button.setObjectName(_fromUtf8("login_button"))
        self.horizontalLayout.addWidget(self.login_button)
        self.register_button = QtGui.QPushButton(self.gridLayoutWidget)
        self.register_button.setDefault(False)
        self.register_button.setObjectName(_fromUtf8("register_button"))
        self.horizontalLayout.addWidget(self.register_button)
        self.gridLayout.addLayout(self.horizontalLayout, 4, 0, 1, 2)
        self.signin_password_label = QtGui.QLabel(self.gridLayoutWidget)
        self.signin_password_label.setObjectName(_fromUtf8("signin_password_label"))
        self.gridLayout.addWidget(self.signin_password_label, 3, 0, 1, 1)
        self.signin_password = QtGui.QLineEdit(self.gridLayoutWidget)
        self.signin_password.setObjectName(_fromUtf8("signin_password"))
        self.gridLayout.addWidget(self.signin_password, 3, 1, 1, 1)
        self.signin_username = QtGui.QLineEdit(self.gridLayoutWidget)
        self.signin_username.setObjectName(_fromUtf8("signin_username"))
        self.gridLayout.addWidget(self.signin_username, 2, 1, 1, 1)
        self.singin_username_label = QtGui.QLabel(self.gridLayoutWidget)
        self.singin_username_label.setObjectName(_fromUtf8("singin_username_label"))
        self.gridLayout.addWidget(self.singin_username_label, 2, 0, 1, 1)
        self.signin_title = QtGui.QLabel(self.gridLayoutWidget)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Small Fonts"))
        font.setPointSize(72)
        self.signin_title.setFont(font)
        self.signin_title.setObjectName(_fromUtf8("signin_title"))
        self.gridLayout.addWidget(self.signin_title, 1, 0, 1, 2)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 243, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)


        self.register_button.clicked.connect(self.register)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.login_button.setText(_translate("MainWindow", "Login", None))
        self.register_button.setText(_translate("MainWindow", "Register", None))
        self.signin_password_label.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:10pt; color:#ffffff;\">password:</span></p></body></html>", None))
        self.singin_username_label.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:10pt; color:#ffffff;\">username:</span></p></body></html>", None))
        self.signin_title.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" color:#ffffff;\">Blank</span></p></body></html>", None))
    def register(self):
         pass
    def login(self):
        pass
if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Login()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

注册窗口

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'signupwindowS.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 SignUp(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(243, 250)
        palette = QtGui.QPalette()
        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(0, 0, 0))
        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.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
        MainWindow.setPalette(palette)
        MainWindow.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        MainWindow.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.gridLayoutWidget = QtGui.QWidget(self.centralwidget)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 10, 222, 191))
        self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
        self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.signup_username = QtGui.QLineEdit(self.gridLayoutWidget)
        self.signup_username.setObjectName(_fromUtf8("signup_username"))
        self.gridLayout.addWidget(self.signup_username, 2, 1, 1, 1)
        self.signup_password_label = QtGui.QLabel(self.gridLayoutWidget)
        self.signup_password_label.setObjectName(_fromUtf8("signup_password_label"))
        self.gridLayout.addWidget(self.signup_password_label, 3, 0, 1, 1)
        self.signup_password = QtGui.QLineEdit(self.gridLayoutWidget)
        self.signup_password.setObjectName(_fromUtf8("signup_password"))
        self.gridLayout.addWidget(self.signup_password, 3, 1, 1, 1)
        self.signup_confirmpassword = QtGui.QLineEdit(self.gridLayoutWidget)
        self.signup_confirmpassword.setObjectName(_fromUtf8("signup_confirmpassword"))
        self.gridLayout.addWidget(self.signup_confirmpassword, 4, 1, 1, 1)
        self.signup_confirmpassword_label = QtGui.QLabel(self.gridLayoutWidget)
        self.signup_confirmpassword_label.setTextFormat(QtCore.Qt.AutoText)
        self.signup_confirmpassword_label.setObjectName(_fromUtf8("signup_confirmpassword_label"))
        self.gridLayout.addWidget(self.signup_confirmpassword_label, 4, 0, 1, 1)
        self.signup_username_label = QtGui.QLabel(self.gridLayoutWidget)
        self.signup_username_label.setObjectName(_fromUtf8("signup_username_label"))
        self.gridLayout.addWidget(self.signup_username_label, 2, 0, 1, 1)
        self.signup_title = QtGui.QLabel(self.gridLayoutWidget)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Small Fonts"))
        font.setPointSize(72)
        self.signup_title.setFont(font)
        self.signup_title.setObjectName(_fromUtf8("signup_title"))
        self.gridLayout.addWidget(self.signup_title, 0, 0, 1, 2)
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.submit_button = QtGui.QPushButton(self.gridLayoutWidget)
        palette = QtGui.QPalette()
        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(0, 0, 0))
        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.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
        self.submit_button.setPalette(palette)
        self.submit_button.setObjectName(_fromUtf8("submit_button"))
        self.horizontalLayout.addWidget(self.submit_button)
        self.cancle_button = QtGui.QPushButton(self.gridLayoutWidget)
        self.cancle_button.setDefault(False)
        self.cancle_button.setObjectName(_fromUtf8("cancle_button"))
        self.horizontalLayout.addWidget(self.cancle_button)
        self.gridLayout.addLayout(self.horizontalLayout, 5, 0, 1, 2)
        self.signup_subtitle = QtGui.QLabel(self.gridLayoutWidget)
        self.signup_subtitle.setObjectName(_fromUtf8("signup_subtitle"))
        self.gridLayout.addWidget(self.signup_subtitle, 1, 0, 1, 2)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 243, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "Blank", None))
        self.signup_password_label.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" color:#ffffff;\">new password</span></p></body></html>", None))
        self.signup_confirmpassword_label.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" color:#ffffff;\">confirm password</span></p></body></html>", None))
        self.signup_username_label.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" color:#ffffff;\">new username</span></p></body></html>", None))
        self.signup_title.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-size:24pt; color:#ffffff;\">Blank sign up</span></p></body></html>", None))
        self.submit_button.setText(_translate("MainWindow", "Submit", None))
        self.cancle_button.setText(_translate("MainWindow", "Cancel", None))
        self.signup_subtitle.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-size:12pt; color:#ffffff;\">put in information below</span></p></body></html>", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = SignUp()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

def registerFunction():
    s = SignUp()
    s.show()
    return s

编辑:

在函数的第一行,我们创建了SignUp表单的实例 然后调用其成员槽函数show()来显示表单,在函数末尾我们返回SignUp表单的实例。

现在当你调用这个registerFunction()时,你会注意到SignUp窗口正在立即关闭,因为我们在该函数内创建的SignUp实例在超出范围时会自动销毁

registerFunction() 
//destroyed, out of scope

要防止这种情况发生,你必须保留一个引用此函数的类的变量。

(假设我们将其命名为holder) 现在我们做:

holder = registerFunction()