如何从textbox1复制字符串,并在subproccess命令中使用,并在textbox2中输出

时间:2015-06-28 12:25:19

标签: python python-2.7 pyqt4

您好我想知道如何在textbox_1中键入字符串“word”(顶部一个框) 然后复制该字符串并在子进程命令中使用并将其输出到textbox_2(buttom框)而不使用信号

我尝试复制textbox_1,但该剂量似乎有效。

我的代码如下所示。

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
import subprocess
from subprocess import Popen, PIPE
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(840, 460)
        self.textEdit_1 = QtGui.QTextEdit(Form)
        self.textEdit_1.setGeometry(QtCore.QRect(0, 0, 291, 141))
        self.textEdit_1.setObjectName(_fromUtf8("textEdit_1"))
        self.textEdit_2 = QtGui.QTextEdit(Form)
        self.textEdit_2.setGeometry(QtCore.QRect(0, 200, 301, 101))
        self.textEdit_2.setObjectName(_fromUtf8("textEdit_2"))
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(370, 40, 381, 241))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.pushButton.setText(_translate("Form", "go", None))

        self.pushButton.clicked.connect(self.run)

    def run(self):
        ff = self.textEdit_1.copy()
        dog1 = subprocess.Popen('help ' + ff, stdout=subprocess.PIPE, shell=True)
        self.textEdit_2.setText(dog1.stdout.read())



if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

```

只使用一个子进程命令而没有来自text_box1的副本,因为输出右侧文本。

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
import subprocess
from subprocess import Popen, PIPE
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(840, 460)
        self.textEdit_1 = QtGui.QTextEdit(Form)
        self.textEdit_1.setGeometry(QtCore.QRect(0, 0, 291, 141))
        self.textEdit_1.setObjectName(_fromUtf8("textEdit_1"))
        self.textEdit_2 = QtGui.QTextEdit(Form)
        self.textEdit_2.setGeometry(QtCore.QRect(0, 200, 301, 101))
        self.textEdit_2.setObjectName(_fromUtf8("textEdit_2"))
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(370, 40, 381, 241))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.pushButton.setText(_translate("Form", "go", None))

        self.pushButton.clicked.connect(self.run)

    def run(self):
        #ff = self.textEdit_1.copy()
        dog1 = subprocess.Popen('help start', stdout=subprocess.PIPE, shell=True)
        self.textEdit_2.setText(dog1.stdout.read())



if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

```

1 个答案:

答案 0 :(得分:0)

你是什​​么意思? “复制”将任何选定的文本复制到剪贴板。您只需在框中键入内容,然后突出显示某些字符或整个内容并点击“运行”。现在您可以在任何地方从剪贴板粘贴它。 http://pyqt.sourceforge.net/Docs/PyQt4/qtextedit.html#copy