简体   繁体   English

如何在 python 中读取 QTextedit?

[英]How to read from QTextedit in python?

I created the GUI using QTDesigner and save the file as .ui extension.我使用 QTDesigner 创建了 GUI,并将文件保存为 .ui 扩展名。 Then convert the file to .py file using the following code然后使用以下代码将文件转换为 .py 文件

pyuic4 -x test.ui -o test.py 

Now I want to integrate my project code to this test.py file.现在我想将我的项目代码集成到这个 test.py 文件中。 Since I am new to pyqt4, I dont know how to read text from text edit and save to file and viceversa.由于我是 pyqt4 的新手,我不知道如何从文本编辑中读取文本并保存到文件,反之亦然。 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_())  

When i click enter (push button) the input in the textEdit is read and save into a file named input.txt.当我单击输入(按钮)时,会读取 textEdit 中的输入并将其保存到名为 input.txt 的文件中。 When i click quit (pushbutton) the output in the outfile.txt is read and write into textEdit_2 .当我单击退出(按钮)时,将读取 outfile.txt 中的输出并将其写入 textEdit_2 。 How to solve this ?如何解决这个问题?

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. 如果您需要的只是QTextEdit小部件中显示的文本,则可以通过在小部件上使用toPlainText()方法来访问该文本。

Example: 例:

mytext = self.textEdit.toPlainText()

At this point, you can do whatever you want with mytext . 此时,你可以用mytext做任何你想做的事。 You can write it to a file, you can manipulated it, etc. 你可以把它写到一个文件,你可以操纵它,等等。

If you need to (re)populate your QTextEdit with the modified value of mytext , you can do that by using setPlainText 如果您需要(重新)填充您QTextEdit与修改后的值mytext ,你可以通过使用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)

This will append mytext to somefile.txt 这会将mytext附加到somefile.txt

Along with the answer posted above. 随着上面的答案。 Don't modify the actual .py that is converted when you use pyuic4. 不要修改使用pyuic4时转换的实际.py。 You want to incorporate a class to handle the setup so that you can modify the UI without erasing everything that you have written. 您希望合并一个类来处理设置,以便您可以修改UI而不删除您编写的所有内容。 Example in pyqt5 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)

This should be how you utilize the converted Ui files it will make it so you can re edit the file and you wont loose all of the work you have put into the Ui control structure. 这应该是你如何使用转换后的Ui文件,这样你就可以重新编辑文件了,你不会失去你放入Ui控制结构的所有工作。 It will also allow you to use the same Ui for multiple purposes as you are using it as a template and changing what the buttons do in a different file. 它还允许您将相同的UI用于多种用途,因为您将其用作模板并更改按钮在不同文件中的操作。 Hope this helps. 希望这可以帮助。

mytext = self.textEdit.toPlainText()

Above code is not working上面的代码不起作用

Try Following Code :尝试以下代码:

mytext = self.textEdit.text()

And to change text in textedit :并更改 textedit 中的文本:

self.textEdit.setText("some text")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM