Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'design.ui'
# Created: Wed May 27 16:39:17 2015
# by: PyQt4 UI code generator 4.11.3
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.resize(240, 345)
self.centralwidget = QtGui.QWidget(MainWindow)
self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
self.listWidget = QtGui.QListWidget(self.centralwidget)
self.btnBrowse = QtGui.QPushButton(self.centralwidget)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.btnBrowse.setText(_translate("MainWindow", "Pick a folder", None))
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<property name="windowTitle">
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<widget class="QListWidget" name="listWidget"/>
<widget class="QPushButton" name="btnBrowse">
<property name="text">
<string>Pick a folder</string>
from PyQt4 import QtGui # Import the PyQt4 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(QtGui.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 file
super(self.__class__, self).__init__()
self.setupUi(self) # This is defined in 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) # 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
Copy link

elitely commented Sep 9, 2016


Copy link

mikes66 commented Feb 6, 2017

Truely brilliant, helped clear some of the confusion I was having

Copy link

belgareth commented Aug 10, 2017

MAny thanks

Copy link

Sahil624 commented Sep 17, 2017

Hi, I followed your instructions but I got following Error can you please help. 
File "", line 20, in main     form = ExampleApp()                 # We set the form to be our ExampleApp (design)   File "", line 14, in __init__     self.setupUi(self)  # This is defined in file automatically   File "D:\Present work\product\", line 1418, in setupUi     QtCore.QObject.connect(self.close, QtCore.SIGNAL(_fromUtf8("triggered()")), MainWindow.close) TypeError: arguments did not match any overloaded call:   QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection): not enough arguments   QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'QAction'   QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 2 has unexpected type 'bytes'

Any help would be appreciated.

Copy link

racekiller commented Apr 28, 2018

Hello would you please update your blog, you need to clarify that your code will not work on PyQt5 environment.
The QtGui is not supported in PyQt5, it needs to be changed using QtWidgets instead.

This is the code updated to work with PyQt5
from PyQt5 import QtGui, QtCore, QtWidgets
import sys, os

import design

class ExampleApp(QtWidgets.QMainWindow, design.Ui_MainWindow):
def init(self, parent=None):
super(ExampleApp, self).init(parent)

def browse_folder(self):
    directory = QtWidgets.QFileDialog.getExistingDirectory(self,"Pick a folder")
    if directory:
        for file_name in os.listdir(directory):

def main():
app = QtWidgets.QApplication(sys.argv)
form = ExampleApp()

if name == 'main':


Copy link

MakChiKin commented Feb 22, 2019

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment