连接2个小部件,PyQt

时间:2016-02-20 11:59:38

标签: python pyqt4 qt-designer

在将2个窗口连接在一起而不破坏主循环时出现问题。你能帮我连接这2个PyQt文件吗?按下按钮即可打开第二个文件。

# usr/bin/env python

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



from PyQt4 import QtCore, QtGui
# from Monster import aknator

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)


def Aknator():
   from Monster import aknator







class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(1695, 885)
        self.gridLayout = QtGui.QGridLayout(Form)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.commandLinkButton = QtGui.QCommandLinkButton(Form)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Segoe UI"))
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.commandLinkButton.setFont(font)
        self.commandLinkButton.setMouseTracking(False)
        self.commandLinkButton.setAccessibleDescription(_fromUtf8(""))
        self.commandLinkButton.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.commandLinkButton.setAutoFillBackground(False)

        self.retranslateUi(Form)
        QtCore.QObject.connect(self.commandLinkButton, QtCore.SIGNAL(_fromUtf8("clicked()")), Aknator)

        QtCore.QMetaObject.connectSlotsByName(Form)
        Form.setTabOrder(self.commandLinkButton, #Future comadndlinks)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Monster Hunter 4 Ultimate Pro", None))
        self.commandLinkButton.setText(_translate("Form", "Akantor", 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_())

这是第二个文件:

# usr/bin/env python

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



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(1096, 399)
        Form.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.gridLayout = QtGui.QGridLayout(Form)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.verticalLayout_5 = QtGui.QVBoxLayout()
        self.verticalLayout_5.setObjectName(_fromUtf8("verticalLayout_5"))
        self.label = QtGui.QLabel(Form)
        font = QtGui.QFont()
        font.setPointSize(20)
        self.label.setFont(font)
        self.label.setObjectName(_fromUtf8("label"))
        self.verticalLayout_5.addWidget(self.label)
        self.textEdit = QtGui.QTextEdit(Form)
        self.textEdit.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.verticalLayout_5.addWidget(self.textEdit)
        self.gridLayout.addLayout(self.verticalLayout_5, 0, 1, 1, 1)
        self.verticalLayout_8 = QtGui.QVBoxLayout()
        self.verticalLayout_8.setObjectName(_fromUtf8("verticalLayout_8"))
        self.pushButton_5 = QtGui.QPushButton(Form)
        self.pushButton_5.setMaximumSize(QtCore.QSize(550, 200))
        font = QtGui.QFont()
        font.setPointSize(26)
        self.pushButton_5.setFont(font)
        self.pushButton_5.setAutoFillBackground(False)
        self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
        self.verticalLayout_8.addWidget(self.pushButton_5)
        self.gridLayout.addLayout(self.verticalLayout_8, 2, 2, 1, 1)
        self.verticalLayout_7 = QtGui.QVBoxLayout()
        self.verticalLayout_7.setObjectName(_fromUtf8("verticalLayout_7"))
        self.label_2 = QtGui.QLabel(Form)
        font = QtGui.QFont()
        font.setPointSize(20)
        self.label_2.setFont(font)
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.verticalLayout_7.addWidget(self.label_2)
        self.textEdit_2 = QtGui.QTextEdit(Form)
        self.textEdit_2.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
        self.textEdit_2.setObjectName(_fromUtf8("textEdit_2"))
        self.verticalLayout_7.addWidget(self.textEdit_2)
        self.gridLayout.addLayout(self.verticalLayout_7, 0, 2, 1, 1)
        self.verticalLayout_6 = QtGui.QVBoxLayout()
        self.verticalLayout_6.setObjectName(_fromUtf8("verticalLayout_6"))
        self.gridLayout.addLayout(self.verticalLayout_6, 2, 1, 1, 1)

        self.retranslateUi(Form)
        QtCore.QObject.connect(self.pushButton_5, QtCore.SIGNAL(_fromUtf8("clicked()")), drops)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Monster Hunter 4 Ultimate Pro", None))
        self.label.setText(_translate("Form", "Weapon Damage", None))
        self.textEdit.setHtml(_translate("Form", "Not Important HTML Code", 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)

假设您的文件名为a.pyb.py,请将b导入到其中创建新的对象调用show

a.py

# usr/bin/env python

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



from PyQt4 import QtCore, QtGui
from b import Ui_Form2

# from Monster import aknator

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)


def Aknator():
   from Monster import aknator



class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(1695, 885)
        self.gridLayout = QtGui.QGridLayout(Form)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.commandLinkButton = QtGui.QCommandLinkButton(Form)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Segoe UI"))
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.commandLinkButton.setFont(font)
        self.commandLinkButton.setMouseTracking(False)
        self.commandLinkButton.setAccessibleDescription(_fromUtf8(""))
        self.commandLinkButton.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.commandLinkButton.setAutoFillBackground(False)

        self.page2 = QtGui.QWidget()
        ui = Ui_Form2()
        ui.setupUi(self.page2)

        self.commandLinkButton.clicked.connect(self.page2.show)

        self.retranslateUi(Form)
        QtCore.QObject.connect(self.commandLinkButton, QtCore.SIGNAL(_fromUtf8("clicked()")), Aknator)

        QtCore.QMetaObject.connectSlotsByName(Form)
        #Form.setTabOrder(self.commandLinkButton)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Monster Hunter 4 Ultimate Pro", None))
        self.commandLinkButton.setText(_translate("Form", "Akantor", 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_())

b.py

# usr/bin/env python

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



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_Form2(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(1096, 399)
        Form.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.gridLayout = QtGui.QGridLayout(Form)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.verticalLayout_5 = QtGui.QVBoxLayout()
        self.verticalLayout_5.setObjectName(_fromUtf8("verticalLayout_5"))
        self.label = QtGui.QLabel(Form)
        font = QtGui.QFont()
        font.setPointSize(20)
        self.label.setFont(font)
        self.label.setObjectName(_fromUtf8("label"))
        self.verticalLayout_5.addWidget(self.label)
        self.textEdit = QtGui.QTextEdit(Form)
        self.textEdit.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.verticalLayout_5.addWidget(self.textEdit)
        self.gridLayout.addLayout(self.verticalLayout_5, 0, 1, 1, 1)
        self.verticalLayout_8 = QtGui.QVBoxLayout()
        self.verticalLayout_8.setObjectName(_fromUtf8("verticalLayout_8"))
        self.pushButton_5 = QtGui.QPushButton(Form)
        self.pushButton_5.setMaximumSize(QtCore.QSize(550, 200))
        font = QtGui.QFont()
        font.setPointSize(26)
        self.pushButton_5.setFont(font)
        self.pushButton_5.setAutoFillBackground(False)
        self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
        self.verticalLayout_8.addWidget(self.pushButton_5)
        self.gridLayout.addLayout(self.verticalLayout_8, 2, 2, 1, 1)
        self.verticalLayout_7 = QtGui.QVBoxLayout()
        self.verticalLayout_7.setObjectName(_fromUtf8("verticalLayout_7"))
        self.label_2 = QtGui.QLabel(Form)
        font = QtGui.QFont()
        font.setPointSize(20)
        self.label_2.setFont(font)
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.verticalLayout_7.addWidget(self.label_2)
        self.textEdit_2 = QtGui.QTextEdit(Form)
        self.textEdit_2.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
        self.textEdit_2.setObjectName(_fromUtf8("textEdit_2"))
        self.verticalLayout_7.addWidget(self.textEdit_2)
        self.gridLayout.addLayout(self.verticalLayout_7, 0, 2, 1, 1)
        self.verticalLayout_6 = QtGui.QVBoxLayout()
        self.verticalLayout_6.setObjectName(_fromUtf8("verticalLayout_6"))
        self.gridLayout.addLayout(self.verticalLayout_6, 2, 1, 1, 1)

        self.retranslateUi(Form)
        #QtCore.QObject.connect(self.pushButton_5, QtCore.SIGNAL(_fromUtf8("clicked()")), drops)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Monster Hunter 4 Ultimate Pro", None))
        self.label.setText(_translate("Form", "Weapon Damage", None))
        self.textEdit.setHtml(_translate("Form", "Not Important HTML Code", None))

我必须注释掉你的一些代码以使其运行,因此请确保在发布问题之前运行Stackoverflow上粘贴的代码

我建议你不要继续这样做,你应该学会相应地构建你的项目,而不是从qt designer

修改自动生成的ui文件

查看此链接以帮助您组织PyQt项目Saving connect statements pyqt when UI is still changing

我的github回购,它的PySide但概念仍然相同

https://github.com/danidee10/Maven