1

When I close an application window in PyQt the console is still left running in the background and python.exe process is present until I close the console. I think the sys.exit(app.exec_()) is not able to operate properly.

Mainscript (which opens Firstwindow):

if __name__ == '__main__':
    from firstwindow import main
    main()

Firstwindow

On button press:

    self.close() #close firstprogram
    Start() #function to open mainprogram

Start():

def Start():
        global MainWindow
        MainWindow = QtWidgets.QMainWindow()
        ui = genui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()

main() (suggested here):

def main_window():
     return form

def main():
    global form
    app = QtWidgets.QApplication(sys.argv)  
    form = MyApp()  
    form.show()
    app.exec_()
    sys.exit(app.exec_())
4

1 回答 1

2

问题是您exec_()在函数中调用了两次main()

def main():
    global form
    app = QtWidgets.QApplication(sys.argv)  
    form = MyApp()  
    form.show()
    app.exec_()
    sys.exit(app.exec_())

第一app.exec_()行将启动一个事件循环,这意味着main()当您与 gui 交互时该函数将在那里暂停。当您关闭顶级窗口(或调用quit()应用程序)时,事件循环将停止,exec_()将返回,并且main()函数将继续。

但是下一行调用sys.exit(app.exec_()),它重新启动事件循环,并main()再次暂停函数——包括sys.exit()调用,它必须等待exec_()返回。但是,它将永远等待,因为现在没有 gui 可以与之交互,因此除了强制终止脚本之外,您无法停止事件循环。

于 2015-11-14T17:48:44.470 回答