QT:如何将程序生成的文件保存到用户的机器中



>我有一个 PyQt 程序,它从用户那里获取一个文件进行处理,然后相应地创建一个新文件。 我希望用户能够将新创建的文件下载/保存到他们的机器/桌面中......

这是上传代码:

def open_dialog_box(self):
#To just print the name of file
filename = QFileDialog.getOpenFileName()
filename = filename[0]
basename = os.path.basename(filename)
def pushButton_2_handler(self):
path = QFileDialog.getOpenFileName()
path = path[0]
basename = os.path.basename(path)
filename, file_extension = os.path.splitext(basename)
print(path)
print(filename)
print(file_extension)
self.textBrowser_fileSelectedName.clear()
self.textBrowser_fileSelectedName.append(str(basename))

您想使用其他QFileDialog但此将设置为返回用户键入的选定文件夹/自定义名称。

def save_file(self):
# Open a dialog so that the user can select a target location.
dialog = QFileDialog()
dialog.setWindowTitle("Select a location to save your file.")
dialog.setAcceptMode(QFileDialog.AcceptSave)
dialog.exec_()
if not dialog.selectedFiles(): # If the user closed the choice window without selecting anything.
return
else:
path_to_file = dialog.selectedFiles()[0]
# Create that file and work on it, for example:
with open(path_to_file + ".txt") as file:
file.write("Hello there!")

最新更新