在pyqt4中,如何从现有窗口打开新窗口?

时间:2016-04-10 14:02:02

标签: python pyqt

我想从pyqt中的现有窗口打开新窗口。

我的源代码有两个功能。

  • 一个功能:单击按钮时,将打开“文件对话框”。 另一个功能:当点击另一个按钮时,New Window就是 打开。

但是当我想要新窗口时,会出现错误消息:

回溯(最近一次调用最后一次):文件“C:\ Users \ Han \ Desktop \ project \ prototype \ newwindow.py”,第61行,在check_start中自我.w.show()属性错误:'Ui_dialog'对象有没有属性'show'

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_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(518, 349)
        self.label = QtGui.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(40, 40, 141, 16))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(400, 330, 141, 16))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.horizontalLayoutWidget = QtGui.QWidget(Form)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(70, 200, 381, 31))            self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
        self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.textEdit = QtGui.QTextEdit(self.horizontalLayoutWidget)
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.horizontalLayout.addWidget(self.textEdit)
        self.pushButton = QtGui.QPushButton(self.horizontalLayoutWidget)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.horizontalLayout.addWidget(self.pushButton)
        self.pushButton_2 = QtGui.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(230, 270, 75, 23))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))

        self.retranslateUi(Form)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.file_open)
        QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), self.check_start)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Off_Strip", None))
        self.label.setText(_translate("Form", "123", None))
        self.label_2.setText(_translate("Form", "@Copyright MokLab", None))
        self.pushButton.setText(_translate("Form", "123", None))
        self.pushButton_2.setText(_translate("Form", "123!", None))

    def file_open(self):
        fname = QtGui.QFileDialog.getOpenFileName()
        f = open(fname, 'r')
        f.close()

    def check_start(self):
        self.w = Ui_dialog()
        self.w.show()

class Ui_dialog(object):
    def setupUi(self, dialog):
        dialog.setObjectName(_fromUtf8("dialog"))
        dialog.resize(400, 300)
        self.pushButton = QtGui.QPushButton(dialog)
        self.pushButton.setGeometry(QtCore.QRect(190, 130, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.radioButton = QtGui.QRadioButton(dialog)
        self.radioButton.setGeometry(QtCore.QRect(30, 70, 90, 16))
        self.radioButton.setObjectName(_fromUtf8("radioButton"))
        self.radioButton_2 = QtGui.QRadioButton(dialog)
        self.radioButton_2.setGeometry(QtCore.QRect(40, 110, 90, 16))
        self.radioButton_2.setObjectName(_fromUtf8("radioButton_2"))

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

    def retranslateUi(self, dialog):
        dialog.setWindowTitle(_translate("dialog", "Form", None))
        self.pushButton.setText(_translate("dialog", "PushButton", None))
        self.radioButton.setText(_translate("dialog", "RadioButton", None))
        self.radioButton_2.setText(_translate("dialog", "RadioButton", None))

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

您已将self.w.show声明为w对象的行Ui_dialog将尝试在show内调用Ui_dialog实例变量,因为你从未定义任何名为show的函数,所以不起作用。

对于课程object,您继承Ui_dialog没有任何帮助。您应该继承该类的QtGui.QDialog,并相应地重写您的类。