使用Python 3中的QTDesigner 5打开第二个窗口

时间:2017-06-19 19:52:08

标签: python class user-interface window pyqt5

我对QTDesigner 5有问题,这应该是微不足道的,但我无法弄清楚问题。

我想要做的是在点击按钮时打开第二个窗口: 我用QTDesigner(PyQT5!)设计了主窗口和辅助窗口,并用pyuic转换为.py文件。主窗口打开时没有出现以下代码问题:

from PyQt5 import QtGui, QtWidgets, QtCore, uic
import UI14 as UIImport
import GIPrompt as GIImport

     class MainWindow(UIImport.Ui_MainWindow):
          def __init__(self, window):
               UIImport.Ui_MainWindow.__init__(self)   
               self.setupUi(window)
               self.radioButtonGI.clicked.connect(self.openGIPrompt)

          def openGIPrompt(self):
              windowGI = QtWidgets.QDialog()
              Gi = GIPrompt(windowGI)
              windowGI.show()


     class GIPrompt(GIImport.Ui_GIPrompt):
          def __init__(self, windowGI):
               GIImport.Ui_GIPrompt.__init__(self)   
               self.setupUi(windowGI)

if __name__ == '__main__':


app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
prog = MainWindow(window)

window.show()
sys.exit(app.exec_())

如果我将以下内容添加到主函数中," GiPrompt"窗口以及主窗口也会打开:

if __name__ == '__main__':

app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
prog = MainWindow(window)

window.show()

"""Open secondWindow"""
windowGI = QtWidgets.QDialog()
Gi = GIPrompt(windowGI)
windowGI.show()

sys.exit(app.exec_())

如果我尝试通过openGIPrompt函数打开第二个窗口,则没有任何反应。我没有收到错误消息,也没有出现窗口。然而,打印命令告诉我第二个窗口的init_function被调用...

有人知道,问题可能是什么?

提前致谢!

1 个答案:

答案 0 :(得分:0)

我已经找到了问题: 显然,初始化的Window由垃圾收集处理,因为变量未声明为self:

这解决了问题:

from PyQt5 import QtGui, QtWidgets, QtCore, uic
import UI14 as UIImport
import GIPrompt as GIImport

     class MainWindow(UIImport.Ui_MainWindow):
          windowGI=None
          Gi=None
          def __init__(self, window):
               UIImport.Ui_MainWindow.__init__(self)   
               self.setupUi(window)
               self.radioButtonGI.clicked.connect(self.openGIPrompt)

          def openGIPrompt(self):
              self.windowGI = QtWidgets.QDialog()
              self.Gi = GIPrompt(self.windowGI)
              self.windowGI.show()


     class GIPrompt(GIImport.Ui_GIPrompt):
          def __init__(self, windowGI):
               GIImport.Ui_GIPrompt.__init__(self)   
               self.setupUi(windowGI)

if __name__ == '__main__':


app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
prog = MainWindow(window)

window.show()
sys.exit(app.exec_())