<filename>CalcGui/mainctrl.py<gh_stars>0
# Import PySide classes
'''
Using Qt Designer with PySide

Defines a GuiApplication class that uses Qt PySide bindings.
The GUI has been created using Qt Designer tool.

Reference:
http://it.toolbox.com/blogs/enlightenment/pyside-tutorial-using-qt-designer-with-pyside-66012
'''
from PySide import QtCore, QtGui
from mainwindow import Ui_MainWindow

#importing * : to enable writing sin(13) instead of math.sin(13)
from math import *


class MainViewControl(QtGui.QMainWindow, Ui_MainWindow):
    '''Wrapper of PySide's QMainWindow class and
    Ui_mainWindow class created in Qt Designer.
    '''
    def __init__(self, parent=None):
        # Call Python's super function to inherit the properties of
        # both PySide's QMainWindow class as well as
        # the properties of the Ui_mainWindow created in Qt Designer.
        super(MainViewControl, self).__init__(parent)
        # Create the widgets defined in Qt Designer.
        # All files generated by the pyside-uic command require you
        # to call the self.setupUi function to do this.
        self.setupUi(self)
        # Customize the widgets
        self.assignWidgets()

    def assignWidgets(self):
        # Connect the signal clicked to solveFunc function
        self.solveButton.clicked.connect(self.solveFunc)
        # Connect the signal clicked to clearFunc function
        self.clearButton.clicked.connect(self.clearFunc)

    def solveFunc(self):
        '''Perform any valid mathematical expression entered in QLineEdit widget
        and put the result.
        '''
        try:
            ans = eval(self.inpLine.text())
            self.inpLine.setText(str(ans))
        except Exception:
            print 'error'

    def clearFunc(self):
        self.inpLine.clear()


class GuiApplication(QtGui.QApplication):
    def __init__(self, cli_args=None):
        super(GuiApplication, self).__init__(cli_args)
        self.window = MainViewControl()


if __name__ == "__main__":
    from sys import argv, exit
    # Create and show a Gui application
    app = GuiApplication(argv)
    app.window.show()
    # Enter Gui application main loop
    exit(app.exec_())
