mifomen

Untitled

Jan 13th, 2021
665
356 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2.  
  3. # Form implementation generated from reading ui file 'mainwindow.ui'
  4. #
  5. # Created by: PyQt5 UI code generator 5.11.3
  6. #
  7. # WARNING! All changes made in this file will be lost!
  8.  
  9. from PyQt5 import QtCore, QtGui, QtWidgets
  10.  
  11. class Ui_MainWindow(object):
  12.     def setupUi(self, MainWindow):
  13.         MainWindow.setObjectName("MainWindow")
  14.         MainWindow.resize(338, 429)
  15.         self.centralWidget = QtWidgets.QWidget(MainWindow)
  16.         sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Maximum)
  17.         sizePolicy.setHorizontalStretch(0)
  18.         sizePolicy.setVerticalStretch(0)
  19.         sizePolicy.setHeightForWidth(self.centralWidget.sizePolicy().hasHeightForWidth())
  20.         self.centralWidget.setSizePolicy(sizePolicy)
  21.         self.centralWidget.setObjectName("centralWidget")
  22.         self.verticalLayout = QtWidgets.QVBoxLayout(self.centralWidget)
  23.         self.verticalLayout.setContentsMargins(11, 11, 11, 11)
  24.         self.verticalLayout.setSpacing(6)
  25.         self.verticalLayout.setObjectName("verticalLayout")
  26.         self.lcdNumber = QtWidgets.QLCDNumber(self.centralWidget)
  27.         font = QtGui.QFont()
  28.         font.setFamily("Arial")
  29.         self.lcdNumber.setFont(font)
  30.         self.lcdNumber.setDigitCount(10)
  31.         self.lcdNumber.setObjectName("lcdNumber")
  32.         self.verticalLayout.addWidget(self.lcdNumber)
  33.         self.gridLayout = QtWidgets.QGridLayout()
  34.         self.gridLayout.setSpacing(6)
  35.         self.gridLayout.setObjectName("gridLayout")
  36.         self.pushButton_n4 = QtWidgets.QPushButton(self.centralWidget)
  37.         self.pushButton_n4.setMinimumSize(QtCore.QSize(0, 50))
  38.         font = QtGui.QFont()
  39.         font.setPointSize(27)
  40.         font.setBold(False)
  41.         font.setWeight(50)
  42.         self.pushButton_n4.setFont(font)
  43.         self.pushButton_n4.setStyleSheet("QPushButton {\n"
  44. "color: #01000;\n"
  45. "}")
  46.         self.pushButton_n4.setObjectName("pushButton_n4")
  47.         self.gridLayout.addWidget(self.pushButton_n4, 3, 0, 1, 1)
  48.         self.pushButton_n1 = QtWidgets.QPushButton(self.centralWidget)
  49.         self.pushButton_n1.setMinimumSize(QtCore.QSize(0, 50))
  50.         font = QtGui.QFont()
  51.         font.setPointSize(27)
  52.         font.setBold(False)
  53.         font.setWeight(50)
  54.         self.pushButton_n1.setFont(font)
  55.         self.pushButton_n1.setStyleSheet("QPushButton {\n"
  56. "color: #01000;\n"
  57. "}")
  58.         self.pushButton_n1.setObjectName("pushButton_n1")
  59.         self.gridLayout.addWidget(self.pushButton_n1, 4, 0, 1, 1)
  60.         self.pushButton_n8 = QtWidgets.QPushButton(self.centralWidget)
  61.         self.pushButton_n8.setMinimumSize(QtCore.QSize(0, 50))
  62.         font = QtGui.QFont()
  63.         font.setPointSize(27)
  64.         font.setBold(False)
  65.         font.setWeight(50)
  66.         self.pushButton_n8.setFont(font)
  67.         self.pushButton_n8.setStyleSheet("QPushButton {\n"
  68. "color: #01000;\n"
  69. "}")
  70.         self.pushButton_n8.setObjectName("pushButton_n8")
  71.         self.gridLayout.addWidget(self.pushButton_n8, 2, 1, 1, 1)
  72.         self.pushButton_mul = QtWidgets.QPushButton(self.centralWidget)
  73.         self.pushButton_mul.setMinimumSize(QtCore.QSize(0, 50))
  74.         font = QtGui.QFont()
  75.         font.setPointSize(27)
  76.         font.setBold(False)
  77.         font.setWeight(50)
  78.         self.pushButton_mul.setFont(font)
  79.         self.pushButton_mul.setObjectName("pushButton_mul")
  80.         self.gridLayout.addWidget(self.pushButton_mul, 2, 3, 1, 1)
  81.         self.pushButton_n7 = QtWidgets.QPushButton(self.centralWidget)
  82.         self.pushButton_n7.setMinimumSize(QtCore.QSize(0, 50))
  83.         font = QtGui.QFont()
  84.         font.setPointSize(27)
  85.         font.setBold(False)
  86.         font.setWeight(50)
  87.         self.pushButton_n7.setFont(font)
  88.         self.pushButton_n7.setStyleSheet("QPushButton {\n"
  89. "color: #01000;\n"
  90. "}")
  91.         self.pushButton_n7.setObjectName("pushButton_n7")
  92.         self.gridLayout.addWidget(self.pushButton_n7, 2, 0, 1, 1)
  93.         self.pushButton_n6 = QtWidgets.QPushButton(self.centralWidget)
  94.         self.pushButton_n6.setMinimumSize(QtCore.QSize(0, 50))
  95.         font = QtGui.QFont()
  96.         font.setPointSize(27)
  97.         font.setBold(False)
  98.         font.setWeight(50)
  99.         self.pushButton_n6.setFont(font)
  100.         self.pushButton_n6.setStyleSheet("QPushButton {\n"
  101. "color: #01000;\n"
  102. "}")
  103.         self.pushButton_n6.setObjectName("pushButton_n6")
  104.         self.gridLayout.addWidget(self.pushButton_n6, 3, 2, 1, 1)
  105.         self.pushButton_n5 = QtWidgets.QPushButton(self.centralWidget)
  106.         self.pushButton_n5.setMinimumSize(QtCore.QSize(0, 50))
  107.         font = QtGui.QFont()
  108.         font.setPointSize(27)
  109.         font.setBold(False)
  110.         font.setWeight(50)
  111.         self.pushButton_n5.setFont(font)
  112.         self.pushButton_n5.setStyleSheet("QPushButton {\n"
  113. "color: #01000;\n"
  114. "}")
  115.         self.pushButton_n5.setObjectName("pushButton_n5")
  116.         self.gridLayout.addWidget(self.pushButton_n5, 3, 1, 1, 1)
  117.         self.pushButton_n0 = QtWidgets.QPushButton(self.centralWidget)
  118.         self.pushButton_n0.setMinimumSize(QtCore.QSize(0, 50))
  119.         font = QtGui.QFont()
  120.         font.setPointSize(27)
  121.         font.setBold(False)
  122.         font.setWeight(50)
  123.         self.pushButton_n0.setFont(font)
  124.         self.pushButton_n0.setStyleSheet("QPushButton {\n"
  125. "color: #01000;\n"
  126. "}")
  127.         self.pushButton_n0.setObjectName("pushButton_n0")
  128.         self.gridLayout.addWidget(self.pushButton_n0, 5, 0, 1, 1)
  129.         self.pushButton_n2 = QtWidgets.QPushButton(self.centralWidget)
  130.         self.pushButton_n2.setMinimumSize(QtCore.QSize(0, 50))
  131.         font = QtGui.QFont()
  132.         font.setPointSize(27)
  133.         font.setBold(False)
  134.         font.setWeight(50)
  135.         self.pushButton_n2.setFont(font)
  136.         self.pushButton_n2.setStyleSheet("QPushButton {\n"
  137. "color: #01000;\n"
  138. "}")
  139.         self.pushButton_n2.setObjectName("pushButton_n2")
  140.         self.gridLayout.addWidget(self.pushButton_n2, 4, 1, 1, 1)
  141.         self.pushButton_n9 = QtWidgets.QPushButton(self.centralWidget)
  142.         self.pushButton_n9.setMinimumSize(QtCore.QSize(0, 50))
  143.         font = QtGui.QFont()
  144.         font.setPointSize(27)
  145.         font.setBold(False)
  146.         font.setWeight(50)
  147.         self.pushButton_n9.setFont(font)
  148.         self.pushButton_n9.setStyleSheet("QPushButton {\n"
  149. "color: #01000;\n"
  150. "}")
  151.         self.pushButton_n9.setObjectName("pushButton_n9")
  152.         self.gridLayout.addWidget(self.pushButton_n9, 2, 2, 1, 1)
  153.         self.pushButton_n3 = QtWidgets.QPushButton(self.centralWidget)
  154.         self.pushButton_n3.setMinimumSize(QtCore.QSize(0, 50))
  155.         font = QtGui.QFont()
  156.         font.setPointSize(27)
  157.         font.setBold(False)
  158.         font.setWeight(50)
  159.         self.pushButton_n3.setFont(font)
  160.         self.pushButton_n3.setStyleSheet("QPushButton {\n"
  161. "color: #01000;\n"
  162. "}")
  163.         self.pushButton_n3.setObjectName("pushButton_n3")
  164.         self.gridLayout.addWidget(self.pushButton_n3, 4, 2, 1, 1)
  165.         self.pushButton_div = QtWidgets.QPushButton(self.centralWidget)
  166.         self.pushButton_div.setMinimumSize(QtCore.QSize(0, 50))
  167.         font = QtGui.QFont()
  168.         font.setPointSize(27)
  169.         font.setBold(False)
  170.         font.setWeight(50)
  171.         self.pushButton_div.setFont(font)
  172.         self.pushButton_div.setObjectName("pushButton_div")
  173.         self.gridLayout.addWidget(self.pushButton_div, 1, 3, 1, 1)
  174.         self.pushButton_sub = QtWidgets.QPushButton(self.centralWidget)
  175.         self.pushButton_sub.setMinimumSize(QtCore.QSize(0, 50))
  176.         font = QtGui.QFont()
  177.         font.setPointSize(27)
  178.         font.setBold(False)
  179.         font.setWeight(50)
  180.         self.pushButton_sub.setFont(font)
  181.         self.pushButton_sub.setObjectName("pushButton_sub")
  182.         self.gridLayout.addWidget(self.pushButton_sub, 3, 3, 1, 1)
  183.         self.pushButton_add = QtWidgets.QPushButton(self.centralWidget)
  184.         self.pushButton_add.setMinimumSize(QtCore.QSize(0, 50))
  185.         font = QtGui.QFont()
  186.         font.setPointSize(27)
  187.         font.setBold(False)
  188.         font.setWeight(50)
  189.         self.pushButton_add.setFont(font)
  190.         self.pushButton_add.setObjectName("pushButton_add")
  191.         self.gridLayout.addWidget(self.pushButton_add, 4, 3, 1, 1)
  192.         self.pushButton_ac = QtWidgets.QPushButton(self.centralWidget)
  193.         self.pushButton_ac.setMinimumSize(QtCore.QSize(0, 50))
  194.         font = QtGui.QFont()
  195.         font.setFamily("Arial")
  196.         font.setPointSize(27)
  197.         font.setBold(False)
  198.         font.setWeight(50)
  199.         self.pushButton_ac.setFont(font)
  200.         self.pushButton_ac.setStyleSheet("QPushButton {\n"
  201. "    color: #01000;\n"
  202. "}")
  203.         self.pushButton_ac.setObjectName("pushButton_ac")
  204.         self.gridLayout.addWidget(self.pushButton_ac, 1, 0, 1, 1)
  205.         self.pushButton_mr = QtWidgets.QPushButton(self.centralWidget)
  206.         self.pushButton_mr.setMinimumSize(QtCore.QSize(0, 50))
  207.         font = QtGui.QFont()
  208.         font.setPointSize(27)
  209.         font.setBold(False)
  210.         font.setWeight(50)
  211.         self.pushButton_mr.setFont(font)
  212.         self.pushButton_mr.setStyleSheet("QPushButton {\n"
  213. "   color: #01000;\n"
  214. "}")
  215.         self.pushButton_mr.setObjectName("pushButton_mr")
  216.         self.gridLayout.addWidget(self.pushButton_mr, 1, 2, 1, 1)
  217.         self.pushButton_m = QtWidgets.QPushButton(self.centralWidget)
  218.         self.pushButton_m.setMinimumSize(QtCore.QSize(0, 50))
  219.         font = QtGui.QFont()
  220.         font.setPointSize(27)
  221.         font.setBold(False)
  222.         font.setWeight(50)
  223.         self.pushButton_m.setFont(font)
  224.         self.pushButton_m.setStyleSheet("QPushButton {\n"
  225. "   color: #01000;\n"
  226. "}")
  227.         self.pushButton_m.setObjectName("pushButton_m")
  228.         self.gridLayout.addWidget(self.pushButton_m, 1, 1, 1, 1)
  229.         self.pushButton_pc = QtWidgets.QPushButton(self.centralWidget)
  230.         self.pushButton_pc.setMinimumSize(QtCore.QSize(0, 50))
  231.         font = QtGui.QFont()
  232.         font.setPointSize(27)
  233.         font.setBold(False)
  234.         font.setWeight(50)
  235.         self.pushButton_pc.setFont(font)
  236.         self.pushButton_pc.setObjectName("pushButton_pc")
  237.         self.gridLayout.addWidget(self.pushButton_pc, 5, 1, 1, 1)
  238.         self.pushButton_eq = QtWidgets.QPushButton(self.centralWidget)
  239.         self.pushButton_eq.setMinimumSize(QtCore.QSize(0, 50))
  240.         font = QtGui.QFont()
  241.         font.setPointSize(27)
  242.         font.setBold(False)
  243.         font.setWeight(50)
  244.         self.pushButton_eq.setFont(font)
  245.         self.pushButton_eq.setStyleSheet("QPushButton {\n"
  246. "color: #01000;\n"
  247. "}")
  248.         self.pushButton_eq.setObjectName("pushButton_eq")
  249.         self.gridLayout.addWidget(self.pushButton_eq, 5, 2, 1, 2)
  250.         self.verticalLayout.addLayout(self.gridLayout)
  251.         MainWindow.setCentralWidget(self.centralWidget)
  252.         self.statusBar = QtWidgets.QStatusBar(MainWindow)
  253.         self.statusBar.setObjectName("statusBar")
  254.         MainWindow.setStatusBar(self.statusBar)
  255.         self.menuBar = QtWidgets.QMenuBar(MainWindow)
  256.         self.menuBar.setGeometry(QtCore.QRect(0, 0, 338, 21))
  257.         self.menuBar.setObjectName("menuBar")
  258.         self.menuFIle = QtWidgets.QMenu(self.menuBar)
  259.         self.menuFIle.setObjectName("menuFIle")
  260.         MainWindow.setMenuBar(self.menuBar)
  261.         self.actionExit = QtWidgets.QAction(MainWindow)
  262.         self.actionExit.setObjectName("actionExit")
  263.         self.actionReset = QtWidgets.QAction(MainWindow)
  264.         self.actionReset.setObjectName("actionReset")
  265.         self.actionAbout_Calculator = QtWidgets.QAction(MainWindow)
  266.         self.actionAbout_Calculator.setObjectName("actionAbout_Calculator")
  267.         self.menuFIle.addAction(self.actionReset)
  268.         self.menuFIle.addAction(self.actionExit)
  269.         self.menuBar.addAction(self.menuFIle.menuAction())
  270.  
  271.         self.retranslateUi(MainWindow)
  272.         QtCore.QMetaObject.connectSlotsByName(MainWindow)
  273.  
  274.     def retranslateUi(self, MainWindow):
  275.         _translate = QtCore.QCoreApplication.translate
  276.         MainWindow.setWindowTitle(_translate("MainWindow", "Calculator"))
  277.         self.pushButton_n4.setText(_translate("MainWindow", "4"))
  278.         self.pushButton_n4.setShortcut(_translate("MainWindow", "4"))
  279.         self.pushButton_n1.setText(_translate("MainWindow", "1"))
  280.         self.pushButton_n1.setShortcut(_translate("MainWindow", "1"))
  281.         self.pushButton_n8.setText(_translate("MainWindow", "8"))
  282.         self.pushButton_n8.setShortcut(_translate("MainWindow", "8"))
  283.         self.pushButton_mul.setText(_translate("MainWindow", "x"))
  284.         self.pushButton_mul.setShortcut(_translate("MainWindow", "*"))
  285.         self.pushButton_n7.setText(_translate("MainWindow", "7"))
  286.         self.pushButton_n7.setShortcut(_translate("MainWindow", "7"))
  287.         self.pushButton_n6.setText(_translate("MainWindow", "6"))
  288.         self.pushButton_n6.setShortcut(_translate("MainWindow", "6"))
  289.         self.pushButton_n5.setText(_translate("MainWindow", "5"))
  290.         self.pushButton_n5.setShortcut(_translate("MainWindow", "5"))
  291.         self.pushButton_n0.setText(_translate("MainWindow", "0"))
  292.         self.pushButton_n0.setShortcut(_translate("MainWindow", "0"))
  293.         self.pushButton_n2.setText(_translate("MainWindow", "2"))
  294.         self.pushButton_n2.setShortcut(_translate("MainWindow", "2"))
  295.         self.pushButton_n9.setText(_translate("MainWindow", "9"))
  296.         self.pushButton_n9.setShortcut(_translate("MainWindow", "9"))
  297.         self.pushButton_n3.setText(_translate("MainWindow", "3"))
  298.         self.pushButton_n3.setShortcut(_translate("MainWindow", "3"))
  299.         self.pushButton_div.setText(_translate("MainWindow", "÷"))
  300.         self.pushButton_div.setShortcut(_translate("MainWindow", "/"))
  301.         self.pushButton_sub.setText(_translate("MainWindow", "-"))
  302.         self.pushButton_sub.setShortcut(_translate("MainWindow", "-"))
  303.         self.pushButton_add.setText(_translate("MainWindow", "+"))
  304.         self.pushButton_add.setShortcut(_translate("MainWindow", "+"))
  305.         self.pushButton_ac.setText(_translate("MainWindow", "AC"))
  306.         self.pushButton_ac.setShortcut(_translate("MainWindow", "Esc"))
  307.         self.pushButton_mr.setText(_translate("MainWindow", "MR"))
  308.         self.pushButton_mr.setShortcut(_translate("MainWindow", "R"))
  309.         self.pushButton_m.setText(_translate("MainWindow", "M"))
  310.         self.pushButton_m.setShortcut(_translate("MainWindow", "M"))
  311.         self.pushButton_pc.setText(_translate("MainWindow", "%"))
  312.         self.pushButton_pc.setShortcut(_translate("MainWindow", "%"))
  313.         self.pushButton_eq.setText(_translate("MainWindow", "="))
  314.         self.pushButton_eq.setShortcut(_translate("MainWindow", "Return"))
  315.         self.menuFIle.setTitle(_translate("MainWindow", "File"))
  316.         self.actionExit.setText(_translate("MainWindow", "Exit"))
  317.         self.actionExit.setShortcut(_translate("MainWindow", "Ctrl+Q"))
  318.         self.actionReset.setText(_translate("MainWindow", "Reset"))
  319.         self.actionReset.setShortcut(_translate("MainWindow", "Ctrl+R"))
  320.         self.actionAbout_Calculator.setText(_translate("MainWindow", "About Calculator"))
  321.  
  322.  
  323. from PyQt5.QtGui import *
  324. from PyQt5.QtWidgets import *
  325. from PyQt5.QtCore import *
  326.  
  327. import operator
  328.  
  329. from MainWindow import Ui_MainWindow
  330.  
  331. READY = 0
  332. INPUT = 1
  333.  
  334.  
  335. class MainWindow(QMainWindow, Ui_MainWindow):
  336.     def __init__(self, *args, **kwargs):
  337.         super(MainWindow, self).__init__(*args, **kwargs)
  338.         self.setupUi(self)
  339.  
  340.         # Setup numbers.
  341.         for n in range(0, 10):
  342.             getattr(self, 'pushButton_n%s' % n).pressed.connect(lambda v=n: self.input_number(v))
  343.  
  344.         # Setup operations.
  345.         self.pushButton_add.pressed.connect(lambda: self.operation(operator.add))
  346.         self.pushButton_sub.pressed.connect(lambda: self.operation(operator.sub))
  347.         self.pushButton_mul.pressed.connect(lambda: self.operation(operator.mul))
  348.         self.pushButton_div.pressed.connect(lambda: self.operation(operator.truediv))
  349.  
  350.         self.pushButton_pc.pressed.connect(self.operation_pc)
  351.         self.pushButton_eq.pressed.connect(self.equals)
  352.  
  353.         # Setup actions
  354.         self.actionReset.triggered.connect(self.reset)
  355.         self.pushButton_ac.pressed.connect(self.reset)
  356.  
  357.         self.pushButton_m.pressed.connect(self.memory_store)
  358.         self.pushButton_mr.pressed.connect(self.memory_recall)
  359.  
  360.         self.memory = 0
  361.         self.reset()
  362.  
  363.         self.show()
  364.  
  365.     def display(self):
  366.         self.lcdNumber.display(self.stack[-1])
  367.  
  368.     def reset(self):
  369.         self.state = READY
  370.         self.stack = [0]
  371.         self.last_operation = None
  372.         self.current_op = None
  373.         self.display()
  374.  
  375.     def memory_store(self):
  376.         self.memory = self.lcdNumber.value()
  377.  
  378.     def memory_recall(self):
  379.         self.state = INPUT
  380.         self.stack[-1] = self.memory
  381.         self.display()
  382.  
  383.     def input_number(self, v):
  384.         if self.state == READY:
  385.             self.state = INPUT
  386.             self.stack[-1] = v
  387.         else:
  388.             self.stack[-1] = self.stack[-1] * 10 + v
  389.  
  390.         self.display()
  391.  
  392.     def operation(self, op):
  393.         if self.current_op:  # Complete the current operation
  394.             self.equals()
  395.  
  396.         self.stack.append(0)
  397.         self.state = INPUT
  398.         self.current_op = op
  399.  
  400.     def operation_pc(self):
  401.         self.state = INPUT
  402.         self.stack[-1] *= 0.01
  403.         self.display()
  404.  
  405.     def equals(self):
  406.         # Support to allow '=' to repeat previous operation
  407.         # if no further input has been added.
  408.         if self.state == READY and self.last_operation:
  409.             s, self.current_op = self.last_operation
  410.             self.stack.append(s)
  411.  
  412.         if self.current_op:
  413.             self.last_operation = self.stack[-1], self.current_op
  414.  
  415.             self.stack = [self.current_op(*self.stack)]
  416.             self.current_op = None
  417.             self.state = READY
  418.             self.display()
  419.  
  420.  
  421. if __name__ == '__main__':
  422.     app = QApplication([])
  423.     app.setApplicationName("PyQt Calculator")
  424.  
  425.     window = MainWindow()
  426.     app.exec_()
  427.  
  428.  
  429.  
RAW Paste Data