random_1 15 Light Poster
from PyQt5 import QtCore, QtGui, QtWidgets  # Import the PyQt5 module we'll need
import sys  # We need sys so that we can pass argv to QApplication

import design  # This file holds our MainWindow and all design related things

# it also keeps events etc that we defined in Qt Designer
import os  # For listing directory methods

class ExampleApp(QtWidgets.QMainWindow, design.Ui_MainWindow):
    def __init__(self):
        # Explaining super is out of the scope of this article
        # So please google it if you're not familar with it
        # Simple reason why we use it here is that it allows us to
        # access variables, methods etc in the design.py file
        super(self.__class__, self).__init__()
        self.setupUi(self)  # This is defined in design.py file automatically
        # It sets up layout and widgets that are defined
        self.btnBrowse.clicked.connect(self.browse_folder)  # When the button is pressed
                                                            # Execute browse_folder function

    def browse_folder(self):
        self.listWidget.clear() # In case there are any existing elements in the list
        directory = QtGui.QFileDialog.getExistingDirectory(self,
                                                           "Pick a folder")
        # execute getExistingDirectory dialog and set the directory variable to be equal
        # to the user selected directory

        if directory: # if user didn't pick a directory don't continue
            for file_name in os.listdir(directory): # for all files, if any, in the directory
                self.listWidget.addItem(file_name)  # add file to the listWidget

def main():
    app = QtGui.QApplication(sys.argv)  # A new instance of QApplication
    form = ExampleApp()  # We set the form to be our ExampleApp (design)
    form.show()  # Show the form
    app.exec_()  # and execute the app

if __name__ == '__main__':  # if we're running file directly and not importing it
    main()  # run the main function

I get the error :
app = QtGui.QApplication(sys.argv) AttributeError : 'module' object has no attribute 'QApplication'

I have a "design.py" which is the gui of my application. This code is my "main.py" that's suppose to show and execute the app. But I got that error. Not sure what is wrong. Thank you in advance.

Dani AI

Generated

Your code is using PyQt5 but you are asking QtGui for classes that moved in Qt5. In PyQt5, many widget-related classes were split out of QtGui into QtWidgets. That is why QtGui.QApplication (and QtGui.QFileDialog) do not exist anymore. Use the QtWidgets versions instead. The PyQt5 porting notes call out this split explicitly, and common PyQt5 examples import QApplication and QFileDialog from QtWidgets. (doc.bccnsoft.com)

Quick fixes you can drop in:

  • Construct the app with QtWidgets:

    app = QtWidgets.QApplication(sys.argv)
  • Open the folder chooser from QtWidgets:

    directory = QtWidgets.QFileDialog.getExistingDirectory(self, "Pick a folder")

If you prefer named imports, this also works and reads clearly:

from PyQt5.QtWidgets import QApplication, QFileDialog

For a small cleanup: in Python 3 you can simplify the constructor call to avoid MRO pitfalls by using super().__init__() instead of super(self.__class__, self).__init__(). This is the recommended form and avoids subtle issues when subclassing. (stackoverflow.com)

Reference: QApplication lives in PyQt5.QtWidgets (not QtGui), and the same is true for QFileDialog; you can see this in both the accepted answer on a similar SO thread and standard PyQt5 tutorials. (stackoverflow.com)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.