The full code can be found below as well as on GitHub.

import sys

from PyQt5.QtWidgets import (QApplication, QLabel, QMainWindow, QPushButton,
                             QWidget, QGridLayout)
from PyQt5.QtCore import Qt


# CONSTANTS
ORANGE_BUTTON_STYLE = "background-color: #e07000"
GRAY_BUTTON_STYLE = "background-color: lightGray; color: black;"
OPERATORS = ["+", "-", "/", "*", "."]


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("My Calculator")

        main_layout = QGridLayout()
        self.label = QLabel("0")
        self.label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
        main_layout.addWidget(self.label, 0, 0, 1, 4)
        self.buttons = {}

        self._setup_button("AC", main_layout, 1, 0, 1, 3, GRAY_BUTTON_STYLE)
        self._setup_button("/", main_layout, 1, 3, 1, 1, ORANGE_BUTTON_STYLE)

        self._setup_button("7", main_layout, 2, 0, 1, 1)
        self._setup_button("8", main_layout, 2, 1, 1, 1)
        self._setup_button("9", main_layout, 2, 2, 1, 1)
        self._setup_button("*", main_layout, 2, 3, 1, 1, ORANGE_BUTTON_STYLE)

        self._setup_button("4", main_layout, 3, 0, 1, 1)
        self._setup_button("5", main_layout, 3, 1, 1, 1)
        self._setup_button("6", main_layout, 3, 2, 1, 1)
        self._setup_button("-", main_layout, 3, 3, 1, 1, ORANGE_BUTTON_STYLE)

        self._setup_button("1", main_layout, 4, 0, 1, 1)
        self._setup_button("2", main_layout, 4, 1, 1, 1)
        self._setup_button("3", main_layout, 4, 2, 1, 1)
        self._setup_button("+", main_layout, 4, 3, 1, 1, ORANGE_BUTTON_STYLE)

        self._setup_button("0", main_layout, 5, 0, 1, 2)
        self._setup_button(".", main_layout, 5, 2, 1, 1)
        self._setup_button("=", main_layout, 5, 3, 1, 1, ORANGE_BUTTON_STYLE)

        widget = QWidget()
        widget.setLayout(main_layout)
        self.setCentralWidget(widget)

    def _setup_button(self, text, layout, row, col, row_span=1,
                      col_span=1, style=None):
        button = QPushButton(text)
        button.clicked.connect(lambda: self.button_pressed(button))
        if style:
            button.setStyleSheet(style)
        self.buttons[text] = button
        layout.addWidget(button, row, col, row_span, col_span)

    def button_pressed(self, button):
        if button.text() == "=":
            new_text = str(eval(self.label.text()))
        elif button.text() == "AC":
            new_text = "0"
        elif self.label.text() == "0" and button.text() not in OPERATORS:
            new_text = button.text()
        else:
            new_text = self.label.text() + button.text()
        self.label.setText(new_text)


def main():
    app = QApplication(sys.argv)

    # This is necessary not to lose the whole styling after changing one
    # thing in the stylesheet
    app.setStyle("Fusion")

    window = MainWindow()
    window.show()
    app.exec()


if __name__ == '__main__':
    main()
    

Similar Posts