Python构建应用程序

2024-04-20 12:14:57 发布

您现在位置:Python中文网/ 问答频道 /正文

所以问题是: 我正在pyqt中构建一个应用程序:

  1. 框架1.py

    from PyQt5 import QtWidgets, QtCore, QtGui
    import runpy, sys
    
    class LoginFrame(QtWidgets.QWidget):
    
        def __init__(self):
            super().__init__()
            self.logFrameSetup(self)
    
        def logFrameSetup(self, GuiWin):
                GuiWin.setWindowTitle('GuiWin')
                GuiWin.resize(450, 215)
    
                self.pushbutton = QtWidgets.QPushButton('Go To', GuiWin)
                self.pushbutton.clicked.connect(self.change)
    
        def change(self):
            try:
                runpy.run_path('frame2.py', run_name="__main__")
            except:
                pass
            finally:
                self.close()
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        ex = LoginFrame()
        ex.show()
        x = app.exec_()
        sys.exit(x)
    
  2. 框架2.py

     from PyQt5 import QtCore, QtGui, QtWidgets
     import sys
     class MainTradingPlatform(QtWidgets.QMainWindow):
    
     def __init__(self):
        super().__init__()
    
        self.setupFrame(self)
    
     def setupFrame(self, frame2):
         frame2.setWindowTitle('2frame')
         frame2.resize(1200, 1000)
    
    
    if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ex = MainTradingPlatform()
    ex.show()
    x = app.exec_()
    sys.exit(x)
    

现在这几乎完美了,但当我关闭frame2.py时,后台进程仍在进行/运行中。
我想要实现的是,通过关闭frame2.py,这个过程以退出代码结束。(已终止)

ps:在调用frame2.py之后,我还想终止frame1。你知道吗

谢谢你的帮助,很抱歉我的回答。你知道吗


Tags: namefrompyimportself框架appinit
1条回答
网友
1楼 · 发布于 2024-04-20 12:14:57

使用runpy执行代码时,进程与初始应用程序分离,关闭第二个应用程序的选项是覆盖closeEvent方法。你知道吗

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class MainTradingPlatform(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupFrame(self)

    def setupFrame(self, frame2):
        frame2.setWindowTitle('2frame')
        frame2.resize(1200, 1000)

    def closeEvent(self, e):
        sys.exit(0)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ex = MainTradingPlatform()
    ex.show()
    sys.exit(app.exec_())

相关问题 更多 >