Recently I was learning to code using PyQt5 and Python. I was doing a school project on Calculator. Here is my graphic class:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'calculator.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Calculator(object):
def setupUi(self, Calculator):
Calculator.setObjectName("Calculator")
Calculator.resize(240, 361)
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
Calculator.setFont(font)
Calculator.setLayoutDirection(QtCore.Qt.LeftToRight)
self.centralwidget = QtWidgets.QWidget(Calculator)
self.centralwidget.setObjectName("centralwidget")
self.Display = QtWidgets.QLabel(self.centralwidget)
self.Display.setGeometry(QtCore.QRect(0, 0, 241, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(16)
font.setBold(False)
font.setWeight(50)
self.Display.setFont(font)
self.Display.setLayoutDirection(QtCore.Qt.RightToLeft)
self.Display.setStyleSheet("QLabel {\n"
" qproperty-alignment: \'AlignVCenter | AlignRight\';\n"
" border: 1px solid gray;\n"
"}\n"
"\n"
"background-color : white;")
self.Display.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.Display.setObjectName("Display")
self.Clear = QtWidgets.QPushButton(self.centralwidget)
self.Clear.setGeometry(QtCore.QRect(0, 60, 61, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.Clear.setFont(font)
self.Clear.setStyleSheet("QPushButton {\n"
" background-color: rgb(215, 215, 215);\n"
" border: 1px solid gray;\n"
"}\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #BEBEBE, stop: 1 #D7D7D7);\n"
"}")
self.Clear.setObjectName("Clear")
self.btndiv = QtWidgets.QPushButton(self.centralwidget)
self.btndiv.setGeometry(QtCore.QRect(180, 60, 61, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.btndiv.setFont(font)
self.btndiv.setStyleSheet("QPushButton {\n"
" background-color: rgb(255, 151, 57);\n"
" color: white; \n"
" border: 1px solid gray;\n"
"}\n"
"\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #FF7832, stop: 1 #FF9739);\n"
"}")
self.btndiv.setObjectName("btndiv")
self.btnpercent = QtWidgets.QPushButton(self.centralwidget)
self.btnpercent.setGeometry(QtCore.QRect(120, 60, 61, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.btnpercent.setFont(font)
self.btnpercent.setStyleSheet("QPushButton {\n"
" background-color: rgb(215, 215, 215);\n"
" border: 1px solid gray;\n"
"}\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #BEBEBE, stop: 1 #D7D7D7);\n"
"}")
self.btnpercent.setObjectName("btnpercent")
self.btnneg = QtWidgets.QPushButton(self.centralwidget)
self.btnneg.setGeometry(QtCore.QRect(60, 60, 61, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.btnneg.setFont(font)
self.btnneg.setStyleSheet("QPushButton {\n"
" background-color: rgb(215, 215, 215);\n"
" border: 1px solid gray;\n"
"}\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #BEBEBE, stop: 1 #D7D7D7);\n"
"}")
self.btnneg.setObjectName("btnneg")
self.btn8 = QtWidgets.QPushButton(self.centralwidget)
self.btn8.setGeometry(QtCore.QRect(60, 120, 61, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.btn8.setFont(font)
self.btn8.setStyleSheet("QPushButton {\n"
" border: 1px solid gray;\n"
"}\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #dadbde, stop: 1 #f6f7fa);\n"
"}")
self.btn8.setObjectName("btn8")
self.btn7 = QtWidgets.QPushButton(self.centralwidget)
self.btn7.setGeometry(QtCore.QRect(0, 120, 61, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.btn7.setFont(font)
self.btn7.setStyleSheet("QPushButton {\n"
" border: 1px solid gray;\n"
"}\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #dadbde, stop: 1 #f6f7fa);\n"
"}")
self.btn7.setObjectName("btn7")
self.btnmul = QtWidgets.QPushButton(self.centralwidget)
self.btnmul.setGeometry(QtCore.QRect(180, 120, 61, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.btnmul.setFont(font)
self.btnmul.setStyleSheet("QPushButton {\n"
" background-color: rgb(255, 151, 57);\n"
" color: white; \n"
" border: 1px solid gray;\n"
"}\n"
"\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #FF7832, stop: 1 #FF9739);\n"
"}")
self.btnmul.setObjectName("btnmul")
self.btn9 = QtWidgets.QPushButton(self.centralwidget)
self.btn9.setGeometry(QtCore.QRect(120, 120, 61, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.btn9.setFont(font)
self.btn9.setStyleSheet("QPushButton {\n"
" border: 1px solid gray;\n"
"}\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #dadbde, stop: 1 #f6f7fa);\n"
"}")
self.btn9.setObjectName("btn9")
self.btn5 = QtWidgets.QPushButton(self.centralwidget)
self.btn5.setGeometry(QtCore.QRect(60, 180, 61, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.btn5.setFont(font)
self.btn5.setStyleSheet("QPushButton {\n"
" border: 1px solid gray;\n"
"}\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #dadbde, stop: 1 #f6f7fa);\n"
"}")
self.btn5.setObjectName("btn5")
self.btn4 = QtWidgets.QPushButton(self.centralwidget)
self.btn4.setGeometry(QtCore.QRect(0, 180, 61, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.btn4.setFont(font)
self.btn4.setStyleSheet("QPushButton {\n"
" border: 1px solid gray;\n"
"}\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #dadbde, stop: 1 #f6f7fa);\n"
"}")
self.btn4.setObjectName("btn4")
self.btnsub = QtWidgets.QPushButton(self.centralwidget)
self.btnsub.setGeometry(QtCore.QRect(180, 180, 61, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.btnsub.setFont(font)
self.btnsub.setStyleSheet("QPushButton {\n"
" background-color: rgb(255, 151, 57);\n"
" color: white; \n"
" border: 1px solid gray;\n"
"}\n"
"\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #FF7832, stop: 1 #FF9739);\n"
"}")
self.btnsub.setObjectName("btnsub")
self.btn6 = QtWidgets.QPushButton(self.centralwidget)
self.btn6.setGeometry(QtCore.QRect(120, 180, 61, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.btn6.setFont(font)
self.btn6.setStyleSheet("QPushButton {\n"
" border: 1px solid gray;\n"
"}\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #dadbde, stop: 1 #f6f7fa);\n"
"}")
self.btn6.setObjectName("btn6")
self.btn0 = QtWidgets.QPushButton(self.centralwidget)
self.btn0.setGeometry(QtCore.QRect(0, 300, 121, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.btn0.setFont(font)
self.btn0.setStyleSheet("QPushButton {\n"
" border: 1px solid gray;\n"
"}\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #dadbde, stop: 1 #f6f7fa);\n"
"}")
self.btn0.setObjectName("btn0")
self.btndecimal = QtWidgets.QPushButton(self.centralwidget)
self.btndecimal.setGeometry(QtCore.QRect(120, 300, 61, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.btndecimal.setFont(font)
self.btndecimal.setStyleSheet("QPushButton {\n"
" background-color: rgb(215, 215, 215);\n"
" border: 1px solid gray;\n"
"}\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #BEBEBE, stop: 1 #D7D7D7);\n"
"}")
self.btndecimal.setObjectName("btndecimal")
self.btnequal = QtWidgets.QPushButton(self.centralwidget)
self.btnequal.setGeometry(QtCore.QRect(180, 300, 61, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.btnequal.setFont(font)
self.btnequal.setStyleSheet("QPushButton {\n"
" background-color: rgb(255, 151, 57);\n"
" color: white; \n"
" border: 1px solid gray;\n"
"}\n"
"\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #FF7832, stop: 1 #FF9739);\n"
"}")
self.btnequal.setObjectName("btnequal")
self.btn3 = QtWidgets.QPushButton(self.centralwidget)
self.btn3.setGeometry(QtCore.QRect(120, 240, 61, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.btn3.setFont(font)
self.btn3.setStyleSheet("QPushButton {\n"
" border: 1px solid gray;\n"
"}\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #dadbde, stop: 1 #f6f7fa);\n"
"}")
self.btn3.setObjectName("btn3")
self.btn2 = QtWidgets.QPushButton(self.centralwidget)
self.btn2.setGeometry(QtCore.QRect(60, 240, 61, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.btn2.setFont(font)
self.btn2.setStyleSheet("QPushButton {\n"
" border: 1px solid gray;\n"
"}\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #dadbde, stop: 1 #f6f7fa);\n"
"}")
self.btn2.setObjectName("btn2")
self.btn1 = QtWidgets.QPushButton(self.centralwidget)
self.btn1.setGeometry(QtCore.QRect(0, 240, 61, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.btn1.setFont(font)
self.btn1.setStyleSheet("QPushButton {\n"
" border: 1px solid gray;\n"
"}\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #dadbde, stop: 1 #f6f7fa);\n"
"}")
self.btn1.setObjectName("btn1")
self.btnplus = QtWidgets.QPushButton(self.centralwidget)
self.btnplus.setGeometry(QtCore.QRect(180, 240, 61, 61))
font = QtGui.QFont()
font.setFamily("Lucida Console")
font.setPointSize(12)
self.btnplus.setFont(font)
self.btnplus.setStyleSheet("QPushButton {\n"
" background-color: rgb(255, 151, 57);\n"
" color: white; \n"
" border: 1px solid gray;\n"
"}\n"
"\n"
"QPushButton:pressed {\n"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
" stop: 0 #FF7832, stop: 1 #FF9739);\n"
"}")
self.setFixedSize(self.size())
self.btnplus.setObjectName("btnplus")
Calculator.setCentralWidget(self.centralwidget)
self.retranslateUi(Calculator)
QtCore.QMetaObject.connectSlotsByName(Calculator)
def retranslateUi(self, Calculator):
_translate = QtCore.QCoreApplication.translate
Calculator.setWindowTitle(_translate("Calculator", "Calculator"))
self.Display.setText(_translate("Calculator", "0"))
self.Clear.setText(_translate("Calculator", "C"))
self.btndiv.setText(_translate("Calculator", "/"))
self.btnpercent.setText(_translate("Calculator", "%"))
self.btnneg.setText(_translate("Calculator", "+/-"))
self.btn8.setText(_translate("Calculator", "8"))
self.btn7.setText(_translate("Calculator", "7"))
self.btnmul.setText(_translate("Calculator", "X"))
self.btn9.setText(_translate("Calculator", "9"))
self.btn5.setText(_translate("Calculator", "5"))
self.btn4.setText(_translate("Calculator", "4"))
self.btnsub.setText(_translate("Calculator", "-"))
self.btn6.setText(_translate("Calculator", "6"))
self.btn0.setText(_translate("Calculator", "0"))
self.btndecimal.setText(_translate("Calculator", "."))
self.btnequal.setText(_translate("Calculator", "="))
self.btn3.setText(_translate("Calculator", "3"))
self.btn2.setText(_translate("Calculator", "2"))
self.btn1.setText(_translate("Calculator", "1"))
self.btnplus.setText(_translate("Calculator", "+"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Calculator = QtWidgets.QMainWindow()
ui = Ui_Calculator()
ui.setupUi(Calculator)
Calculator.show()
sys.exit(app.exec_())
I added my operations on another file but the problem is coming when I am trying to do multiple operations. Also please help me make my code more shorter if possible. Here is the operations or the main.py file:
from PyQt5 import QtWidgets
from calculatorui import Ui_Calculator
class Calculator(QtWidgets.QMainWindow, Ui_Calculator):
first_num = None
second_num = 0
user_enter_second_num = False
def __init__(self):
super().__init__()
self.setupUi(self)
# for the digits
self.btn0.clicked.connect(self.digit_pressed)
self.btn1.clicked.connect(self.digit_pressed)
self.btn2.clicked.connect(self.digit_pressed)
self.btn3.clicked.connect(self.digit_pressed)
self.btn4.clicked.connect(self.digit_pressed)
self.btn5.clicked.connect(self.digit_pressed)
self.btn6.clicked.connect(self.digit_pressed)
self.btn7.clicked.connect(self.digit_pressed)
self.btn8.clicked.connect(self.digit_pressed)
self.btn9.clicked.connect(self.digit_pressed)
# For the decimal point
self.btndecimal.clicked.connect(self.decimal_pressed)
# For the unary operators
self.btnpercent.clicked.connect(self.unary_press)
self.btnneg.clicked.connect(self.unary_press)
# Make binary operators ready to get checked
self.btnplus.setCheckable(True)
self.btnsub.setCheckable(True)
self.btnmul.setCheckable(True)
self.btndiv.setCheckable(True)
# Binding the binary operators with their functions
self.btnplus.clicked.connect(self.binary_operations)
self.btnsub.clicked.connect(self.binary_operations)
self.btnmul.clicked.connect(self.binary_operations)
self.btndiv.clicked.connect(self.binary_operations)
# Binding the equal button
self.btnequal.clicked.connect(self.equal_operation)
self.show()
def digit_pressed(self):
label = self.sender()
if (self.btnplus.isChecked() or self.btnmul.isChecked() or self.btnsub.isChecked() or
self.btndiv.isChecked()) and (not self.user_enter_second_num):
new_label = format(float(label.text()), '.15g')
self.user_enter_second_num = True
else:
if '.' in self.Display.text() and (label.text() == "0"):
new_label = format(self.Display.text()+label.text(), '.15')
else:
new_label = format(float(self.Display.text() + label.text()), '.15g')
if new_label == "inf":
pass
else:
self.Display.setText(new_label)
def decimal_pressed(self):
label = self.Display.text()
if "." in label:
pass
else:
self.Display.setText(label + '.')
def unary_press(self):
label = self.sender()
new_label = float(self.Display.text())
if label.text() == "+/-":
new_label *= -1
else:
# Check for % sign
new_label *= 0.01
label = format(new_label, '.15g')
self.Display.setText(label)
def binary_operations(self):
button = self.sender()
self.first_num = float(self.Display.text())
button.setChecked(True)
self.user_enter_second_num = False
def equal_operation(self):
self.second_num = float(self.Display.text())
if self.btnplus.isChecked():
label_number = self.first_num + self.second_num
new_label = format(label_number, '.15g')
self.Display.setText(new_label)
self.btnplus.setChecked(False)
elif self.btnsub.isChecked():
label_number = self.first_num - self.second_num
new_label = format(label_number, '.15g')
self.Display.setText(new_label)
self.btnsub.setChecked(False)
elif self.btnmul.isChecked():
label_number = self.first_num * self.second_num
new_label = format(label_number, '.15g')
self.Display.setText(new_label)
self.btnmul.setChecked(False)
elif self.btndiv.isChecked():
label_number = self.first_num / self.second_num
new_label = format(label_number, '.15g')
self.Display.setText(new_label)
self.btndiv.setChecked(False)
else:
pass
self.user_enter_second_num = False
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
calc = Calculator()
sys.exit(app.exec_())
```