pyqt5最简单的打开和保存文件
Posted 金明爱python
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pyqt5最简单的打开和保存文件相关的知识,希望对你有一定的参考价值。
import sys import os from PyQt5.QtWidgets import QApplication,QWidget,QFileDialog from t import Ui_Form class myform(QWidget,Ui_Form): def __init__(self): super().__init__() self.setupUi(self) self.btn1.clicked.connect(self.btn_clear) self.btn2.clicked.connect(self.btn_open) self.btn3.clicked.connect(self.btn_save) self.show() def btn_clear(self): self.textEdit.clear() def btn_open(self): filename=QFileDialog.getOpenFileName(self,\'open file\',\'/home/jm/study\') with open(filename[0],\'r\') as f: my_txt=f.read() self.textEdit.setPlainText(my_txt) def btn_save(self): filename=QFileDialog.getSaveFileName(self,\'save file\',\'/home/jm/study\') with open(filename[0],\'w\') as f: my_text=self.textEdit.toPlainText() f.write(my_text) if __name__==\'__main__\': app=QApplication(sys.argv) w=myform() app.exec_()
以上是关于pyqt5最简单的打开和保存文件的主要内容,如果未能解决你的问题,请参考以下文章