2016-02-20 75 views
0

我有一个问题连接2个窗口在一起没有破坏主循环。你能帮我连接这两个PyQt文件吗?第二个文件应该按下按钮打开。连接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_()) 

回答

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)) 

我不得不注释掉一些代码,使其运行,所以一定要确保你#2粘贴代码,发布您的问题之前运行

我不会建议你继续这样你应该学会从Qt设计

修改自动生成的UI文件来组织你的项目相应的,而不是来看看这个链接,可以帮助你组织你的PyQt的项目Saving connect statements pyqt when UI is still changing

我的GitHub库,它是PySide但概念是stil l相同

https://github.com/danidee10/Maven

+0

好的。谢谢你非常mutch –

+0

嗨@PericaMatic如果这个答案解决了你的问题,请考虑接受它通过点击复选标记。这向更广泛的社区表明,您已经找到了解决方案,并为答复者和您自己提供了一些声誉。没有义务这样做。 – danidee