pyqt5之将textBrowser的内容写入txt文档

        try:
            StrText = self.textBrowser.toPlainText()
            qS = str(StrText)
            f = open('/***/test.txt', 'w')
            print(f.write('{}'.format(qS)))
            f.close()
        except Exception as e:
            print(e)

首先通过toPlainText转化textBrowser里面的内容。

然后将转化后的结果强制str型

最后进行写入操作,如果想累加写入的话可以这么写:

        try:
            StrText = self.textBrowser.toPlainText()
            qS = str(StrText)
            f = open('/***/test.txt', 'a')
            print(f.write('\n{}'.format(qS)))
            f.close()
        except Exception as e:
            print(e)

我曾经跨过山和大海,也穿过人山人海,我曾经拥有着的一切,转眼都飘散如烟,我曾经失落失望失掉所有方向,直到看见平凡才是唯一的答案。
——韩寒《平凡之路》

猜你喜欢

转载自blog.csdn.net/shangxiaqiusuo1/article/details/82915420
今日推荐