Advertisement
AboodXD

Simple Calculator in Python 3

Sep 8th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.39 KB | None | 0 0
  1. from PyQt5 import QtWidgets
  2. import sys
  3.  
  4.  
  5. class Button(QtWidgets.QPushButton):
  6.     def __init__(self, text, parent, x, y, w, h, bgcolor, func):
  7.         super().__init__(text, parent)
  8.  
  9.         self.move(x, y)
  10.         self.resize(w, h)
  11.         self.setStyleSheet("background-color: #%s; color: #FDFEFE;  font-size: 20pt;" % bgcolor)
  12.         self.clicked.connect(func)
  13.  
  14.  
  15. class Widget(QtWidgets.QWidget):
  16.     def __init__(self):
  17.         super().__init__()
  18.  
  19.         self.setMaximumHeight(500)
  20.         self.setMinimumHeight(500)
  21.         self.setMaximumWidth(349)
  22.         self.setMinimumWidth(349)
  23.         self.setWindowTitle('الحاسبة')
  24.  
  25.         self.lineEdit = QtWidgets.QLineEdit('0', self)
  26.         self.lineEdit.setReadOnly(True)
  27.         self.lineEdit.move(4, 200)
  28.         self.lineEdit.resize(257, 59)
  29.  
  30.         self.addButtons()
  31.  
  32.         self.actions = []
  33.  
  34.     def addButtons(self):
  35.         self.b0 = Button('0', self, 90, 440, 85, 59, "2C3E50", self.zeroClicked)
  36.         self.b1 = Button('1', self, 4, 380, 85, 59, "148F77", self.oneClicked)
  37.         self.b2 = Button('2', self, 90, 380, 85, 59, "239B56", self.twoClicked)
  38.         self.b3 = Button('3', self, 176, 380, 85, 59, "1F618D", self.threeClicked)
  39.         self.b4 = Button('4', self, 4, 320, 85, 59, "17A589", self.fourClicked)
  40.         self.b5 = Button('5', self, 90, 320, 85, 59, "28B463", self.fiveClicked)
  41.         self.b6 = Button('6', self, 176, 320, 85, 59, "2471A3", self.sixClicked)
  42.         self.b7 = Button('7', self, 4, 260, 85, 59, "1ABC9C", self.sevenClicked)
  43.         self.b8 = Button('8', self, 90, 260, 85, 59, "28B463", self.eightClicked)
  44.         self.b9 = Button('9', self, 176, 260, 85, 59, "2980B9", self.nineClicked)
  45.         self.dot = Button(".", self, 4, 440, 85, 59, "2C3E50", self.dotClicked)
  46.         self.re = Button('±', self, 176, 440, 85, 59, "2C3E50", self.MIClicked)
  47.         self.plus = Button('+', self, 262, 380, 85, 59, "EC7063", self.plusClicked)
  48.         self.minuse = Button('-', self, 262, 320, 85, 59, "EC7063", self.minusClicked)
  49.         self.multi = Button('×', self, 262, 260, 85, 59, "EC7063", self.multiplyClicked)
  50.         self.division = Button('÷', self, 262, 200, 85, 59, "EC7063", self.divideClicked)
  51.         self.equl = Button('=' , self, 262, 440, 85, 59, "EC7063", self.calcAns)
  52.         self.bC = Button('C', self, 262, 140, 85, 59, "EC7063", self.Clear)
  53.  
  54.     def zeroClicked(self):
  55.         if self.lineEdit.text() == "0":
  56.             return
  57.  
  58.         self.lineEdit.setText(self.lineEdit.text() + "0")
  59.  
  60.     def oneClicked(self):
  61.         if self.lineEdit.text() == "0":
  62.             self.lineEdit.setText("1")
  63.  
  64.         else:
  65.             self.lineEdit.setText(self.lineEdit.text() + "1")
  66.  
  67.     def twoClicked(self):
  68.         if self.lineEdit.text() == "0":
  69.             self.lineEdit.setText("2")
  70.  
  71.         else:
  72.             self.lineEdit.setText(self.lineEdit.text() + "2")
  73.  
  74.     def threeClicked(self):
  75.         if self.lineEdit.text() == "0":
  76.             self.lineEdit.setText("3")
  77.  
  78.         else:
  79.             self.lineEdit.setText(self.lineEdit.text() + "3")
  80.  
  81.     def fourClicked(self):
  82.         if self.lineEdit.text() == "0":
  83.             self.lineEdit.setText("4")
  84.  
  85.         else:
  86.             self.lineEdit.setText(self.lineEdit.text() + "4")
  87.  
  88.     def fiveClicked(self):
  89.         if self.lineEdit.text() == "0":
  90.             self.lineEdit.setText("5")
  91.  
  92.         else:
  93.             self.lineEdit.setText(self.lineEdit.text() + "5")
  94.  
  95.     def sixClicked(self):
  96.         if self.lineEdit.text() == "0":
  97.             self.lineEdit.setText("6")
  98.  
  99.         else:
  100.             self.lineEdit.setText(self.lineEdit.text() + "6")
  101.  
  102.     def sevenClicked(self):
  103.         if self.lineEdit.text() == "0":
  104.             self.lineEdit.setText("7")
  105.  
  106.         else:
  107.             self.lineEdit.setText(self.lineEdit.text() + "7")
  108.  
  109.     def eightClicked(self):
  110.         if self.lineEdit.text() == "0":
  111.             self.lineEdit.setText("8")
  112.  
  113.         else:
  114.             self.lineEdit.setText(self.lineEdit.text() + "8")
  115.  
  116.     def nineClicked(self):
  117.         if self.lineEdit.text() == "0":
  118.             self.lineEdit.setText("9")
  119.  
  120.         else:
  121.             self.lineEdit.setText(self.lineEdit.text() + "9")
  122.  
  123.     def dotClicked(self):
  124.         if "." in self.lineEdit.text():
  125.             return
  126.  
  127.         self.lineEdit.setText(self.lineEdit.text() + ".")
  128.  
  129.     def MIClicked(self):
  130.         text = self.lineEdit.text()
  131.         if not text or text == "0":
  132.             return
  133.  
  134.         if text[0] == "-":
  135.             self.lineEdit.setText(text[1:])
  136.  
  137.         else:
  138.             self.lineEdit.setText("-" + text)
  139.  
  140.     def plusClicked(self):
  141.         text = self.lineEdit.text()
  142.         if text[-1] == ".":
  143.             text = text[:-1]
  144.  
  145.         if not text:
  146.             self.actions[-1] = "+"
  147.             return
  148.  
  149.         elif text == "0":
  150.             return
  151.  
  152.         if "." in text:
  153.             self.actions.append(float(text))
  154.  
  155.         else:
  156.             self.actions.append(int(text))
  157.  
  158.         self.actions.append("+")
  159.         self.lineEdit.clear()
  160.  
  161.     def minusClicked(self):
  162.         text = self.lineEdit.text()
  163.         if text[-1] == ".":
  164.             text = text[:-1]
  165.  
  166.         if not text:
  167.             self.actions[-1] = "-"
  168.             return
  169.  
  170.         elif text == "0":
  171.             return
  172.  
  173.         if "." in text:
  174.             self.actions.append(float(text))
  175.  
  176.         else:
  177.             self.actions.append(int(text))
  178.  
  179.         self.actions.append("-")
  180.         self.lineEdit.clear()
  181.  
  182.     def multiplyClicked(self):
  183.         text = self.lineEdit.text()
  184.         if text[-1] == ".":
  185.             text = text[:-1]
  186.  
  187.         if not text:
  188.             self.actions[-1] = "×"
  189.             return
  190.  
  191.         if "." in text:
  192.             self.actions.append(float(text))
  193.  
  194.         else:
  195.             self.actions.append(int(text))
  196.  
  197.         self.actions.append("×")
  198.         self.lineEdit.clear()
  199.  
  200.     def divideClicked(self):
  201.         text = self.lineEdit.text()
  202.         if text[-1] == ".":
  203.             text = text[:-1]
  204.  
  205.         if not text:
  206.             self.actions[-1] = "÷"
  207.             return
  208.  
  209.         if "." in text:
  210.             self.actions.append(float(text))
  211.  
  212.         else:
  213.             self.actions.append(int(text))
  214.  
  215.         self.actions.append("÷")
  216.         self.lineEdit.clear()
  217.  
  218.     def calcAns(self):
  219.         if not self.actions:
  220.             return
  221.  
  222.         text = self.lineEdit.text()
  223.         if text:
  224.             if "." in text:
  225.                 self.actions.append(float(text))
  226.  
  227.             else:
  228.                 self.actions.append(int(text))
  229.  
  230.         i = 1
  231.         ans = self.actions[0]
  232.         while i < len(self.actions):
  233.             action = self.actions[i]
  234.             if len(self.actions) < i + 2:
  235.                 break
  236.  
  237.             if action == "+":
  238.                 ans += self.actions[i + 1]
  239.  
  240.             elif action == "-":
  241.                 ans -= self.actions[i + 1]
  242.  
  243.             elif action == "×":
  244.                 ans *= self.actions[i + 1]
  245.  
  246.             else:
  247.                 ans /= self.actions[i + 1]
  248.  
  249.             i += 2
  250.  
  251.         self.actions = []
  252.         self.lineEdit.setText(str(ans))
  253.  
  254.     def Clear(self):
  255.         self.actions = []
  256.         self.lineEdit.setText("0")
  257.  
  258.  
  259. def main():
  260.     app = QtWidgets.QApplication(sys.argv)
  261.     window = Widget()
  262.     window.show()
  263.     sys.exit(app.exec_())
  264.  
  265.  
  266. if __name__ == '__main__':
  267.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement