0

I am building a simple calculator in PyQt5. I am using the following code:

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox, QVBoxLayout, QTextBrowser
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot


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

    def initUI(self):
        self.setWindowTitle('Calculator')
        self.setGeometry(100, 100, 500, 300)

        self.layout = QVBoxLayout(self)
        self.setLayout(self.layout)

        self.number = QLineEdit(self)
        self.number.move(50, 50)
        self.number.resize(280, 40)
        self.number.setPlaceholderText('First number')
        self.layout.addChildWidget(self.number)

        self.number2 = QLineEdit(self)
        self.number2.move(50, 100)
        self.number2.resize(280, 40)
        self.number2.setPlaceholderText('Second number')
        self.layout.addChildWidget(self.number2)

        # minus button
        self.minus = QPushButton('-', self)
        self.minus.move(80, 180)
        self.minus.resize(20, 40)
        self.minus.clicked.connect(self.minus_onclick)
        self.layout.addChildWidget(self.minus)

        # plus button
        self.plus = QPushButton('+', self)
        self.plus.move(120, 180)
        self.plus.resize(20, 40)
        self.plus.clicked.connect(self.plus_onclick)
        self.layout.addChildWidget(self.plus)

        # * button
        self.into = QPushButton('*', self)
        self.into.move(160, 180)
        self.into.resize(20, 40)
        self.into.clicked.connect(self.into_onclick)
        self.layout.addChildWidget(self.into)

        # division button
        self.div = QPushButton('/', self)
        self.div.move(200, 180)
        self.div.resize(20, 40)
        self.div.clicked.connect(self.div_onclick)
        self.layout.addChildWidget(self.div)

        #equalto button
        self.equal = QPushButton('=', self)
        self.equal.move(240, 180)
        self.equal.resize(20, 40)
        self.equal.clicked.connect(self.equal_onclick)
        self.layout.addChildWidget(self.equal)


    def plus_onclick(self):
        op = '+'

    def minus_onclick(self):
        op = '-'

    def into_onclick(self):
        op = '*'

    def div_onclick(self):
        op = '/'

    #final calculator code
    def equal_onclick(self):
        num1 = int(self.number.text())
        num2 = int(self.number2.text())
        if op == '+':
            result = num1 + num2
            QMessageBox.question(self, 'Result-' + result , QMessageBox.Ok, QMessageBox.Ok)
        elif op == '-':
            result = num1 - num2
            QMessageBox.question(self, 'Result-' + result , QMessageBox.Ok, QMessageBox.Ok)
        elif op == '*':
            result = num1 * num2
            QMessageBox.question(self, 'Result-' + result , QMessageBox.Ok, QMessageBox.Ok)
        elif op == '/':
            result = num1 / num2
            QMessageBox.question(self, 'Result-' + result , QMessageBox.Ok, QMessageBox.Ok)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()

    sys.exit(app.exec_())

However, when I press the '=' button, it gives me this error: NameError: name 'op' is not defined

I have no absolutely no idea why python is throwing this error, can someone please point out the error? The name op is defined, I don't understand the reason for the error. Thanks in advance. :)

Sid
  • 2,174
  • 1
  • 13
  • 29
  • 1
    You want to prefix all your `op` usages with `self.` so that you use `self.op` instead. That way, you are actually setting the value within your window instance instead of writing to local variables that are effectively thrown away (and explicitly not available within your `equal_onclick` method). – poke Sep 02 '19 at 06:20

1 Answers1

2

You should be referring to op as self.op if you want the variable to be visible in other class methods.

In your def equal_onclick(self): method you simply refer to it as if op == '+': and the method attempts to treat this as a variable that should be defined within the method, and as the error tells you, it does not know what this op variable is or contains.

MurrayW
  • 393
  • 2
  • 10