本文介绍了Pyside 应用程序未正确关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始在 PySide 中进行 GUI 编程,但在退出命令方面遇到了一些奇怪的问题.Sys.exit([x]) 似乎被广泛用于停止 PySide 程序;但是,每当我在使用类的 PySide 程序中使用它时,它都不会向 PyCharm 返回退出代码或停止任务管理器中的 Python 进程.

I recently took up GUI programming in PySide and I'm having trouble with an oddity regarding exit commands. Sys.exit([x]) seems to be widely used to stop a PySide program; whenever I use it in a PySide program using classes, however, it doesn't return an exit code to PyCharm or stop the Python process in the task manager.

奇怪的是,当使用 PySide 时,似乎很难做到这两件事;我可以跳出主循环,调用 sys.exit(0), raise SystemExit(0) 并且它不会停止在后台运行.这在使用 PySide 时发生,发生在使用类构造小部件时.

Oddly enough it seems it's incredibly hard to do either of these things when using PySide; I could break out of the main loop, call sys.exit(0), raise SystemExit(0) and it wouldn't stop running in the background. This only happens when using PySide, and only when using classes to construct widgets.

我尝试了各种组合,但我能可靠地终止进程的唯一方法是使用 os._exit(0),这有点残酷.奇怪的是,我似乎是遇到这个问题的极少数人之一,我很好奇是什么导致了它.

I have tried all sorts of combinations, but the only way I can reliably kill the process is by using os._exit(0), which is a bit brutal. Bizzarely it seems I am one of very few people that encounter this issue, and I'm very curious as to what's causing it.

我通过使用 .同样,调用 sys.exit() 没有返回退出值或终止 Python 进程.因为它是一个教程,我假设对于大多数人来说,这段代码工作得很好.这可能是版本问题(我使用 Python 3.4 和 PySide 1.2.2)?

I verified this wasn't a bug in my code per se by running a Pyside tutorial script using sys.exit(app.exec_()) from Zetcode. Again, calling sys.exit() did not return an exit value or kill the Python process. As it's a tutorial I am assuming that, for most people, this code works just fine. Could this be a version issue (I use Python 3.4 and PySide 1.2.2)?

编辑;我还发现将 sys.exit(0) 命令放在哪里并不重要,只要它在我的类定义之后.Imports > sys.exit() > class 只是立即退出(正如人们所期望的那样),但是 Imports > class > sys.exit() - 即使我还没有真正调用任何类 - 并没有正确关闭程序.

EDIT; I also found out it doesn't matter where I place the sys.exit(0) command, provided it's after my class definitions. Imports > sys.exit() > class just instantly quits (as one would expect), but Imports > class > sys.exit() - even if I haven't actually called on any classes yet - does not properly shut down the program.

推荐答案

我没有看到这种行为.这意味着我可以使用 sys.exit 来停止 PySide 程序.但是我更经常使用 app.quit 其中 appQApplication 实例.

I do not see this behavior. That means I can use sys.exit to stop a PySide program. However I more often use app.quit where app is the QApplication instance.

示例:

import sys
from PySide import QtGui

class MyWindow(QtGui.QWidget):
    def __init__(self):
        super().__init__()
        layout = QtGui.QVBoxLayout(self)
        button1 = QtGui.QPushButton('app.quit')
        button1.clicked.connect(app.quit)
        layout.addWidget(button1)
        button2 = QtGui.QPushButton('sys.exit')
        button2.clicked.connect(sys.exit)
        layout.addWidget(button2)

app = QtGui.QApplication([])

window = MyWindow()
window.show()

app.exec_()

这里两种方法都行.

我在 Windows 7 上使用 Python 3.3 上的 PySide 1.2.2.

I'm on PySide 1.2.2 on Python 3.3 on Windows 7.

这篇关于Pyside 应用程序未正确关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 02:31