使用pyqt框架从python中的另一个窗口打开一个窗口

时间:2015-01-30 09:02:45

标签: python user-interface pyqt

我正在尝试在点击按钮时从另一个pyqt窗口打开一个pyqt窗口,但我无法抓住它。 python文件opens.py和signup.py都可以独立运行,但我无法想办法链接它们......(点击注册按钮后从opens.py运行signup.py)

opening.py

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

# Form implementation generated from reading ui file 'opening.ui'
#
# Created: Tue Jan 20 00:19:45 2015
#      by: PyQt4 UI code generator 4.10.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
#from signup import Ui_Dialog1

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_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(290, 237)
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(70, 19, 151, 41))
        self.label.setObjectName(_fromUtf8("label"))
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(40, 170, 201, 41))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton_2 = QtGui.QPushButton(Dialog)
        self.pushButton_2.setGeometry(QtCore.QRect(40, 100, 201, 41))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.pushButton.clicked.connect(self.handleTest1)
        self.pushButton_2.clicked.connect(self.handleTest2)

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

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.label.setText(_translate("Dialog", "MAIL SERVER", None))
        self.pushButton.setText(_translate("Dialog", "SIGN UP", None))
        self.pushButton_2.setText(_translate("Dialog", "LOGIN", None))

    def handleTest1(self):

        #self.accept()       
        #self.h=Ui_Dialog1()
        #self.h.setupUi(QtGui.QDialog())        
        pass

    def handleTest2(self):
        execfile('login.py')

import sys

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

和其他在点击注册按钮后打开的文件(登录按钮现在不起作用)

signup.py

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

# Form implementation generated from reading ui file 'abc.ui'
#
# Created: Mon Jan 19 23:35:37 2015
#      by: PyQt4 UI code generator 4.10.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_Dialog1(object):
    def setupUi(self, Dialog):
        print "16"        

        Dialog.setObjectName(_fromUtf8("SignUp"))
        Dialog.resize(415, 364)
        self.SignUp = QtGui.QDialogButtonBox(Dialog)
        self.SignUp.setGeometry(QtCore.QRect(130, 310, 176, 27))
        self.SignUp.setOrientation(QtCore.Qt.Horizontal)
        self.SignUp.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)  
        self.SignUp.setObjectName(_fromUtf8("SignUp"))
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(50, 50, 91, 21))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(50, 110, 66, 17))
        self.label_2.setText(_fromUtf8(""))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.label_3 = QtGui.QLabel(Dialog)
        self.label_3.setGeometry(QtCore.QRect(50, 100, 66, 17))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.label_4 = QtGui.QLabel(Dialog)
        self.label_4.setGeometry(QtCore.QRect(50, 150, 66, 17))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.label_5 = QtGui.QLabel(Dialog)
        self.label_5.setGeometry(QtCore.QRect(50, 200, 121, 17))
        self.label_5.setObjectName(_fromUtf8("label_5"))
        self.label_6 = QtGui.QLabel(Dialog)
        self.label_6.setGeometry(QtCore.QRect(50, 250, 91, 17))
        self.label_6.setObjectName(_fromUtf8("label_6"))
        self.lineEdit_3 = QtGui.QLineEdit(Dialog)
        self.lineEdit_3.setGeometry(QtCore.QRect(200, 140, 181, 27))
        self.lineEdit_3.setObjectName(_fromUtf8("lineEdit_3"))
        self.lineEdit_4 = QtGui.QLineEdit(Dialog)
        self.lineEdit_4.setGeometry(QtCore.QRect(200, 190, 181, 27))
        self.lineEdit_4.setObjectName(_fromUtf8("lineEdit_4"))
        self.lineEdit_5 = QtGui.QLineEdit(Dialog)
        self.lineEdit_5.setGeometry(QtCore.QRect(200, 90, 181, 27))
        self.lineEdit_5.setObjectName(_fromUtf8("lineEdit_5"))
        self.lineEdit_6 = QtGui.QLineEdit(Dialog)
        self.lineEdit_6.setGeometry(QtCore.QRect(200, 40, 181, 27))
        self.lineEdit_6.setObjectName(_fromUtf8("lineEdit_6"))
        self.lineEdit_7 = QtGui.QLineEdit(Dialog)
        self.lineEdit_7.setGeometry(QtCore.QRect(200, 240, 181, 27))
        self.lineEdit_7.setObjectName(_fromUtf8("lineEdit_7"))

        self.retranslateUi(Dialog)
        QtCore.QObject.connect(self.SignUp, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
        QtCore.QObject.connect(self.SignUp, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
        print "17"
        Dialog.show()

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("SignUp", "SignUp", None))
        self.label.setText(_translate("Dialog", "Name", None))
        self.label_3.setText(_translate("Dialog", "E-mail", None))
        self.label_4.setText(_translate("Dialog", "Password", None))
        self.label_5.setText(_translate("Dialog", "Confirm Password", None))
        self.label_6.setText(_translate("Dialog", "Mobile (+91)", None))


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

opens.py中的注释部分是创建问题的部分。帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

你的Ui_Dialog1应该继承QDialog,它使它成为一个UI对象,但是你继承自object,它没有" show"方法

def handleTest1(self):

        #self.accept()
        #self.h=Ui_Dialog1()     # You need to move this to __init__ function
        #self.h.setupUi(QtGui.QDialog()) # You need to move this to __init__ function
        self.h.show()  # call the show method here

您需要将self.h = Ui_Dialog1()和setupUi移动到 init 函数,这样可以避免可能的内存泄漏。 (否则每次点击" sigunup",它都会创建一个新对象)