Simple GUI calculator using PyQt5 in Python
GUI:
The user is provided with information using manipulable visual widgets that don't require command-line input. These interface components respond to the user's interactions per the pre-programmed script, assisting each user's action. Since many GUIs represent text and graphical elements in standard formats, it is possible for programs using the same GUI software to share data. As it is patched and developed, the same software application or os version may present distinct or marginally distinct GUIs. The appearance of an application may change depending on user needs or to enhance the user experience, even if the application's core functionality and functions remain unchanged, as was the case with the various Windows versions over time.
A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast to text-based interfaces, which only display data and commands as text, GUIs visualize information and related user controls. A cursor, trackball, stylus, or thumb on a touch screen are all pointing devices that can be used to communicate with GUI representations. The first text interface between a human and a computer operated using keyboard input and a prompt. Commands were entered at the DOS prompt to ask for responses from a desktop.
PyQt5:
PyQt5 is one of several solutions that Python offers for creating GUI applications. Cross-platform GUI toolkit PyQt5 is a collection of Python interfaces for Qt version 5. With the capabilities and convenience of use that this library offers, one can create an interactive computer program very quickly. Front-end and Back-end make up a GUI application. In order to speed up development & allow more time to be spent on back-end tasks, PyQt5 has developed a tool called "QtDesigner" that allows one to drag and drop elements to create the front-end. Riverbank Computing is the company in charge of developing and maintaining PyQt.
CODE:
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Python ")
self.setGeometry(100, 100, 360, 350)
self.UiComponents()
self.show()
def UiComponents(self):
self.label = QLabel(self)
self.label.setGeometry(5, 5, 350, 70)
self.label.setWordWrap(True)
self.label.setStyleSheet("QLabel"
"{"
"border : 4px solid black;"
"background : white;"
"}")
self.label.setAlignment(Qt.AlignRight)
self.label.setFont(QFont('Arial', 15))
push1 = QPushButton("1", self)
push1.setGeometry(5, 150, 80, 40)
push2 = QPushButton("2", self)
push2.setGeometry(95, 150, 80, 40)
push3 = QPushButton("3", self)
push3.setGeometry(185, 150, 80, 40)
push4 = QPushButton("4", self)
push4.setGeometry(5, 200, 80, 40)
push5 = QPushButton("5", self)
push5.setGeometry(95, 200, 80, 40)
push6 = QPushButton("5", self)
push6.setGeometry(185, 200, 80, 40)
push7 = QPushButton("7", self)
push7.setGeometry(5, 250, 80, 40)
push8 = QPushButton("8", self)
push8.setGeometry(95, 250, 80, 40)
push9 = QPushButton("9", self)
push9.setGeometry(185, 250, 80, 40)
push0 = QPushButton("0", self)
push0.setGeometry(5, 300, 80, 40)
push_equal = QPushButton("=", self)
push_equal.setGeometry(275, 300, 80, 40)
c_effect = QGraphicsColorizeEffect()
c_effect.setColor(Qt.blue)
push_equal.setGraphicsEffect(c_effect)
push_plus = QPushButton("+", self)
push_plus.setGeometry(275, 250, 80, 40)
push_minus = QPushButton("-", self)
push_minus.setGeometry(275, 200, 80, 40)
push_mul = QPushButton("*", self)
push_mul.setGeometry(275, 150, 80, 40)
push_div = QPushButton("/", self)
push_div.setGeometry(185, 300, 80, 40)
push_point = QPushButton(".", self)
push_point.setGeometry(95, 300, 80, 40)
push_clear = QPushButton("Clear", self)
push_clear.setGeometry(5, 100, 200, 40)
push_del = QPushButton("Del", self)
push_del.setGeometry(210, 100, 145, 40)
push_minus.clicked.connect(self.action_minus)
push_equal.clicked.connect(self.action_equal)
push0.clicked.connect(self.action0)
push1.clicked.connect(self.action1)
push2.clicked.connect(self.action2)
push3.clicked.connect(self.action3)
push4.clicked.connect(self.action4)
push5.clicked.connect(self.action5)
push6.clicked.connect(self.action6)
push7.clicked.connect(self.action7)
push8.clicked.connect(self.action8)
push9.clicked.connect(self.action9)
push_div.clicked.connect(self.action_div)
push_mul.clicked.connect(self.action_mul)
push_plus.clicked.connect(self.action_plus)
push_point.clicked.connect(self.action_point)
push_clear.clicked.connect(self.action_clear)
push_del.clicked.connect(self.action_del)
def action_equal(self):
equation = self.label.text()
try:
ans = eval(equation)
self.label.setText(str(ans))
except:
self.label.setText("Wrong Input")
def action_plus(self):
text = self.label.text()
self.label.setText(text + " + ")
def action_minus(self):
text = self.label.text()
self.label.setText(text + " - ")
def action_div(self):
text = self.label.text()
self.label.setText(text + " / ")
def action_mul(self):
text = self.label.text()
self.label.setText(text + " * ")
def action_point(self):
text = self.label.text()
self.label.setText(text + ".")
def action0(self):
text = self.label.text()
self.label.setText(text + "0")
def action1(self):
text = self.label.text()
self.label.setText(text + "1")
def action2(self):
text = self.label.text()
self.label.setText(text + "2")
def action3(self):
text = self.label.text()
self.label.setText(text + "3")
def action4(self):
text = self.label.text()
self.label.setText(text + "4")
def action5(self):
text = self.label.text()
self.label.setText(text + "5")
def action6(self):
text = self.label.text()
self.label.setText(text + "6")
def action7(self):
text = self.label.text()
self.label.setText(text + "7")
def action8(self):
text = self.label.text()
self.label.setText(text + "8")
def action9(self):
text = self.label.text()
self.label.setText(text + "9")
def action_clear(self):
self.label.setText("")
def action_del(self):
text = self.label.text()
print(text[:len(text)-1])
self.label.setText(text[:len(text)-1])
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
CODE EXPLANATION:
Import the libraries PyQt5 to widgets, QtCore, QtGui and sys. We create the calculator using window title geometry and Uicomponents to get a significant output. We set the label using Uicomponents for stylesheet, wordwrap and geometry. Before the implementation we create an instances of mathematical operations like addition, subtraction, multiplication, division, percentage calculations and some more in which simple calculator is included with. Then merging all the calculation scanned programs to the PyQt5 modules by using some stack operations push pop to make the code easy with some functional activities to build a simple calculator.
OUTPUT:
