PyQt [control]: the use of QFileDialog.getSaveFileName()

1. Introduction
Function of the control: open the file explorer and get the file name you need to save. ( Note : it will not help you create a file, but only return a tuple) The first item of the tuple is your file path (including the file name you gave), and the second item is the type of the file.


2. For example,
I want to create an excel file with a specified directory and file name

#获得包含文件路径+文件名的元组
dirpath = QFileDialog.getSaveFileName(self.wnd, '选择保存路径', 'C:\\Users\\ASUS\\Desktop\\test.xlsx', 'xlsx(*.xlsx)')
#创建指定路径和文件名的excel文件
workbook = xlsxwriter.Workbook(dirpath[0])

The file dialog that pops up when you click the button is as follows:

insert image description here

Also note that if you click cancel it will return a tuple containing two empty strings.
insert image description here
Make sure you handle this situation.

Guess you like

Origin blog.csdn.net/qq_44683653/article/details/106712862