Why does button.clicked.connect gives unexpected output when i set callback for it in pyqt6?

Viewed 18
import sys
from functools import partial
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import (
    QApplication,
    QWidget,
    QMainWindow,
    QGridLayout,
    QVBoxLayout,
    QLineEdit,
    QPushButton
)

ErrorMessage = "ERROR"


class CalculatorUi(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Calculator")
        self.setFixedSize(300, 300)
        self.generatLayout = QVBoxLayout()
        central_widget = QWidget()
        central_widget.setLayout(self.generatLayout)
        self.setCentralWidget(central_widget)
        self._createDisplay()
        self._createButtons()

    def _createDisplay(self):
        self.display = QLineEdit()
        self.display.setReadOnly(True)
        self.display.setMaxLength(12)
        self.display.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.display.setFixedHeight(50)
        self.generatLayout.addWidget(self.display)

    def _createButtons(self):
        self.buttonMap = {}
        buttonLayout = QGridLayout()
        keyboard = [
            ["7", "8", "9", "/", "C"],
            ["4", "5", "6", "*", "("],
            ["1", "2", "3", "-", ")"],
            ["0", "00", ".", "+", "="]
        ]


        for row, keys in enumerate(keyboard):
            for col,key in enumerate(keys):
                self.buttonMap[key] = QPushButton(key)
                self.buttonMap[key].setFixedSize(50, 50)
                buttonLayout.addWidget(self.buttonMap[key], row, col)

        self.generatLayout.addLayout(buttonLayout)

    def _getDisplayText(self):
        return self.display.text()

    def _setText(self, text):
        self.display.setText(text)

    def _clearDisplay(self):
        self.display.setText('')


class Calculate:
    def __init__(self, model, view):
        self._evaluate = model
        self._view = view
        self._connectSignalsAndSLot()


    def _connectSignalsAndSLot(self):
        for key, button in self._view.buttonMap.items():
            if key not in ("C", "="):
                button.clicked.connect(
                    lambda: print(key)
                )




def evaluate_expression(expression):
    try:
        result = str(eval(expression))
    except Exception:
        result = ErrorMessage
    return result


def main():
    app = QApplication([])
    calculator = CalculatorUi()
    calculator.show()
    Calculate(evaluate_expression, calculator)
    sys.exit(app.exec())

if __name__ == "__main__":
    main()

what i am expecting is that i want print the key that i pressed other than 'C' and '='. But wheneever i click some numeric button and arthematic operator button it just prints out '='. i want it to print the button that i pressed i couldnt understant this behavior. Is there any wrong with my callback or is it due to some other reasonss. plus if i use partial function from the functools it works perfectly fine.

0 Answers
Related