【发布时间】:2023-05-16 17:56:01
【问题描述】:
我有这个简单的代码:基本上是一个向 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_()
【问题讨论】:
-
也许可以试试 def readConsole(self): Ba = self.process.readAllStandardOutput() QString s = Ba self.console.append(s)
-
感谢您提供帮助,但您的解决方案确实返回了同样的问题
-
可能字符串中有垃圾值,请尝试使用内置的 string.strip() 功能
-
我遇到了同样的问题....有什么解决办法吗?
标签: python-2.7 append pyside qtextedit qprocess