当我尝试运行以下PyQt代码来运行进程和tmux时,我遇到错误QProcess: Destroyed while process is still running.
我该如何解决这个问题?
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class embeddedTerminal(QWidget):
def __init__(self):
QWidget.__init__(self)
self._processes = []
self.resize(800, 600)
self.terminal = QWidget(self)
layout = QVBoxLayout(self)
layout.addWidget(self.terminal)
self._start_process(
'xterm',
['-into', str(self.terminal.winId()),
'-e', 'tmux', 'new', '-s', 'my_session']
)
button = QPushButton('list files')
layout.addWidget(button)
button.clicked.connect(self._list_files)
def _start_process(self, prog, args):
child = QProcess()
self._processes.append(child)
child.start(prog, args)
def _list_files(self):
self._start_process(
'tmux', ['send-keys', '-t', 'my_session:0', 'ls', 'Enter']
)
if __name__ == "__main__":
app = QApplication(sys.argv)
main = embeddedTerminal()
main.show()
答案 0 :(得分:3)
当应用程序关闭且流程尚未完成时,您通常会收到错误QProcess: Destroyed while process is still running
。
在您当前的代码中,您的应用程序会在启动时立即结束,因为您没有调用app.exec_()
。你应该做点什么:
if __name__ == "__main__":
app = QApplication(sys.argv)
main = embeddedTerminal()
main.show()
sys.exit(app.exec_())
现在,它工作正常,但是当您关闭应用程序时,您仍会收到错误消息。您需要覆盖close事件才能正确结束该过程。如果您将child
替换为self.child
:
def closeEvent(self,event):
self.child.terminate()
self.child.waitForFinished()
event.accept()