我有這個簡單的代碼:基本上是一個工具來發送一些命令到CMD,並顯示在QTextEdit cmd的輸出。 基本上,它的工作原理。 我唯一的問題是,每次點擊發送(有或沒有新命令)時,文本都被追加了,但奇怪的空白行出現在QTextEdit的末尾。即使我清除「控制檯」,仍然有這些線。 也許它與我稱之爲過程的方式有關,因此我不知道需要幫助。PySide,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_()
也許tr Ÿ 高清readConsole(個體經營): BA = self.process.readAllStandardOutput() QString的S =巴 self.console.append(S) – Saurabh7 2013-03-10 06:54:41
感謝提供幫助但您的解決方案不會返回同樣的問題 – Abe 2013-03-10 11:56:15
也許有垃圾值與字符串,嘗試使用內置的string.strip()功能 – Saurabh7 2013-03-11 06:57:25