An Introduce to PyQT QFileDialog Get Directory Path with Examples – PyQT Tutorial

By | December 13, 2019

If you want to select a directory to save a file in pyqt, there are two problems you should solve.

How to open a file dialog to choose a directory?

How to get the directory path you have chosen?

To solve these two problems, we will write an example to show you how to do.

How to open a file dialog to choose a directory?

In pyqt, we can use QFileDialog to open a file dialog to select a directory. Here is a example:

import sys
import os
from PyQt5.QtWidgets import QApplication, QWidget,QPushButton, QHBoxLayout, QVBoxLayout
from PyQt5.QtWidgets import QLineEdit, QMessageBox, QFileDialog
from PyQt5.QtWidgets import QTableWidget,QTableWidgetItem, QHeaderView
from PyQt5 import QtCore

self.dir_path=QFileDialog.getExistingDirectory(self,"Choose Directory","E:\\")

In this code, we can open a file dialog and select directory.

Where Choose Directory is the name of this file dialog. E:\\ is the directory path this dialog will open.

The effect likes:

Open a pyqt qfiledialog example

How to get the directory path you have chosen?

If you have selected a directory, this dialog will be closed. The directory path will be returned and saved in self.dir_path.

The effect likes:

pyqt qfiledialog get directory path example

Moreover, you can bind the function which selects a directory to a pyqt button. To understand how to bind a click event for a button, you can view this tutorial.

An Introduce to PyQT Button Bind Click Event with Examples – PyQT Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *