ㅇ 프로젝트/(Toy)_project
pyqt 계산기 만들기
BrainKimDu
2022. 11. 30. 12:00
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import uic
# 보통은 이름을 같게만든다.-> 꼭 같을 필요는 없다.
from_class = uic.loadUiType("calculator.ui")[0]
class WindowClass(QMainWindow, from_class):
def __init__(self):
super().__init__() # 부모클래스 생성자 실행
self.setupUi(self)
self.start = "0"
self.result = 0.
self.left = ""
self.right = ""
self.cal = ""
self.PB_0.clicked.connect(self.button0_Clicked)
self.PB_1.clicked.connect(self.button1_Clicked)
self.PB_2.clicked.connect(self.button2_Clicked)
self.PB_3.clicked.connect(self.button3_Clicked)
self.PB_4.clicked.connect(self.button4_Clicked)
self.PB_5.clicked.connect(self.button5_Clicked)
self.PB_6.clicked.connect(self.button6_Clicked)
self.PB_7.clicked.connect(self.button7_Clicked)
self.PB_8.clicked.connect(self.button8_Clicked)
self.PB_9.clicked.connect(self.button9_Clicked)
self.PB_AC.clicked.connect(self.button_AC_Clicked)
self.PB_CE.clicked.connect(self.button_CE_Clicked)
self.PB_PM.clicked.connect(self.button_PM_Clicked)
self.PB_Diff.clicked.connect(self.button_Diff_Clicked)
self.PB_Mul.clicked.connect(self.button_Mul_Clicked)
self.PB_Plus.clicked.connect(self.button_Plus_Clicked)
self.PB_Minus.clicked.connect(self.button_Minus_Clicked)
self.PB_POINT.clicked.connect(self.button_Point_Clicked)
self.PB_Equal.clicked.connect(self.button_Equal_Clicked)
self.lineEdit.setText(self.start)
self.setWindowTitle("Hello. Calculator!")
def button0_Clicked(self):
if self.cal == "":
if self.left == "0":
return
else:
self.left = self.left + "0"
self.lineEdit.setText(self.left)
else:
if self.right == "0":
return
else:
self.right = self.right + "0"
self.lineEdit.setText(self.right)
def button1_Clicked(self):
if self.cal == "":
self.left = self.left + "1"
self.lineEdit.setText(self.left)
else:
self.right = self.right + "1"
self.lineEdit.setText(self.right)
def button2_Clicked(self):
if self.cal == "":
self.left = self.left + "2"
self.lineEdit.setText(self.left)
else:
self.right = self.right + "2"
self.lineEdit.setText(self.right)
def button3_Clicked(self):
if self.cal == "":
self.left = self.left + "3"
self.lineEdit.setText(self.left)
else:
self.right = self.right + "3"
self.lineEdit.setText(self.right)
def button4_Clicked(self):
if self.cal == "":
self.left = self.left + "4"
self.lineEdit.setText(self.left)
else:
self.right = self.right + "4"
self.lineEdit.setText(self.right)
def button5_Clicked(self):
if self.cal == "":
self.left = self.left + "5"
self.lineEdit.setText(self.left)
else:
self.right = self.right + "5"
self.lineEdit.setText(self.right)
def button6_Clicked(self):
if self.cal == "":
self.left = self.left + "6"
self.lineEdit.setText(self.left)
else:
self.right = self.right + "6"
self.lineEdit.setText(self.right)
def button7_Clicked(self):
if self.cal == "":
self.left = self.left + "7"
self.lineEdit.setText(self.left)
else:
self.right = self.right + "7"
self.lineEdit.setText(self.right)
def button8_Clicked(self):
if self.cal == "":
self.left = self.left + "8"
self.lineEdit.setText(self.left)
else:
self.right = self.right + "8"
self.lineEdit.setText(self.right)
def button9_Clicked(self):
if self.cal == "":
self.left = self.left + "9"
self.lineEdit.setText(self.left)
else:
self.right = self.right + "9"
self.lineEdit.setText(self.right)
def button_AC_Clicked(self):
self.left = ""
self.right = ""
self.cal = ""
self.lineEdit.setText(self.start)
def button_CE_Clicked(self):
if self.cal == "":
self.left = ""
self.lineEdit.setText(self.left)
else:
self.right = ""
self.lineEdit.setText(self.right)
def button_PM_Clicked(self):
if self.cal == "":
if self.left == "":
return
else:
if self.left[0] == '-':
self.left = self.left[0:]
self.lineEdit.setText(self.left)
else:
self.left = "-" + self.left
self.lineEdit.setText(self.left)
else:
if self.left == "":
return
else:
if self.right[0] == '-':
self.right = self.right[0:]
self.lineEdit.setText(self.right)
else:
self.right = "-" + self.right
self.lineEdit.setText(self.right)
def button_Diff_Clicked(self):
self.cal = "/"
self.lineEdit.setText(self.start)
def button_Mul_Clicked(self):
self.cal = "*"
self.lineEdit.setText(self.start)
def button_Plus_Clicked(self):
self.cal = "+"
self.lineEdit.setText(self.start)
def button_Minus_Clicked(self):
self.cal = "-"
self.lineEdit.setText(self.start)
def button_Point_Clicked(self):
if self.cal == "":
self.left = self.left + "."
self.lineEdit.setText(self.left)
else:
self.right = self.right + "."
self.lineEdit.setText(self.right)
def button_Equal_Clicked(self):
if self.cal != "":
if self.right != "" :
if self.cal == "+":
self.result = float(self.left) + float(self.right)
self.left = ""
self.right = ""
self.cal = ""
self.lineEdit.setText(str(self.result))
if self.cal == "-":
self.result = float(self.left) - float(self.right)
self.left = ""
self.right = ""
self.cal = ""
self.lineEdit.setText(str(self.result))
if self.cal == "/":
if self.right != "0":
self.result = float(self.left) / float(self.right)
self.left = ""
self.right = ""
self.cal = ""
self.lineEdit.setText(str(self.result))
else:
self.lineEdit.setText("can not divide 0")
self.right = ""
if self.cal == "*":
self.result = float(self.left) * float(self.right)
self.left = ""
self.right = ""
self.cal = ""
self.lineEdit.setText(str(self.result))
else:
self.lineEdit.setText("Right is empty")
pass
else:
self.left = ""
self.lineEdit.setText(str(self.start))
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindows = WindowClass()
myWindows.show()
sys.exit(app.exec_())
일단 구현
강사님의 조언을 듣고
파이썬의 람다라는 것을 이용하여 하나의 함수로 구현한다
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import uic
# 보통은 이름을 같게만든다.-> 꼭 같을 필요는 없다.
from_class = uic.loadUiType("calculator.ui")[0]
class WindowClass(QMainWindow, from_class):
def __init__(self):
super().__init__() # 부모클래스 생성자 실행
self.setupUi(self)
self.start = "0"
self.result = 0.
self.left = ""
self.right = ""
self.cal = ""
self.PB_0.clicked.connect(lambda: self.buttonClicked("0"))
self.PB_1.clicked.connect(lambda: self.buttonClicked("1"))
self.PB_2.clicked.connect(lambda: self.buttonClicked("2"))
self.PB_3.clicked.connect(lambda: self.buttonClicked("3"))
self.PB_4.clicked.connect(lambda: self.buttonClicked("4"))
self.PB_5.clicked.connect(lambda: self.buttonClicked("5"))
self.PB_6.clicked.connect(lambda: self.buttonClicked("6"))
self.PB_7.clicked.connect(lambda: self.buttonClicked("7"))
self.PB_8.clicked.connect(lambda: self.buttonClicked("8"))
self.PB_9.clicked.connect(lambda: self.buttonClicked("9"))
self.PB_POINT.clicked.connect(lambda: self.buttonClicked("."))
self.PB_AC.clicked.connect(self.button_AC_Clicked)
self.PB_CE.clicked.connect(self.button_CE_Clicked)
self.PB_PM.clicked.connect(self.button_PM_Clicked)
self.PB_Diff.clicked.connect(lambda: self.buttonCal("/"))
self.PB_Mul.clicked.connect(lambda: self.buttonCal("*"))
self.PB_Plus.clicked.connect(lambda: self.buttonCal("+"))
self.PB_Minus.clicked.connect(lambda: self.buttonCal("-"))
self.PB_Equal.clicked.connect(self.button_Equal_Clicked)
self.lineEdit.setText(self.start)
self.setWindowTitle("Hello. Calculator!")
def buttonClicked(self, num):
if num == "0":
if self.cal == "":
if self.left == "0":
return
else:
self.left = self.left + "0"
self.lineEdit.setText(self.left)
else:
if self.right == "0":
return
else:
self.right = self.right + "0"
self.lineEdit.setText(self.right)
else:
if self.cal == "":
self.left = self.left + num
self.lineEdit.setText(self.left)
else:
self.right = self.right + num
self.lineEdit.setText(self.right)
pass
def buttonCal(self, func_cal):
self.cal = func_cal
self.lineEdit.setText(self.start)
def button_AC_Clicked(self):
self.left = ""
self.right = ""
self.cal = ""
self.lineEdit.setText(self.start)
def button_CE_Clicked(self):
if self.cal == "":
self.left = ""
self.lineEdit.setText(self.left)
else:
self.right = ""
self.lineEdit.setText(self.right)
def button_PM_Clicked(self):
if self.cal == "":
if self.left == "":
return
else:
if self.left[0] == '-':
self.left = self.left[0:]
self.lineEdit.setText(self.left)
else:
self.left = "-" + self.left
self.lineEdit.setText(self.left)
else:
if self.left == "":
return
else:
if self.right[0] == '-':
self.right = self.right[0:]
self.lineEdit.setText(self.right)
else:
self.right = "-" + self.right
self.lineEdit.setText(self.right)
def button_Equal_Clicked(self):
if self.cal != "":
if self.right != "" :
if self.cal == "+":
self.result = float(self.left) + float(self.right)
self.left = ""
self.right = ""
self.cal = ""
self.lineEdit.setText(str(self.result))
if self.cal == "-":
self.result = float(self.left) - float(self.right)
self.left = ""
self.right = ""
self.cal = ""
self.lineEdit.setText(str(self.result))
if self.cal == "/":
if self.right != "0":
self.result = float(self.left) / float(self.right)
self.left = ""
self.right = ""
self.cal = ""
self.lineEdit.setText(str(self.result))
else:
self.lineEdit.setText("can not divide 0")
self.right = ""
if self.cal == "*":
self.result = float(self.left) * float(self.right)
self.left = ""
self.right = ""
self.cal = ""
self.lineEdit.setText(str(self.result))
else:
self.lineEdit.setText("Right is empty")
pass
else:
self.left = ""
self.lineEdit.setText(str(self.start))
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindows = WindowClass()
myWindows.show()
sys.exit(app.exec_())
결과물
4 + 4 =
문제는 이게 float 형이라 정답결과에 .0이 출력됨
이거 좀 지워보겠음
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import uic
# 보통은 이름을 같게만든다.-> 꼭 같을 필요는 없다.
from_class = uic.loadUiType("calculator.ui")[0]
class WindowClass(QMainWindow, from_class):
def __init__(self):
super().__init__() # 부모클래스 생성자 실행
self.setupUi(self)
self.start = "0"
self.result = 0.
self.left = ""
self.right = ""
self.cal = ""
self.PB_0.clicked.connect(lambda: self.buttonClicked("0"))
self.PB_1.clicked.connect(lambda: self.buttonClicked("1"))
self.PB_2.clicked.connect(lambda: self.buttonClicked("2"))
self.PB_3.clicked.connect(lambda: self.buttonClicked("3"))
self.PB_4.clicked.connect(lambda: self.buttonClicked("4"))
self.PB_5.clicked.connect(lambda: self.buttonClicked("5"))
self.PB_6.clicked.connect(lambda: self.buttonClicked("6"))
self.PB_7.clicked.connect(lambda: self.buttonClicked("7"))
self.PB_8.clicked.connect(lambda: self.buttonClicked("8"))
self.PB_9.clicked.connect(lambda: self.buttonClicked("9"))
self.PB_POINT.clicked.connect(lambda: self.buttonClicked("."))
self.PB_AC.clicked.connect(self.button_AC_Clicked)
self.PB_CE.clicked.connect(self.button_CE_Clicked)
self.PB_PM.clicked.connect(self.button_PM_Clicked)
self.PB_Diff.clicked.connect(lambda: self.buttonCal("/"))
self.PB_Mul.clicked.connect(lambda: self.buttonCal("*"))
self.PB_Plus.clicked.connect(lambda: self.buttonCal("+"))
self.PB_Minus.clicked.connect(lambda: self.buttonCal("-"))
self.PB_Equal.clicked.connect(self.button_Equal_Clicked)
self.lineEdit.setText(self.start)
self.setWindowTitle("Hello. Calculator!")
def buttonClicked(self, num):
if num == "0":
if self.cal == "":
if self.left == "0":
return
else:
self.left = self.left + "0"
self.lineEdit.setText(self.left)
else:
if self.right == "0":
return
else:
self.right = self.right + "0"
self.lineEdit.setText(self.right)
else:
if self.cal == "":
self.left = self.left + num
self.lineEdit.setText(self.left)
else:
self.right = self.right + num
self.lineEdit.setText(self.right)
pass
def buttonCal(self, func_cal):
self.cal = func_cal
self.lineEdit.setText(self.start)
def button_AC_Clicked(self):
self.left = ""
self.right = ""
self.cal = ""
self.lineEdit.setText(self.start)
def button_CE_Clicked(self):
if self.cal == "":
self.left = ""
self.lineEdit.setText(self.left)
else:
self.right = ""
self.lineEdit.setText(self.right)
def button_PM_Clicked(self):
if self.cal == "":
if self.left == "":
return
else:
if self.left[0] == '-':
self.left = self.left[0:]
self.lineEdit.setText(self.left)
else:
self.left = "-" + self.left
self.lineEdit.setText(self.left)
else:
if self.left == "":
return
else:
if self.right[0] == '-':
self.right = self.right[0:]
self.lineEdit.setText(self.right)
else:
self.right = "-" + self.right
self.lineEdit.setText(self.right)
def button_Equal_Clicked(self):
if self.cal != "":
if self.right != "" :
if self.cal == "+":
self.result = float(self.left) + float(self.right)
self.left = ""
self.right = ""
self.cal = ""
if str(self.result)[-2:] == '.0':
self.lineEdit.setText(str(self.result)[:-2])
else:
self.lineEdit.setText(str(self.result))
if self.cal == "-":
self.result = float(self.left) - float(self.right)
self.left = ""
self.right = ""
self.cal = ""
if str(self.result)[-2:] == '.0':
self.lineEdit.setText(str(self.result)[:-2])
else:
self.lineEdit.setText(str(self.result))
if self.cal == "/":
if self.right != "0":
self.result = float(self.left) / float(self.right)
self.left = ""
self.right = ""
self.cal = ""
if str(self.result)[-2:] == '.0':
self.lineEdit.setText(str(self.result)[:-2])
else:
self.lineEdit.setText(str(self.result))
else:
self.lineEdit.setText("can not divide 0")
self.right = ""
if self.cal == "*":
self.result = float(self.left) * float(self.right)
self.left = ""
self.right = ""
self.cal = ""
if str(self.result)[-2:] == '.0':
self.lineEdit.setText(str(self.result)[:-2])
else:
self.lineEdit.setText(str(self.result))
else:
self.lineEdit.setText("Right is empty")
pass
else:
self.left = ""
self.lineEdit.setText(str(self.start))
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindows = WindowClass()
myWindows.show()
sys.exit(app.exec_())
출력에서 문자열 슬라이싱을 이용해서 잘랐다.
10 + 10이 잘 나온다.
조금 결과창의 가독성 향상을 위해서
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import uic
# 보통은 이름을 같게만든다.-> 꼭 같을 필요는 없다.
from_class = uic.loadUiType("calculator.ui")[0]
class WindowClass(QMainWindow, from_class):
def __init__(self):
super().__init__() # 부모클래스 생성자 실행
self.setupUi(self)
self.start = "0"
self.result = 0.
self.left = ""
self.right = ""
self.cal = ""
self.PB_0.clicked.connect(lambda: self.buttonClicked("0"))
self.PB_1.clicked.connect(lambda: self.buttonClicked("1"))
self.PB_2.clicked.connect(lambda: self.buttonClicked("2"))
self.PB_3.clicked.connect(lambda: self.buttonClicked("3"))
self.PB_4.clicked.connect(lambda: self.buttonClicked("4"))
self.PB_5.clicked.connect(lambda: self.buttonClicked("5"))
self.PB_6.clicked.connect(lambda: self.buttonClicked("6"))
self.PB_7.clicked.connect(lambda: self.buttonClicked("7"))
self.PB_8.clicked.connect(lambda: self.buttonClicked("8"))
self.PB_9.clicked.connect(lambda: self.buttonClicked("9"))
self.PB_POINT.clicked.connect(lambda: self.buttonClicked("."))
self.PB_AC.clicked.connect(self.button_AC_Clicked)
self.PB_CE.clicked.connect(self.button_CE_Clicked)
self.PB_PM.clicked.connect(self.button_PM_Clicked)
self.PB_Diff.clicked.connect(lambda: self.buttonCal("/"))
self.PB_Mul.clicked.connect(lambda: self.buttonCal("*"))
self.PB_Plus.clicked.connect(lambda: self.buttonCal("+"))
self.PB_Minus.clicked.connect(lambda: self.buttonCal("-"))
self.PB_Equal.clicked.connect(self.button_Equal_Clicked)
self.lineEdit.setText("L = " + self.start)
self.setWindowTitle("Hello. Calculator!")
def buttonClicked(self, num):
if num == "0":
if self.cal == "":
if self.left == "0":
return
else:
self.left = self.left + "0"
self.lineEdit.setText("L = " + self.left)
else:
if self.right == "0":
return
else:
self.right = self.right + "0"
self.lineEdit.setText("R = " + self.right)
else:
if self.cal == "":
if self.left == "0":
self.left = ""
self.left = self.left + num
self.lineEdit.setText("L = " + self.left)
else:
self.left = self.left + num
self.lineEdit.setText("L = " + self.left)
else:
if self.right == "0":
self.right = ""
self.right = self.right + num
self.lineEdit.setText("R = " + self.right)
else:
self.right = self.right + num
self.lineEdit.setText("R = " + self.right)
def buttonCal(self, func_cal):
self.cal = func_cal
self.lineEdit.setText("R = " + self.start)
def button_AC_Clicked(self):
self.left = ""
self.right = ""
self.cal = ""
self.lineEdit.setText("L = " + self.start)
def button_CE_Clicked(self):
if self.cal == "":
self.left = ""
self.lineEdit.setText("L = " + self.left)
else:
self.right = ""
self.lineEdit.setText("R = " + self.right)
def button_PM_Clicked(self):
if self.cal == "":
if self.left == "":
return
else:
if self.left[0] == '-':
self.left = self.left[0:]
self.lineEdit.setText("L = " + self.left)
else:
self.left = "-" + self.left
self.lineEdit.setText("L = " + self.left)
else:
if self.left == "":
return
else:
if self.right[0] == '-':
self.right = self.right[0:]
self.lineEdit.setText("R = " + self.right)
else:
self.right = "-" + self.right
self.lineEdit.setText("R = " + self.right)
def button_Equal_Clicked(self):
if self.cal != "":
if self.right != "" :
if self.cal == "+":
self.result = float(self.left) + float(self.right)
self.left = ""
self.right = ""
self.cal = ""
if str(self.result)[-2:] == '.0':
self.lineEdit.setText("= " + str(self.result)[:-2])
else:
self.lineEdit.setText("= " + str(self.result))
if self.cal == "-":
self.result = float(self.left) - float(self.right)
self.left = ""
self.right = ""
self.cal = ""
if str(self.result)[-2:] == '.0':
self.lineEdit.setText("= " + str(self.result)[:-2])
else:
self.lineEdit.setText("= " + str(self.result))
if self.cal == "/":
if self.right != "0":
self.result = float(self.left) / float(self.right)
self.left = ""
self.right = ""
self.cal = ""
if str(self.result)[-2:] == '.0':
self.lineEdit.setText("= " + str(self.result)[:-2])
else:
self.lineEdit.setText("= " + str(self.result))
else:
self.lineEdit.setText("can not divide 0")
self.right = ""
if self.cal == "*":
self.result = float(self.left) * float(self.right)
self.left = ""
self.right = ""
self.cal = ""
if str(self.result)[-2:] == '.0':
self.lineEdit.setText("= " + str(self.result)[:-2])
else:
self.lineEdit.setText("= " + str(self.result))
else:
self.lineEdit.setText("Right is empty")
pass
else:
self.left = ""
self.lineEdit.setText("L = " + self.start)
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindows = WindowClass()
myWindows.show()
sys.exit(app.exec_())
bug fix
소수점 을 계속 누를 수 있던거 해결함
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import uic
# 보통은 이름을 같게만든다.-> 꼭 같을 필요는 없다.
from_class = uic.loadUiType("calculator.ui")[0]
class WindowClass(QMainWindow, from_class):
def __init__(self):
super().__init__() # 부모클래스 생성자 실행
self.setupUi(self)
self.start = "0"
self.result = 0.
self.left = ""
self.right = ""
self.cal = ""
self.PB_0.clicked.connect(lambda: self.buttonClicked("0"))
self.PB_1.clicked.connect(lambda: self.buttonClicked("1"))
self.PB_2.clicked.connect(lambda: self.buttonClicked("2"))
self.PB_3.clicked.connect(lambda: self.buttonClicked("3"))
self.PB_4.clicked.connect(lambda: self.buttonClicked("4"))
self.PB_5.clicked.connect(lambda: self.buttonClicked("5"))
self.PB_6.clicked.connect(lambda: self.buttonClicked("6"))
self.PB_7.clicked.connect(lambda: self.buttonClicked("7"))
self.PB_8.clicked.connect(lambda: self.buttonClicked("8"))
self.PB_9.clicked.connect(lambda: self.buttonClicked("9"))
self.PB_POINT.clicked.connect(lambda: self.buttonClicked("."))
self.PB_AC.clicked.connect(self.button_AC_Clicked)
self.PB_CE.clicked.connect(self.button_CE_Clicked)
self.PB_PM.clicked.connect(self.button_PM_Clicked)
self.PB_Diff.clicked.connect(lambda: self.buttonCal("/"))
self.PB_Mul.clicked.connect(lambda: self.buttonCal("*"))
self.PB_Plus.clicked.connect(lambda: self.buttonCal("+"))
self.PB_Minus.clicked.connect(lambda: self.buttonCal("-"))
self.PB_Equal.clicked.connect(self.button_Equal_Clicked)
self.lineEdit.setText("L = " + self.start)
self.setWindowTitle("Hello. Calculator!")
def buttonClicked(self, num):
if num == "0":
if self.cal == "":
if self.left == "0":
return
else:
self.left = self.left + "0"
self.lineEdit.setText("L = " + self.left)
else:
if self.right == "0":
return
else:
self.right = self.right + "0"
self.lineEdit.setText("R = " + self.right)
elif num == '.':
if self.cal == "":
if self.left == "" or self.left == "0":
self.left = ""
self.left = self.left + "0."
self.lineEdit.setText("L = " + self.left)
else:
if self.left.find('.') != -1:
return
else:
self.left = self.left + "."
self.lineEdit.setText("L = " + self.left)
else:
if self.right == "" or self.right == "0":
self.right = ""
self.right = self.right + "0."
self.lineEdit.setText("R = " + self.right)
else:
if self.right.find('.') != -1:
return
else:
self.right = self.right + "."
self.lineEdit.setText("R = " + self.right)
else:
if self.cal == "":
if self.left == "0":
self.left = ""
self.left = self.left + num
self.lineEdit.setText("L = " + self.left)
else:
self.left = self.left + num
self.lineEdit.setText("L = " + self.left)
else:
if self.right == "0":
self.right = ""
self.right = self.right + num
self.lineEdit.setText("R = " + self.right)
else:
self.right = self.right + num
self.lineEdit.setText("R = " + self.right)
def buttonCal(self, func_cal):
self.cal = func_cal
self.lineEdit.setText("R = " + self.start)
def button_AC_Clicked(self):
self.left = ""
self.right = ""
self.cal = ""
self.lineEdit.setText("L = " + self.start)
def button_CE_Clicked(self):
if self.cal == "":
self.left = ""
self.lineEdit.setText("L = " + self.left)
else:
self.right = ""
self.lineEdit.setText("R = " + self.right)
def button_PM_Clicked(self):
if self.cal == "":
if self.left == "":
return
else:
if self.left[0] == '-':
self.left = self.left[0:]
self.lineEdit.setText("L = " + self.left)
else:
self.left = "-" + self.left
self.lineEdit.setText("L = " + self.left)
else:
if self.left == "":
return
else:
if self.right[0] == '-':
self.right = self.right[0:]
self.lineEdit.setText("R = " + self.right)
else:
self.right = "-" + self.right
self.lineEdit.setText("R = " + self.right)
def button_Equal_Clicked(self):
if self.cal != "":
if self.right != "" :
if self.cal == "+":
self.result = float(self.left) + float(self.right)
self.left = ""
self.right = ""
self.cal = ""
if str(self.result)[-2:] == '.0':
self.lineEdit.setText("= " + str(self.result)[:-2])
else:
self.lineEdit.setText("= " + str(self.result))
if self.cal == "-":
self.result = float(self.left) - float(self.right)
self.left = ""
self.right = ""
self.cal = ""
if str(self.result)[-2:] == '.0':
self.lineEdit.setText("= " + str(self.result)[:-2])
else:
self.lineEdit.setText("= " + str(self.result))
if self.cal == "/":
if self.right != "0":
self.result = float(self.left) / float(self.right)
self.left = ""
self.right = ""
self.cal = ""
if str(self.result)[-2:] == '.0':
self.lineEdit.setText("= " + str(self.result)[:-2])
else:
self.lineEdit.setText("= " + str(self.result))
else:
self.lineEdit.setText("can not divide 0")
self.right = ""
if self.cal == "*":
self.result = float(self.left) * float(self.right)
self.left = ""
self.right = ""
self.cal = ""
if str(self.result)[-2:] == '.0':
self.lineEdit.setText("= " + str(self.result)[:-2])
else:
self.lineEdit.setText("= " + str(self.result))
else:
self.lineEdit.setText("Right is empty")
pass
else:
self.left = ""
self.lineEdit.setText("L = " + self.start)
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindows = WindowClass()
myWindows.show()
sys.exit(app.exec_())
추가로 들어온건 한줄로 표현하고 eval() 변수를 이용해서 계산하기
버그가 또나왔다 바로 연산자를 누르면 오류난다 수정해야함
시간나면 공학용 계산기도 만들어보자