Launching PyQt ui from script

When starting a UI application, I always refer to the following recipe to get me started.

I designed this very simple UI using QtDesigner and called it main_window.ui. I placed it in the ui_template folder.

Here is what the structure of the project looks like.

I wrote my own script I used to built automatically the python version of the template

setup.py

import os


class UiBuilder(object):
    pyuic = ''

    def __init__(self, ui_name=''):
        self.define_pyuic_to_run()
        self.ui_name = os.path.abspath('ui_template/' + ui_name)
        [base, ext] = os.path.splitext(ui_name)
        py_name = base + '.py'
        self.py_name = os.path.abspath('ui/' + py_name)

        # run command
        print(self.pyuic + ' ' + self.ui_name + ' -o ' + self.py_name)
        os.system(self.pyuic + ' ' + self.ui_name + ' -o ' + self.py_name)

    def define_pyuic_to_run(self):
        try:
            from PyQt4 import QtGui
            self.pyuic = 'pyuic4'
        except:
            from PyQt5 import QtGui
            self.pyuic = 'pyuic5'


if __name__ == "__main__":
    o_builder = UiBuilder(ui_name='main_window.ui')

to run it, simply do

$ python setup.py

We can now import and use the interface in our main script, called here main.py

import sys

try:
    from PyQt4.QtGui import QFileDialog
    from PyQt4 import QtCore, QtGui
    from PyQt4.QtGui import QMainWindow
except ImportError:
    from PyQt5.QtWidgets import QFileDialog
    from PyQt5 import QtCore, QtGui
    from PyQt5.QtWidgets import QApplication, QMainWindow

from ui.main_window  import Ui_MainWindow as UiMainWindow


class Interface(QMainWindow):

    def __init__(self, parent=None):

        QMainWindow.__init__(self, parent=parent)
        self.ui = UiMainWindow()
        self.ui.setupUi(self)
        self.setWindowTitle("Anton's MCP Detector Efficiency Correction UI")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    o_interface = Interface()
    o_interface.show()
    sys.exit(app.exec_())

and to start the application

$ python main.py