[英]PyQt error “QProcess: Destroyed while process is still running”
當我嘗試運行以下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()
當應用程序關閉且進程尚未完成時QProcess: Destroyed while process is still running
時,通常會收到錯誤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事件才能正確結束該過程。 這是有效的,給你替換self.child
child
:
def closeEvent(self,event):
self.child.terminate()
self.child.waitForFinished()
event.accept()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.