Skip to content

Instantly share code, notes, and snippets.

@nikolak
Created May 27, 2015 14:55
Show Gist options
  • Save nikolak/615def9e197d5db04bef to your computer and use it in GitHub Desktop.
Save nikolak/615def9e197d5db04bef to your computer and use it in GitHub Desktop.
# -*- 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
@MakChiKin
Copy link

Thank you very much!

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