def buttonClicked(self): if self.display.text() == 'Error!': self.display.setText('') button = self.sender() key = button.text() if key == '=': try: result = str(eval(self.display.text())) except: result = 'Error!' self.display.setText(result) elif key == 'C': self.display.clear() elif key == constantList[0]: self.display.setText(self.display.text() + '3.141592') elif key == constantList[1]: self.display.setText(self.display.text() + '3E+8') elif key == constantList[2]: self.display.setText(self.display.text() + '340') elif key == constantList[3]: self.display.setText(self.display.text() + '1.5E+8') elif key == functionList[0]: n = self.display.text() value = calcFunctions.factorial(n) self.display.setText(str(value)) elif key == functionList[1]: n = self.display.text() value = calcFunctions.decToBin(n) self.display.setText(str(value)) elif key == functionList[2]: n = self.display.text() value = calcFunctions.binToDec(n) self.display.setText(str(value)) elif key == functionList[3]: n = self.display.text() value = calcFunctions.decToRoman(n) self.display.setText(str(value)) elif key == functionList[4]: n = self.display.text() value = calcFunctions.romanToDec(n) self.display.setText(str(value)) else: self.display.setText(self.display.text() + key)
def buttonClicked(self): if self.error: self.display.clear() self.error = False button = self.sender() key = button.text() ConstantGroups = { 'pi': '3.141592', '빛의 이동 속도 (m/s)': '3E+8', '소리의 이동 속도 (m/s)': '340', '태양과의 평균 거리 (km)': '1.5E+8' } if key == '=': try: result = str(eval(self.display.text())) except: result = 'Error!' self.error = True self.display.setText(result) elif key == 'C': self.display.clear() elif key in constantList: self.display.setText(self.display.text() + ConstantGroups[key]) elif key == functionList[0]: n = self.display.text() value = factorial(n) self.display.setText(str(value)) elif key == functionList[1]: n = self.display.text() value = decToBin(n) self.display.setText(str(value)) elif key == functionList[2]: n = self.display.text() value = binToDec(n) self.display.setText(str(value)) else: self.display.setText(self.display.text() + key)
def function(calc, key) : if key == 'factorial (!)': n = calc.display.text() value = calcFunctions.factorial(n) calc.display.setText(str(value)) elif key == '-> binary': n = calc.display.text() value = calcFunctions.decToBin(n) calc.display.setText(str(value)) elif key == 'binary -> dec': n = calc.display.text() value = calcFunctions.binToDec(n) calc.display.setText(str(value)) elif key == '-> roman': n = calc.display.text() value = calcFunctions.decToRoman(n) calc.display.setText(str(value))
def buttonClicked(self): if self.display.text() == 'Error!': self.display.setText('') button = self.sender() key = button.text() #리스트 생성 consList = { 'pi': 3.141592, '빛의 이동 속도 (m/s)': 3E+8, '소리의 이동 속도 (m/s)': 340, '태양과의 평균 거리 (km)': 1.5E+8 } funcList = { 'factorial (!)': calcFunctions.factorial(self.display.text()), '-> binary': calcFunctions.decToBin(self.display.text()), 'binary -> dec': calcFunctions.binToDec(self.display.text()), '-> roman': calcFunctions.decToRoman(self.display.text()) } if key == '=': try: result = str(eval(self.display.text())) except: result = 'Error!' self.display.setText(result) elif key == 'C': self.display.clear() elif key in consList.keys(): for conskey in consList.keys(): if key == conskey: self.display.setText(self.display.text() + str(consList[key])) elif key in funcList.keys(): for funckey in funcList.keys(): if key == funckey: value = funcList[key] self.display.setText(str(value)) else: self.display.setText(self.display.text() + key)
def buttonClicked(self): if self.display.text() == 'Error!': self.display.setText('') button = self.sender() key = button.text() if key == '=': self.getResult() elif key == 'C': self.display.clear() elif key == constantList[0]: self.display.setText(self.display.text() + '3.141592') elif key == constantList[1]: self.display.setText(self.display.text() + '3E+8') elif key == constantList[2]: self.display.setText(self.display.text() + '340') elif key == constantList[3]: self.display.setText(self.display.text() + '1.5E+8') elif key == functionList[0]: n = self.display.text() value = calcFunctions.factorial(n) self.display.setText(str(value)) elif key == functionList[1]: n = self.display.text() value = calcFunctions.decToBin(n) self.display.setText(str(value)) elif key == functionList[2]: n = self.display.text() value = calcFunctions.binToDec(n) self.display.setText(str(value)) elif key == functionList[3]: n = float(self.display.text()) value = math.cos(n) self.display.setText(str(value)) elif key == functionList[4]: n = float(self.display.text()) value = math.sin(math.degrees(n)) self.display.setText(str(value)) else: self.display.setText(self.display.text() + key)
def buttonClicked(self): if self.display.text() == 'Error!': self.display.setText('') button = self.sender() key = button.text() constFunc = lambda x: self.display.setText(self.display.text() + x) constValues = { constantList[0]: '3.141592', constantList[1]: '3E+8', constantList[2]: '340', constantList[3]: '1.5E8', } if key == '=': try: result = str(eval(self.display.text())) except: result = 'Error!' self.display.setText(result) elif key == 'C': self.display.clear() elif key in constValues.keys(): constFunc(constValues[key]) elif key == functionList[0]: n = self.display.text() value = calcFunctions.factorial(n) self.display.setText(str(value)) elif key == functionList[1]: n = self.display.text() value = calcFunctions.decToBin(n) self.display.setText(str(value)) elif key == functionList[2]: n = self.display.text() value = calcFunctions.binToDec(n) self.display.setText(str(value)) elif key == functionList[3]: n = self.display.text() value = calcFunctions.decToRoman(n) self.display.setText(str(value)) else: self.display.setText(self.display.text() + key)
def buttonClicked(self): self.count = 0 list1 = ["3.141592", "3E+8", "340", "1.5E+8", "2.718281"] if self.display.text() == 'Error!' or self.display.text() == '0': self.display.setText('') button = self.sender() key = button.text() if self.count == 1: self.display.setText('') self.count = 0 if key == '=': try: result = str(eval(self.display.text())) self.count += 1 except: result = 'Error!' self.display.setText(result) elif key == 'C': self.display.setText('0') elif key in constantList: for i in range(0, len(list1)): if key == constantList[i]: self.display.setText(self.display.text() + list1[i]) elif key in functionList: n = self.display.text() if key == functionList[0]: value = calcFunctions.factorial(n) elif key == functionList[1]: value = calcFunctions.decToBin(n) elif key == functionList[2]: value = calcFunctions.binToDec(n) elif key == functionList[3]: value = calcFunctions.decToRoman(n) elif key == functionList[4]: value = calcFunctions.squareRoot(n) self.display.setText((str(value))) else: self.display.setText(self.display.text() + key)
def buttonClicked(self): if self.display.text() == 'Error!': self.display.setText('') button = self.sender() key = button.text() if key == '=': try: result = str(eval(self.display.text())) # 0으로 나눴을 때 예외처리 except ZeroDivisionError: result = '0으로 나눌 수 없습니다' except: result = 'Error!' self.display.setText(result) elif key == 'C': self.display.clear() # 반복을 줄임 elif key in constantDic: self.display.setText(constantDic[key]) # function도 딕셔너리를 통해 반복을 줄이고 싶었으나 실패함 elif key in functionList[0]: n = self.display.text() value = calcFunctions.factorial(n) self.display.setText(str(value)) elif key == functionList[1]: n = self.display.text() value = calcFunctions.decToBin(n) self.display.setText(str(value)) elif key == functionList[2]: n = self.display.text() value = calcFunctions.binToDec(n) self.display.setText(str(value)) elif key == functionList[3]: n = self.display.text() value = calcFunctions.decToRoman(n) self.display.setText(str(value)) else: self.display.setText(self.display.text() + key)
def buttonClicked(self): if self.display.text() == 'Error!': self.display.setText('') button = self.sender() key = button.text() flag = False contents = {0: '3.141592', 1: '3E+8', 2: '340', 3: '1.5E+8'} n = self.display.text() function_dic = {0: calcFunctions.factorial(n), 1: calcFunctions.decToBin(n), 2: calcFunctions.binToDec(n), 3: calcFunctions.decToRoman(n)} for idx in range(4): if key == constantList[idx]: self.display.setText(self.display.text() + contents[idx]) flag = True for idx in range(4): if key == functionList[idx]: value = function_dic[idx] self.display.setText(str(value)) flag = True if not flag: if key == '=': try: result = str(eval(self.display.text())) except: result = 'Error!' self.display.setText(result) elif key == 'C': self.display.clear() else: self.display.setText(self.display.text() + key)
def buttonClicked(self): if self.display.text() == 'Error!': self.display.setText('') button = self.sender() key = button.text() if key == '=': try: result = str(eval(self.display.text())) except: result = 'Error!' self.display.setText(result) elif key == 'C': self.display.clear() elif key in constantList: #for문과 list를 사용하여 단축 conLi = ['3.141592', '3E+8', '340', '1.5E+8'] for i in range(4): if key == constantList[i]: self.display.setText(self.display.text() + conLi[i]) elif key in functionList: #for문과 리스트를 사용하여 단축 n = self.display.text() fun = [ calcFunctions.factorial(n), calcFunctions.decToBin(n), calcFunctions.binToDec(n), calcFunctions.decToRoman(n) ] #getattr 사용해서 단축 가능 for i in range(4): if key == functionList[i]: value = fun[i] self.display.setText(str(value)) else: self.display.setText(self.display.text() + key)
def buttonClicked(self): if self.display.text() == 'Error!': self.display.setText('') button = self.sender() key = button.text() if key == '=': try: result = str(eval(self.display.text())) except: result = 'Error!' self.display.setText(result) elif key == 'C': self.display.clear() for i in constantList.keys(): if key == i: self.display.setText(constantList[i]) for j in range.functionList(0, 4): if functionList[j]: n = self.display.text() fun = [ calcFunctions.factorial(n), calcFunctions.decToBin(n), calcFunctions.binToDec(n), calcFunctions.decToRoman(n) ] value = fun[j] self.display.setText(str(value)) elif key == 'C': self.display.clear() else: self.display.setText(self.display.text() + key)
def buttonClicked(self): if self.display.text() == 'Error!': self.display.setText('') button = self.sender() key = button.text() if key == '=': try: result = str(eval(self.display.text())) except: result = 'Error!' self.display.setText(result) elif key == 'C': self.display.clear() elif key in constantList: keyIndex = constantList.index(key) self.display.setText(self.display.text() + constantResult[keyIndex]) elif key in functionList: n = eval(self.display.text()) functionResult = [ calcFunctions.factorial(n), calcFunctions.decToBin(n), calcFunctions.binToDec(n), calcFunctions.decToRoman(n) ] keyIndex = functionList.index(key) value = functionResult[keyIndex] self.display.setText(str(value)) else: self.display.setText(self.display.text() + key)
def buttonClicked(self): if self.display.text() == 'Error!': self.display.setText('') button = self.sender() key = button.text() #추가 리스트 numList = {'pi' : 3.141592,'빛의 이동 속도 (m/s)' : 3E+8,'소리의 이동 속도 (m/s)' : 340,'태양과의 평균 거리 (km)' : 1.5E+8} performList = {'factorial (!)' : calcFunctions.factorial(self.display.text()),'-> binary' : calcFunctions.decToBin(self.display.text()), 'binary -> dec': calcFunctions.binToDec(self.display.text()),'dec -> roman' : calcFunctions.decToRoman(self.display.text()),'roman -> dec' : calcFunctions.RomanTodec(self.display.text())} if key == '=': try: result = str(eval(self.display.text())) except: result = 'Error!' self.display.setText(result) elif key == 'C': self.display.clear() #줄인 코드 1 elif key in numList.keys(): for numkey in numList.keys(): if key == numkey: self.display.setText(self.display.text() + str(numList[key])) #줄인 코드 2 elif key in performList.keys(): for performkey in performList.keys(): if key == performkey: value = performList[key] self.display.setText(str(value)) else: self.display.setText(self.display.text() + key)
def buttonClicked(self): if self.display.text() == 'Error!': self.display.setText('') button = self.sender() key = button.text() if key == '=': try: result = str(eval(self.display.text())) self.flag = 1 except: result = 'Error!' self.display.setText(result) elif key == 'C': self.display.clear() elif key in constantList: self.idx = 0 self.display.setText(self.display.text() + self.constantVal[constantList.index(key)]) elif key in functionList: n = str(eval(self.display.text())) self.flag = 1 if functionList.index(key) == 0: n = str(eval(self.display.text())) value = calcFunctions.factorial(n) elif functionList.index(key) == 1: value = calcFunctions.decToBin(n) elif functionList.index(key) == 2: value = calcFunctions.binToDec(n) elif functionList.index(key) == 3: value = calcFunctions.decToRoman(n) self.display.setText(str(value)) elif key in operatorList: # 계산 결과에 괄호 연산자 버튼을 누르면 결과값이 나타나는 오류를 수정 if self.flag == 1 and key == "(": self.display.setText("Error!") self.flag = 0 #괄호가 열려있을 때에만 괄호를 닫을 수 있도록 만들었다. elif key == "(": self.display.setText(self.display.text() + key) self.parIdx = 1 elif self.parIdx == 1 and key == ")": self.display.setText(self.display.text() + key) self.parIdx = 0 elif self.parIdx == 0 and key == ")": self.display.setText(self.display.text()) #나머지의 경우에는 연산이 가능해지도록 한다. #연산자가 연속으로 중복되어 입력되는 것을 방지한다. else: if self.idx == 0: self.idx = 1 self.display.setText(self.display.text() + key) self.flag = 0 #계산이 끝났을 때 연산자가 아닌 숫자 버튼을 누르면, 새로운 수로 시작하지 않고 결과 뒤에 새로운 숫자가 쓰여지는 것을 방지한다. elif key in numPadList: self.idx = 0 if self.flag == 1 and key != "=": self.display.setText(key) self.flag = 0 #그리고 함수 계산 후에도 결과값 뒤에 새로운 숫자가 쓰여지는 것을 방지한다. elif self.flag == 1: self.display.setText(key) self.flag = 0 else: self.display.setText(self.display.text() + key) else: self.display.setText(self.display.text() + key)
def buttonClicked(self): self.flagCheck() button = self.sender() key = button.text() if key == "=": try: result = str(eval(self.display.text())) except: result = "Error!" self.display.setText(result) self.flag = "error" else: self.display.setText(result) elif key == "C": self.display.clear() self.flag = "" elif key in constantList: if self.flag != "number" and self.flag != "constant": idx = constantList.index(key) self.display.setText(self.display.text() + constantValueList[idx]) self.flag = "constant" elif key == functionList[0]: if self.display.text() != "": n = eval(self.display.text()) if isinstance(n, int): if 0 <= int(n) <= 17: value = factorial(int(n)) self.flag = "number" else: value = 'Error!' self.flag = "error" else: value = 'Error!' self.flag = "error" else: value = 'Error!' self.flag = "error" self.display.setText(str(value)) elif key == functionList[1]: if self.display.text() != "": n = str(eval(self.display.text())) value = calcFunctions.decToBin(n) if len(value) > 15: value = "Too long" self.flag = "error" else: self.flag = "bin" else: value = 'Error!' self.flag = "error" self.display.setText(str(value)) elif key == functionList[2]: if self.display.text() != "": n = str(eval(self.display.text())) value = calcFunctions.binToDec(n) if value == "Error!": self.flag = "error" else: self.flag = "number" else: value = 'Error!' self.flag = "error" self.display.setText(str(value)) elif key == functionList[3]: n = self.display.text() value = calcFunctions.decToRoman(n) self.display.setText(str(value)) elif key in operatorList: if self.flag == "bin": pass elif key == "(" or key == ")": self.display.setText(self.display.text() + key) elif self.flag != "operator": self.display.setText(self.display.text() + key) self.flag = "operator" else: self.flagCheck() if self.flag != "constant" and self.flag != "bin": if self.display.text() == "0": if key == "0": pass elif key == ".": self.display.setText(self.display.text() + key) else: self.display.setText(key) elif key == ".": if "." in self.display.text(): pass elif self.display.text() == "": pass else: self.display.setText(self.display.text() + key) else: self.display.setText(self.display.text() + key) self.flag = "number" print(self.flag)
def buttonClicked(self): if self.display.text() == 'Error!': self.display.setText('') button = self.sender() key = button.text() if key == '=': try: result = str(eval(self.display.text())) except: result = 'Error!' self.display.setText(result) self.erasecount = 1 elif key == 'C': self.display.clear() elif key == constantList[0]: self.display.setText('3.141592') elif key == constantList[1]: self.display.setText('3E+8') elif key == constantList[2]: self.display.setText('340') elif key == constantList[3]: self.display.setText('1.5E+8') elif key == functionList[0]: try: n = str(eval(self.display.text())) value = calcFunctions.factorial(n) except: value = 'Error!' self.display.setText(str(value)) elif key == functionList[1]: n = self.display.text() value = calcFunctions.decToBin(n) self.display.setText(str(value)) elif key == functionList[2]: n = self.display.text() value = calcFunctions.binToDec(n) self.display.setText(str(value)) elif key == functionList[3]: n = self.display.text() value = calcFunctions.decToRoman(n) self.display.setText(str(value)) elif key == additionalconstantList[0]: n = self.display.text() if n == '': value = 1 else: value = int(n) self.display.setText(str(value)) elif key == additionalconstantList[1]: n = self.display.text() if n == '': value = 60 value = int(n) * 60 self.display.setText(str(value)) elif key == additionalconstantList[2]: n = self.display.text() if n == '': value = 3600 else: value = int(n) * 3600 self.display.setText(str(value)) elif key == additionalconstantList[3]: n = self.display.text() if n == '': value = 60 * 3600 else: value = int(n) * 60 * 3600 self.display.setText(str(value)) elif key == additionalfunctionList[0]: n = self.display.text() try: a = int(n) value = "yes" for i in range(2, a): if a % i == 0: value = "no" break except: value = "Error!" self.display.setText(str(value)) self.erasecount = 1 elif key == additionalfunctionList[1]: n = self.display.text() try: value = float(n) * float(n) except: value = "Error!" self.display.setText(str(value)) elif key == additionalfunctionList[2]: n = self.display.text() try: value = oct(int(n)) except: value = "Error!" self.display.setText(str(value)) elif key == additionalfunctionList[3]: n = self.display.text() try: value = hex(int(n)) except: value = "Error!" self.display.setText(str(value)) else: if self.erasecount == 1: self.erasecount = 0 self.display.setText('') self.display.setText(self.display.text() + key)
def buttonClicked(self): if 'clear' in init: self.display.clear() init.clear() button = self.sender() key = button.text() ## 06 + 3처럼 앞에 0이 있을 경우 에러처리가 나던 것을 처리하기 위해 ## 맨앞에 0이 나타나지 않을때까지 0을 지우고 연산기호 뒤의 문자도 0이면 없애는 식으로 하려했으나 ## 연산기호를 여러번 쓰는부분을 어떻게 코딩해야할지 몰라 남겼습니다. '''idx = 0 if key == '=': if '+' or '-' or '*' or '/' in self.display.text(): del0 = '' for i in range(1, len(self.display.text())): # 연산기호가 몇번째에 있는 지 확인 if self.display.text()[i] == '+': idx = i if self.display.text()[i] == '-': idx = i if self.display.text()[i] == '*': idx = i if self.display.text()[i] == '/': idx = i del1 = '' ## 첫째 인자에 0이 있는 경우 count = 1 print(idx) while self.display.text()[0] == '0': count += 1 # 첫째 인자 0제거 for i in range(1, len(self.display.text())): del0 += self.display.text()[i] self.display.setText(del0) # 둘째 인자 0 유무 확인 while del0[idx + 1] == '0': # 0이 있네? for i in range(1, len(del0)): if i == idx: continue else: del1 += del0[i] self.display.setText(del1) # 둘째 인자에 0이 없는 경우 else: self.display.setText(str(eval(del0))) ## 첫재 인자에 0이 없는우 경우 else: ## 둘째 인자에 0이 있는 경우 if self.display.text()[idx + 1] == 0: print('22') ## 둘째 인장에 0이 없는 경우 else: try: result = str(eval(self.display.text())) except: result = 'Error!' self.display.setText(result)''' if key == '=': ## 계산 입력 후 값 초기화를 위해 init라는 리스트를 생성 ## ZeroDivisionErro 처리 if '/' in self.display.text(): try: result = str(eval(self.display.text())) except ZeroDivisionError: result = 'ZeroDivisionError' init.append('clear') self.display.setText(result) else: try: result = str(eval(self.display.text())) except: result = 'Error!' init.append('clear') self.display.setText(result) elif key == 'C': self.display.clear() elif key == constantList[0]: self.display.setText(self.display.text() + '3.141592') elif key == constantList[1]: self.display.setText(self.display.text() + '3E+8') elif key == constantList[2]: self.display.setText(self.display.text() + '340') elif key == constantList[3]: self.display.setText(self.display.text() + '1.5E+8') elif key == functionList[0]: n = self.display.text() value = calcFunctions.factorial(n) self.display.setText(str(value)) init.append('clear') elif key == functionList[1]: n = self.display.text() value = calcFunctions.decToBin(n) self.display.setText(str(value)) elif key == functionList[2]: n = self.display.text() value = calcFunctions.binToDec(n) self.display.setText(str(value)) elif key == functionList[3]: n = self.display.text() value = calcFunctions.decToRoman(n) self.display.setText(str(value)) else: self.display.setText(self.display.text() + key) ## 계산 입력