PyQt - File Open Event



File open events occur when users initiate actions within an application to access or manipulate files stored on their system. PyQt provides some robust mechanisms to manage these events and helps the developers to create intuitive and efficient file-handling functionalities within their applications.

Methods for Handling File Open Events in PyQt

To handle file open events in PyQt effectively the developers can utilize various methods provided by the framework.

Method Description
QFileDialog.getOpenFileName() Presents a file dialog allowing users to select a single file for opening.
QFileDialog.getOpenFileNames() Presents a file dialog enabling users to select multiple files for opening.
QFileDialog.getExistingDirectory() Displays a directory dialog enabling users to choose an existing directory.

Example 1: Single File Selection

In the below example we will get to know how to create a PyQt6 application where users can select a single file. When the "Open File" button is clicked, a file dialog appears, allowing the user to choose a file. The selected file path is then printed to the console.

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QFileDialog

class FileOpenExample(QMainWindow):
   def __init__(self):
      super().__init__()
      self.setWindowTitle("Single File Selection Example")
      self.setGeometry(300, 200, 400, 200)

      button = QPushButton("Open File", self)
      button.setGeometry(150, 80, 100, 30)
      button.clicked.connect(self.open_file)

   def open_file(self):
      file_name, _ = QFileDialog.getOpenFileName(self, "Open File", "", "All Files (*)")
      if file_name:
         print("Selected File:", file_name)

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = FileOpenExample()
   window.show()
   sys.exit(app.exec())

Output

The above code produces the following output −

pyqt file open event example 1

The file selected to open is also printed to the console.

Selected File: C:/Users/Muralidhar/Desktop/PYQT Tutorial/Code/discoonecting_signal.py

Example 2: Multiple File Selection

In the below example, users can select multiple files using a PyQt6 application. Upon clicking the "Open Files" button, a file dialog opens, enabling the selection of multiple files. The paths of the selected files are printed to the console.

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QFileDialog

class MultiFileOpenExample(QMainWindow):
   def __init__(self):
      super().__init__()
      self.setWindowTitle("Multiple File Selection Example")
      self.setGeometry(300, 200, 400, 200)

      button = QPushButton("Open Files", self)
      button.setGeometry(150, 80, 100, 30)
      button.clicked.connect(self.open_files)

   def open_files(self):
      files, _ = QFileDialog.getOpenFileNames(self, "Open Files", "", "All Files (*)")
      if files:
         print("Selected Files:", files)

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = MultiFileOpenExample()
   window.show()
   sys.exit(app.exec())

Output

The above code produces the following output −

pyqt file open event example 2

Multiple files selected is also printed to the console.

Selected Files: ['C:/Users/Muralidhar/Desktop/PYQT Tutorial/Code/multiple_signal_slot.py', 'C:/Users/Muralidhar/Desktop/PYQT Tutorial/Code/New_signal.py', 'C:/Users/Muralidhar/Desktop/PYQT Tutorial/Code/new_signal_2.py']

Example 3: Directory Selection

In the below example we will see how to create a PyQt6 application for selecting directories. When the "Open Directory" button is clicked, a directory dialog appears, allowing the user to choose a directory. The selected directory path is then printed to the console.

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QFileDialog

class DirectoryOpenExample(QMainWindow):
   def __init__(self):
      super().__init__()
      self.setWindowTitle("Directory Selection Example")
      self.setGeometry(300, 200, 400, 200)

      button = QPushButton("Open Directory", self)
      button.setGeometry(150, 80, 100, 30)
      button.clicked.connect(self.open_directory)

   def open_directory(self):
      directory = QFileDialog.getExistingDirectory(self, "Open Directory", "")
      if directory:
         print("Selected Directory:", directory)

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = DirectoryOpenExample()
   window.show()
   sys.exit(app.exec())

Output

The above code produces the following output −

pyqt file open event example 3

The file location is also printed to the console.

Selected Directory: C:/Users/Muralidhar/Desktop/PYQT Tutorial/Code/pyqt
Advertisements