0
import sys
from PyQt6 import QtTest
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import (
    QApplication,
    QMainWindow,
    QWidget,
    QGridLayout,
    QVBoxLayout,
    QLineEdit,
    QPushButton
)

ErrorMessage = "#ERROR"

WINDOWSIZE = 350
LINEHEIGHT = 50
BUTTONSIZE = 50

class Calculator(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Calculator')
        self.setFixedSize(WINDOWSIZE, WINDOWSIZE)
        self.generalLayout = QVBoxLayout()
        central_widgit = QWidget()
        central_widgit.setLayout(self.generalLayout)
        self.setCentralWidget(central_widgit)
        self._createDisplay()
        self._createButtons()


    def _createDisplay(self):
        self.display = QLineEdit()
        self.display.setFixedHeight(LINEHEIGHT)
        self.display.setAlignment(Qt.AlignmentFlag.AlignRight)
        self.display.setReadOnly(True)
        self.generalLayout.addWidget(self.display)

    def _createButtons(self):
        self.buttonMap = {}
        buttonsLayout = 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(BUTTONSIZE, BUTTONSIZE)
                buttonsLayout.addWidget(self.buttonMap[key], row, col)
        self.generalLayout.addLayout(buttonsLayout)

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

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

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


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


def main():
    calculatorApp = QApplication([])
    calculatorApp.setStyleSheet(
        """ 
        QWidget {
          color: #f00;
          background-color: #20213f; 
        }
        
        QPushButton {
            border: 1px solid skyblue;
            border-radius: 20px;
        }
        
        QPushButton:hover {
            background-color: pink;
        }
        
        QLineEdit {
            border: 1px solid #202;
            background-color: #20213f;
        }
        """
    )
    calculatorWindow = Calculator()
    calculatorWindow.show()
    sys.exit(calculatorApp.exec())

if __name__ == "__main__":
    main()

I want to apply style on QGridLayout(on _create button function). Plus how can i add selector for QGridLayout so that i can apply style on particular grid when i have more then two grid layouts. I wanted to assign selector(id and class) in the same way we do in css like .addSelectorMethod(selector_name) so that i can directly apply style on particular widget/layout as i want.

  • Styling only works on Qt widgets. Layout managers are **not** widgets. QSS [supports selectors](https://doc.qt.io/qt-5/stylesheet-syntax.html#selector-types) (again, only for widgets) based on some aspects, but consider that QSS is **based** on CSS2.1 and it only supports its basic concepts. Read the documentation to know what properties are actually supported. – musicamante Sep 13 '22 at 02:49

0 Answers0