我有一个PyQt5应用程序,需要为子进程写入输入才能停止。
然而,如果在不使用子进程按钮的情况下使用输入按钮,它也会杀死我的PyQt5 Mainwindow
。
如果我首先使用子进程按钮,然后使用输入按钮,self.bob.stdin.write("b")
应用程序保持打开状态,但如果我先按下输入按钮而不按子进程按钮self.bob.stdin.write("b")
,它会杀死我的应用程序和主窗口。< / p>
那么为什么self.bob.stdin.write("b")
杀死应用程序,如果我先按输入按钮,如何阻止它杀死我的MainWindow?
在这个测试代码中可以看到困境。
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_())
答案 0 :(得分:1)
在不理解所有代码的情况下,我认为您的代码转换为脚本的目的是:
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步时,我会让你考虑什么是黑客。