如何在python中读取QTextedit?

时间:2014-06-04 11:01:13

标签: python pyqt4

我使用QTDesigner创建了GUI并将文件另存为.ui扩展名。然后使用以下代码将文件转换为.py文件

pyuic4 -x test.ui -o test.py 

现在我想将项目代码集成到此test.py文件中。由于我是pyqt4的新手,我不知道如何从文本编辑中读取文本并保存到文件,反之亦然。以下是我的代码。

from PyQt4 import QtCore, QtGui  
from final_ar_gui import *

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_AnaphoraResolution(object):  

  def setupUi(self, AnaphoraResolution):
    AnaphoraResolution.setObjectName(_fromUtf8("AnaphoraResolution"))
    AnaphoraResolution.resize(608, 620)
    self.textEdit = QtGui.QTextEdit(AnaphoraResolution)
    self.textEdit.setGeometry(QtCore.QRect(0, 110, 271, 341))
    self.textEdit.setObjectName(_fromUtf8("textEdit"))
    self.textEdit_2 = QtGui.QTextEdit(AnaphoraResolution)
    self.textEdit_2.setGeometry(QtCore.QRect(310, 110, 271, 341))
    self.textEdit_2.setObjectName(_fromUtf8("textEdit_2"))
    self.pushButton = QtGui.QPushButton(AnaphoraResolution)
    self.pushButton.setGeometry(QtCore.QRect(40, 470, 91, 27))
    self.pushButton.setObjectName(_fromUtf8("pushButton"))
    self.pushButton_2 = QtGui.QPushButton(AnaphoraResolution)
    self.pushButton_2.setGeometry(QtCore.QRect(170, 470, 171, 27))
    self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
    self.pushButton_3 = QtGui.QPushButton(AnaphoraResolution)
    self.pushButton_3.setGeometry(QtCore.QRect(370, 470, 81, 27))
    self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
    self.pushButton_4 = QtGui.QPushButton(AnaphoraResolution)
    self.pushButton_4.setGeometry(QtCore.QRect(480, 470, 98, 27))
    self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
    self.label = QtGui.QLabel(AnaphoraResolution)
    self.label.setGeometry(QtCore.QRect(180, 30, 241, 20))
    self.label.setObjectName(_fromUtf8("label"))

    self.retranslateUi(AnaphoraResolution)
    self.connectActions()
    QtCore.QMetaObject.connectSlotsByName(AnaphoraResolution)

 def retranslateUi(self, AnaphoraResolution):
    AnaphoraResolution.setWindowTitle(QtGui.QApplication.translate("AnaphoraResolution", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton.setText(QtGui.QApplication.translate("AnaphoraResolution", "Enter", None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton_2.setText(QtGui.QApplication.translate("AnaphoraResolution", "Pronominal Resolution", None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton_3.setText(QtGui.QApplication.translate("AnaphoraResolution", "Clear", None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton_4.setText(QtGui.QApplication.translate("AnaphoraResolution", "Quit", None, QtGui.QApplication.UnicodeUTF8))
    self.label.setText(QtGui.QApplication.translate("AnaphoraResolution", "Anaphora Resolution in Malayalam", None, QtGui.QApplication.UnicodeUTF8))

 def connectActions(self):

    self.pushButton.clicked.connect(self.ent)
 def ent(self):

    %Dont know what code to write


if __name__ == "__main__":
  import sys
  app = QtGui.QApplication(sys.argv)
  AnaphoraResolution = QtGui.QDialog()
  ui = Ui_AnaphoraResolution()
  ui.setupUi(AnaphoraResolution)
  AnaphoraResolution.show()
  sys.exit(app.exec_())  

当我单击enter(按钮)时,将读取textEdit中的输入并保存到名为input.txt的文件中。当我单击退出(按钮)时,将读取outfile.txt中的输出并写入textEdit_2。怎么解决这个问题?

2 个答案:

答案 0 :(得分:29)

如果你需要的只是QTextEdit小部件中显示的文本,你可以使用小部件上的toPlainText()方法访问它,你需要文本。

示例:

mytext = self.textEdit.toPlainText()

此时,您可以使用mytext执行任何操作。你可以把它写到一个文件,你可以操纵它等等。

如果您需要(重新)使用修改后的QTextEdit值填充mytext,则可以使用setPlainText

来执行此操作
self.textEdit.setPlainText(mytext)

要将字符串写入文件,您将执行以下操作:

with open('somefile.txt', 'a') as f:
    f.write(mytext)

这会将mytext追加到somefile.txt

答案 1 :(得分:0)

附带上面发布的答案。使用pyuic4时,请勿修改转换的实际.py。您希望合并一个类来处理设置,以便可以在不删除已编写的所有内容的情况下修改UI。 pyqt5中的示例

from MyConvertedUiFile import Ui_SomeName

class MyWorkingCode(QtWidgets.QMainWindow, Ui_SomeName):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.ent)

    def ent(self):
        mytext = self.textEdit.toPlainText()
        with open('somefile.txt', 'a') as f:
            f.write(mytext)

这应该是利用转换后的Ui文件的方式,它将使您可以重新编辑该文件,并且不会丢失已放入Ui控件结构中的所有工作。它还将使您可以将同一Ui用作多种用途,就像您将其用作模板并更改按钮在不同文件中的用途一样。希望这会有所帮助。