TheOtherTD
9/8/2015 - 11:30 PM

Testing a UI

Testing a UI

from PySide import QtGui, QtCore
# from PyQt4 import QtGui, QtCore
import sys


class MyFirstUI(QtGui.QDialog):
    def __init__(self, *args, **kws):
        parent = kws.get('parent', None)
        super(MyFirstUI, self).__init__(parent=parent)

        ## Class variables
        self.logic = Logic()

        ## Setup GUI
        self._buildUI()
        self._setupConnections()

    def _buildUI(self):
        '''
        Purpose: Contains all window elements
        '''
        ## Window Title
        self.setWindowTitle('Zero Out')

        ## Main Layout
        self._mainLayout = QtGui.QVBoxLayout()
        self._mainLayout.setAlignment(QtCore.Qt.AlignCenter)
        self.setLayout(self._mainLayout)

        ## Do Something button
        self._thisLine = QtGui.QLineEdit()
        self._mainLayout.addWidget(self._thisLine)

        ## Do Something button
        self._doSomethingButton = QtGui.QPushButton('Do Something')
        self._mainLayout.addWidget(self._doSomethingButton)

    def _setupConnections(self):
        '''
        Purpose: Connect all GUI Elemenents to functions
        '''
        self._doSomethingButton.clicked.connect(self._doSomethingButton_clicked)

    def _doSomethingButton_clicked(self):
        value = str(self._thisLine.text())

        self.logic.alterString(value)


class Logic():
    def alterString(self, val):
        '''
        Alter string and carlo 

        @param val: String name
        @type  val: str
        '''
        print val+ ' :WAS PRINTED!!!'


def show_dialog():
    '''
    Shows the dialog as a singleton
    '''
    global MyFirstUI_instance

    try:
        MyFirstUI_instance.close()
        MyFirstUI_instance.deleteLater()
    except:
        pass

    MyFirstUI_instance = MyFirstUI(parent=None)
    MyFirstUI_instance.show()

    return MyFirstUI_instance


def main():
    app = QtGui.QApplication(sys.argv)
    # app.setStyle("DarkMojo")
    show_dialog()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()