如何从python中的QTextedit读取? [英] How to read from QTextedit in python?

查看:165
本文介绍了如何从python中的QTextedit读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created the GUI using QTDesigner and save the file as .ui extension. Then convert the file to .py file using the following code

pyuic4 -x test.ui -o test.py 

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

Now I want to integrate my project code to this test.py file. Since I am new to pyqt4, I dont know how to read text from text edit and save to file and viceversa. Following is my code.

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_())  

当我点击回车(按钮)时,textEdit 中的输入被读取并保存到一个名为 input.txt 的文件中.当我单击退出(按钮)时, outfile.txt 中的输出被读取并写入 textEdit_2 .如何解决这个问题?

When i click enter (push button) the input in the textEdit is read and save into a file named input.txt. When i click quit (pushbutton) the output in the outfile.txt is read and write into textEdit_2 . How to solve this ?

推荐答案

如果您只需要 QTextEdit 小部件中显示的文本,您可以使用 toPlainText() 您需要从中获取文本的小部件上的方法.

If all you need is the displayed text in your QTextEdit widget, you can access that by using the toPlainText() method on the widget you need the text from.

示例:

mytext = self.textEdit.toPlainText()

此时,您可以对 mytext 执行任何操作.您可以将其写入文件,也可以对其进行操作等.

At this point, you can do whatever you want with mytext. You can write it to a file, you can manipulated it, etc.

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

If you need to (re)populate your QTextEdit with the modified value of mytext, you can do that by using setPlainText

self.textEdit.setPlainText(mytext)

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

To write the string to a file, you'll do something like this:

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

这会将 mytext 附加到 somefile.txt

这篇关于如何从python中的QTextedit读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆