关闭后无法杀死 PyQT 窗口.这需要我重新启动内核 [英] Can't Kill PyQT Window after closing it. Which requires me to restart the kernal

查看:49
本文介绍了关闭后无法杀死 PyQT 窗口.这需要我重新启动内核的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我没有正确关闭我的 PyQT5 窗口.我正在使用与 anaconda 一起安装的 spyder (3.3.5) 来编写 pyqt5 程序.我正在使用 qt creator 设计我的 ui 文件,我使用 pyqt 包中的 loadUi 函数加载该文件.代码一切正常,直到我需要关闭它.我通过关闭按钮(右上角的 x 按钮)关闭窗口.窗口本身是关闭的,但似乎控制台(或外壳)卡住了,我不能给它进一步的命令或重新运行程序,而不必重新启动内核(完全关闭我的 IDE 并重新打开)它).

I guess that I am not closing my PyQT5 Window correctly. I am using spyder (3.3.5) which I have installed with anaconda, to program a pyqt5 program. I am using qt creator to design my ui file, which I load using the loadUi function in pyqt package. Everything works fine with the code, until I need to close it. I close the window via the close button (the x button in the top right). The window itself is closed, but it seems like the console (or shell) is stuck, and I can't give it further commands or to re run the program, without having to restart the kernel (to completely close my IDE and re opening it).

我曾尝试在互联网上寻找问题的解决方案,但似乎没有一个对我有用.包括将 IPython Console > Graphics 更改为自动.

I have tried looking for solutions to the problem in the internet, but none seems to work for me. Including changing the IPython Console > Graphics to automatic.

还创建了一个问题:https://github.com/spyder-ide/spyder/issues/9991

import sys
from PyQt5 import QtWidgets,uic
from PyQt5.QtWidgets import QMainWindow
class Mic_Analysis(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui=uic.loadUi("qt_test.ui",self)
...
if __name__ == "__main__":
    def  run_app():
        if not QtWidgets.QApplication.instance():        
            app = QtWidgets.QApplication(sys.argv)
        else:
            app=QtWidgets.QApplication.instance()
        mainWin=Mic_Analysis()
        mainWin.show()
        app.exec_()
    run_app()  

如果有人有任何建议,我很乐意听取他们的意见.

If someone have any suggestion I would be very happy to hear them.

推荐答案

对我来说,它有助于删除app.exec_()"命令.但是在运行代码时它会立即关闭.为了保持窗口打开,我需要将 MainWindow 实例返回到全局范围(或​​使其成为全局对象).我的代码如下所示:

For me, it helped to remove the 'app.exec_()' command. But then it closes immediatly when running the code. To keep the window open, I needed to return the MainWindow instance to the global scope (or make it a global object). My code looks like this:

from PyQt5 import QtWidgets
import sys

def main():
    if not QtWidgets.QApplication.instance():
        app = QtWidgets.QApplication(sys.argv)
    else:
        app = QtWidgets.QApplication.instance()
    main = MainWindow()
    main.show()

    return main

if __name__ == '__main__':         
    m = main()

这篇关于关闭后无法杀死 PyQT 窗口.这需要我重新启动内核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆