我有这个简单的代码:基本上是一个向 cmd 发送一些命令并在 QTextEdit 中显示 cmd 输出的工具。基本上,它有效。我遇到的唯一问题是每次单击发送(有或没有新命令)时,都会附加文本,但 QTextEdit 的末尾会出现奇怪的空白行。即使我清除“控制台”,仍然有这些行。也许这与我调用该过程的方式有关,我不知道因此需要帮助。
from PySide.QtCore import *
from PySide.QtGui import *
import sys
class MyWindow(QDialog):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setWindowTitle("Send to CMD")
self.check1 = QCheckBox("Activate Variable")
self.variable = QLineEdit()
self.finalcommand = QLineEdit()
self.clearCommand = QPushButton("Clear")
self.sendCommand = QPushButton("Send")
self.clearOnSend = QCheckBox("Clear on Send")
self.process = QProcess()
self.console = QTextEdit(self)
layout = QVBoxLayout()
layout.addWidget(self.check1)
layout.addWidget(self.variable)
layout.addWidget(self.finalcommand)
layout.addWidget(self.clearOnSend)
layout.addWidget(self.clearCommand)
layout.addWidget(self.sendCommand)
layout.addWidget(self.console)
self.setLayout(layout)
self.connect(self.check1, SIGNAL("clicked()"), self.appendText)
self.variable.textChanged.connect(self.appendText)
self.clearCommand.clicked.connect(self.Clear)
self.sendCommand.clicked.connect(self.Send)
def appendText(self):
if self.check1.isChecked():
TEXT1 = "Dir" + ' ' + str(self.variable.text())
else:
TEXT1 = ""
self.finalcommand.setText(str(TEXT1))
def Clear(self):
if self.clearCommand.isEnabled():
self.console.clear()
def Send(self):
if self.clearOnSend.isChecked():
self.console.clear()
FCTS = "cmd.exe /c" + " " + str(self.finalcommand.text())
self.process.readyReadStandardOutput.connect(self.readConsole)
self.process.start(FCTS)
if not self.process.waitForStarted(0):
return False
if not self.process.waitForFinished(0):
return False
def readConsole(self):
#self.console.setText(str(self.process.readAllStandardOutput()))
self.console.append(str(self.process.readAllStandardOutput()))
app = QApplication(sys.argv)
form = MyWindow()
form.show()
app.exec_()