如何显示另一个.py文件调用的window?

How to show the window which is called out by another .py file?

我有两个 .py 文件,分别是 simple_1.pysimple_2.py。 如何在点击simple_1.py中的按钮时显示simple_2.py的window?
同样,点击simple_2.py中的按钮时,如何显示simple_1.py中的window?
当 window 被另一个 .py 文件调用时,现在这个 window 必须同时关闭。
这是我的 simple_1.py 代码:

# -*- coding: utf-8 -*- 
#simple_1.py

import sys
from PyQt4 import QtCore, QtGui 
from simple import  Ui_Form
class StartQT4(QtGui.QWidget): 
    def __init__(self, parent=None): 
        QtGui.QWidget.__init__(self, parent)                                            
        self.ui = Ui_Form()            
        self.ui.setupUi(self) 

        self.button() 

    def button(self):
        button1= QtGui.QPushButton('show simple_2', self)
        button1.setGeometry(80, 80,100, 50) 
        self.connect(button1, QtCore.SIGNAL('clicked()'),
            self.buttonClicked)

    def buttonClicked(self):
        #to show window of simple_2.py 


if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 
    myapp = StartQT4() 
    myapp.show() 
    sys.exit(app.exec_())

这是我的 simple_2.py 代码:

# -*- coding: utf-8 -*- 
#simple_2.py

import sys
from PyQt4 import QtCore, QtGui 


class Apple(QtGui.QWidget):
    def __init__(self,parent=None):
        super().__init__()
        self.widget = QtGui.QWidget()
        self.resize(250, 150)
        self.setWindowTitle('simple2')
        self.button() 


    def button(self):
        button1= QtGui.QPushButton('show simple_1', self)
        button1.setGeometry(80, 80, 100, 50) 
        self.connect(button1, QtCore.SIGNAL('clicked()'),
            self.buttonClicked)

    def buttonClicked(self):
         #to show window of simple_1.py 


if __name__ == "__main__":         
    app = QtGui.QApplication(sys.argv)
    mywidget = Apple()
    mywidget.show()
    sys.exit(app.exec_())

您的代码中有几个错误导致无法使用您从 运行ning 发布的示例。我会忽略它们并回答你的实际问题,据我所知是这样的:

如何让我的两个 Qt 小部件创建并显示彼此的实例?

首先,我建议将小部件更改为 QDialogs,它具有方便的 exec_() 方法。您可以通过从 QtGui.QDialog 继承而不是像这样从 QtGui.QWidget 继承来做到这一点:

class Apple(QtGui.QDialog):

接下来您需要做的是在按钮回调中导入和 运行 您的自定义 QDialogs。

def buttonClicked(self):
    from simple_1 import StartQT4  # imports your dialog from your other file
    sqt = StartQT4()  # creates an instance of it
    self.close()  # closes the current dialog
    sqt.exec_()  # runs the newly created dialog