#!/usr/bin/env python # $Id: calc.py,v 1.26 2001/09/27 08:24:57 wyy Exp $ # # William Emmanuel S. YU # Ateneo de Manila University, Philippines # # QT Python Calculator # Copyright (C) 2001 William Emmanuel S. Yu # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # import sys from qt import * from FCalc import FCalc from math import sqrt # overriding the original calculator program class FCalcMethods(FCalc): flag = 1; operator = 1; operand = 0; def __init__(self,parent = None,name = None,modal = 0,fl = 0): FCalc.__init__(self,parent,name,modal,fl) if name == None: self.setName('FCalcMethods') def sqrt(self): self.operand = self.Display.text() if str(self.operand) == "": self.operand = QString ("0") result = sqrt(float(str(self.operand))) if result == "": result = 0 self.Display.clear() display = QString (str(result)) self.Display.insert(display) self.operator = -1; self.operand = display; def equals(self): if str(self.operand) == "": self.operand = QString ("0") operand2 = self.Display.text() if str(operand2) == "": operand2 = QString ("0") if self.operator == 1: result = float(str(self.operand)) + float(str(operand2)) elif self.operator == 2: result = float(str(self.operand)) - float(str(operand2)) elif self.operator == 3: result = float(str(self.operand)) * float(str(operand2)) elif self.operator == 4: result = float(str(self.operand)) / float(str(operand2)) elif self.operator == 5: result = float(str(self.operand)) % float(str(operand2)) if result == "": result = 0 self.Display.clear() display = QString (str(result)) self.Display.insert(display) self.operator = -1; self.operand = display; def add(self): self.operand = self.Display.text() self.Display.clear() self.operator = 1; def subtract(self): self.operand = self.Display.text() self.Display.clear() self.operator = 2; def multiply(self): self.operand = self.Display.text() self.Display.clear() self.operator = 3; def divide(self): self.operand = self.Display.text() self.Display.clear() self.operator = 4; def modulo(self): self.operand = self.Display.text() self.Display.clear() self.operator = 5; def allClear(self): self.Display.clear() self.operator = -1; self.operand = 0; def backSpace(self): self.Display.backspace() def printSign(self): display = str(self.Display.text()) self.Display.clear() if display != "": value = float(display) if value == 0: result = -1 else: result = value * -1 else: result = -1 display = QString (str(result)) self.Display.insert (display) def printDot(self): if self.flag == 0: self.Display.insert (".") else: self.Display.clear() self.Display.insert (".") self.flag = 0 def print0(self): if self.flag == 0: self.Display.insert ("0") else: self.Display.clear() self.Display.insert ("0") self.flag = 0 def print1(self): if self.flag == 0: self.Display.insert ("1") else: self.Display.clear() self.Display.insert ("1") self.flag = 0 def print2(self): if self.flag == 0: self.Display.insert ("2") else: self.Display.clear() self.Display.insert ("2") self.flag = 0 def print3(self): if self.flag == 0: self.Display.insert ("3") else: self.Display.clear() self.Display.insert ("3") self.flag = 0 def print4(self): if self.flag == 0: self.Display.insert ("4") else: self.Display.clear() self.Display.insert ("4") self.flag = 0 def print5(self): if self.flag == 0: self.Display.insert ("5") else: self.Display.clear() self.Display.insert ("5") self.flag = 0 def print6(self): if self.flag == 0: self.Display.insert ("6") else: self.Display.clear() self.Display.insert ("6") self.flag = 0 def print7(self): if self.flag == 0: self.Display.insert ("7") else: self.Display.clear() self.Display.insert ("7") self.flag = 0 def print8(self): if self.flag == 0: self.Display.insert ("8") else: self.Display.clear() self.Display.insert ("8") self.flag = 0 def print9(self): if self.flag == 0: self.Display.insert ("9") else: self.Display.clear() self.Display.insert ("9") self.flag = 0 # main function of the program def main (): QApplication.setColorSpec(QApplication.CustomColor) a = QApplication(sys.argv) w = FCalcMethods() a.setMainWidget(w) w.show() a.exec_loop() if __name__ == '__main__': main()