How to read from QTextedit in python?

前端 未结 2 997
长发绾君心
长发绾君心 2020-12-31 01:45

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         


        
2条回答
  •  臣服心动
    2020-12-31 02:14

    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.

    Example:

    mytext = self.textEdit.toPlainText()
    

    At this point, you can do whatever you want with 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

    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

提交回复
热议问题