我有一个PyQt5应用程序需要写入子进程停止的输入。为什么python Subprocess.stdin.write()kill PyQt Gui
但是它也杀死我的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_())
您是否可以将代码简化为最小必需示例来演示问题?这使得它更容易找到问题。 –
删除了几行,其最小的应用程序创建,和两个按钮的功能 如果你按下左键,然后右键然后它没事。 但是,如果你先按左键,然后GUI退出 –