Instantly share code, notes, and snippets.

Embed
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
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_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.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(240, 345)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.listWidget = QtGui.QListWidget(self.centralwidget)
self.listWidget.setObjectName(_fromUtf8("listWidget"))
self.verticalLayout.addWidget(self.listWidget)
self.btnBrowse = QtGui.QPushButton(self.centralwidget)
self.btnBrowse.setObjectName(_fromUtf8("btnBrowse"))
self.verticalLayout.addWidget(self.btnBrowse)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
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">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>240</width>
<height>345</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QListWidget" name="listWidget"/>
</item>
<item>
<widget class="QPushButton" name="btnBrowse">
<property name="text">
<string>Pick a folder</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
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 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
@elitely

This comment has been minimized.

elitely commented Sep 9, 2016

Thanks

@mikes66

This comment has been minimized.

mikes66 commented Feb 6, 2017

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

@belgareth

This comment has been minimized.

belgareth commented Aug 10, 2017

MAny thanks

@Sahil624

This comment has been minimized.

Sahil624 commented Sep 17, 2017

Hi, I followed your instructions but I got following Error can you please help. 
File "test.py", line 20, in main     form = ExampleApp()                 # We set the form to be our ExampleApp (design)   File "test.py", line 14, in __init__     self.setupUi(self)  # This is defined in design.py file automatically   File "D:\Present work\product\maint.py", 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.

@racekiller

This comment has been minimized.

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
CODE
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)
self.setupUi(self)
self.btnBrowse.clicked.connect(self.browse_folder)

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

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

if name == 'main':
main()
CODE

Thanks

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