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_()
天街小雨润如酥,草色遥看近却无
最是一年春好处,绝胜烟柳满皇都