为什么 python Subprocess.stdin.write() 杀死 PyQt Gui
Why do python Subprocess.stdin.write() kill PyQt Gui
我有一个 PyQt5 应用程序需要写入输入以使子进程停止。
然而,如果在没有先使用子进程按钮的情况下使用输入按钮,它也会杀死我的 PyQt5 Mainwindow
。
如果我先使用子进程按钮,然后使用输入按钮,self.bob.stdin.write("b")
应用程序保持打开状态,但如果我先按输入按钮而不按子进程按钮 self.bob.stdin.write("b")
,它会终止我的应用程序和主窗口.
那么,为什么 self.bob.stdin.write("b")
终止应用程序,如果我先按下输入按钮,我该如何阻止它终止我的主窗口?
在这个测试代码中可以看出困境。
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(308, 156)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.Button_2 = QtWidgets.QPushButton(self.centralwidget)
self.Button_2.setGeometry(QtCore.QRect(20, 40, 121, 71))
self.Button_2.setObjectName("subprocessButton_2")
self.Button_1 = QtWidgets.QPushButton(self.centralwidget)
self.Button_1.setGeometry(QtCore.QRect(170, 40, 121, 71))
self.Button_1.setObjectName("inputbutton")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
self.Button_2.setText(_translate("MainWindow", "subprocess"))
self.Button_1.setText(_translate("MainWindow", "input"))
self.Button_2.clicked.connect(self.sub)
self.Button_1.clicked.connect(self.input)
def sub(self):
import subprocess
from subprocess import Popen, PIPE
from subprocess import Popen, PIPE
self.bob = subprocess.Popen('cmd ',stdin=PIPE, shell=True)
def input(self):
self.bob.stdin.write("q")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
在不理解您的所有代码的情况下,我认为您的代码转换为脚本后的作用如下:
import subprocess
from subprocess import Popen, PIPE
# sub button handler
bob = subprocess.Popen('echo',stdin=PIPE, shell=True)
# input button handler
bob.stdin.write(b"text")
我会让你想想如果你不做第 1 步会发生什么。
我有一个 PyQt5 应用程序需要写入输入以使子进程停止。
然而,如果在没有先使用子进程按钮的情况下使用输入按钮,它也会杀死我的 PyQt5 Mainwindow
。
如果我先使用子进程按钮,然后使用输入按钮,self.bob.stdin.write("b")
应用程序保持打开状态,但如果我先按输入按钮而不按子进程按钮 self.bob.stdin.write("b")
,它会终止我的应用程序和主窗口.
那么,为什么 self.bob.stdin.write("b")
终止应用程序,如果我先按下输入按钮,我该如何阻止它终止我的主窗口?
在这个测试代码中可以看出困境。
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(308, 156)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.Button_2 = QtWidgets.QPushButton(self.centralwidget)
self.Button_2.setGeometry(QtCore.QRect(20, 40, 121, 71))
self.Button_2.setObjectName("subprocessButton_2")
self.Button_1 = QtWidgets.QPushButton(self.centralwidget)
self.Button_1.setGeometry(QtCore.QRect(170, 40, 121, 71))
self.Button_1.setObjectName("inputbutton")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
self.Button_2.setText(_translate("MainWindow", "subprocess"))
self.Button_1.setText(_translate("MainWindow", "input"))
self.Button_2.clicked.connect(self.sub)
self.Button_1.clicked.connect(self.input)
def sub(self):
import subprocess
from subprocess import Popen, PIPE
from subprocess import Popen, PIPE
self.bob = subprocess.Popen('cmd ',stdin=PIPE, shell=True)
def input(self):
self.bob.stdin.write("q")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
在不理解您的所有代码的情况下,我认为您的代码转换为脚本后的作用如下:
import subprocess
from subprocess import Popen, PIPE
# sub button handler
bob = subprocess.Popen('echo',stdin=PIPE, shell=True)
# input button handler
bob.stdin.write(b"text")
我会让你想想如果你不做第 1 步会发生什么。