pyqt5中创建并保存文件
时间: 2024-03-17 11:18:35 浏览: 215
Sure, 我可以回答这个问题!在 Pyqt5 中创建并保存一个文件,你需要使用 QFileDialog 和 QFile 。
你可以使用 QFileDialog 来获取用户选择的保存路径和文件名,使用 QFile 来创建并保存文件。下面是一段示范代码:
```
from PyQt5.QtWidgets import QApplication, QFileDialog
from PyQt5.QtCore import QFile, QTextStream
# 创建 QApplication
app = QApplication([])
# 选择保存路径和文件名
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getSaveFileName(None, "保存文件", "", "Text Files (*.txt)", options=options)
# 如果用户选择了文件名,就创建并保存文件
if fileName:
file = QFile(fileName)
if file.open(QFile.WriteOnly | QFile.Truncate):
stream = QTextStream(file)
stream << "Hello, World!"
file.close()
# 退出 QApplication
app.quit()
```
阅读全文
相关推荐


















