PyQt5 QFileDialog: 获取文件夹路径错误
错误的代码:
def get_file_path(self):
file_name = QtWidgets.QFileDialog.getExistingDirectory(self, "Select File Directory to Save File", "")
print(file_name)
引起的错误:
file_name = QtWidgets.QFileDialog.getExistingDirectory(self, "Select File Directory to Save File", "")
TypeError: getExistingDirectory(parent: QWidget = None, caption: str = '', directory: str = '', options: Union[QFileDialog.Options, QFileDialog.Option] = QFileDialog.ShowDirsOnly): argument 1 has unexpected type 'Ui_Filepath'
原因:
在getExistingDirectory()
中的第一个形参输入self
,而这里的self
指的是我所创建的类Ui_Filepath
,并不是这个方法中需要的QWidget类型的对象。
解决方法:
第一个形参输入为None
:
def get_file_path(self, Filepath):
file_name = QtWidgets.QFileDialog.getExistingDirectory(None, "Select File Directory to Save File", "")
print(file_name)
self.file_name = file_name
这里的self.file_name
就是所获得的所选文件路径了!!!