Calculadora: criando um Slot com dados para o Signal "clicked" de cada botão
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
from display import Display
|
||||||
|
from PySide6.QtCore import Slot
|
||||||
from PySide6.QtWidgets import QGridLayout, QPushButton
|
from PySide6.QtWidgets import QGridLayout, QPushButton
|
||||||
from utils import isEmpty, isNumOrDot
|
from utils import isEmpty, isNumOrDot
|
||||||
from variables import MEDIUM_FONT_SIZE
|
from variables import MEDIUM_FONT_SIZE
|
||||||
@@ -13,10 +15,11 @@ class Button(QPushButton):
|
|||||||
font.setPixelSize(MEDIUM_FONT_SIZE)
|
font.setPixelSize(MEDIUM_FONT_SIZE)
|
||||||
self.setFont(font)
|
self.setFont(font)
|
||||||
self.setMinimumSize(75, 75)
|
self.setMinimumSize(75, 75)
|
||||||
|
self.setCheckable(True)
|
||||||
|
|
||||||
|
|
||||||
class ButtonsGrid(QGridLayout):
|
class ButtonsGrid(QGridLayout):
|
||||||
def __init__(self, *args, **kwargs) -> None:
|
def __init__(self, display: Display, *args, **kwargs) -> None:
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
self._gridMask = [
|
self._gridMask = [
|
||||||
@@ -26,6 +29,7 @@ class ButtonsGrid(QGridLayout):
|
|||||||
['1', '2', '3', '+'],
|
['1', '2', '3', '+'],
|
||||||
['', '0', '.', '='],
|
['', '0', '.', '='],
|
||||||
]
|
]
|
||||||
|
self.display = display
|
||||||
self._makeGrid()
|
self._makeGrid()
|
||||||
|
|
||||||
def _makeGrid(self):
|
def _makeGrid(self):
|
||||||
@@ -37,3 +41,18 @@ class ButtonsGrid(QGridLayout):
|
|||||||
button.setProperty('cssClass', 'specialButton')
|
button.setProperty('cssClass', 'specialButton')
|
||||||
|
|
||||||
self.addWidget(button, rowNumber, colNumber)
|
self.addWidget(button, rowNumber, colNumber)
|
||||||
|
buttonSlot = self._makeButtonDisplaySlot(
|
||||||
|
self._insertButtonTextToDisplay,
|
||||||
|
button,
|
||||||
|
)
|
||||||
|
button.clicked.connect(buttonSlot) # type: ignore
|
||||||
|
|
||||||
|
def _makeButtonDisplaySlot(self, func, *args, **kwargs):
|
||||||
|
@Slot(bool)
|
||||||
|
def realSlot(_):
|
||||||
|
func(*args, **kwargs)
|
||||||
|
return realSlot
|
||||||
|
|
||||||
|
def _insertButtonTextToDisplay(self, button):
|
||||||
|
button_text = button.text()
|
||||||
|
self.display.insert(button_text)
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ if __name__ == '__main__':
|
|||||||
window.addWidgetToVLayout(display)
|
window.addWidgetToVLayout(display)
|
||||||
|
|
||||||
# Grid
|
# Grid
|
||||||
buttonsGrid = ButtonsGrid()
|
buttonsGrid = ButtonsGrid(display)
|
||||||
window.vLayout.addLayout(buttonsGrid)
|
window.vLayout.addLayout(buttonsGrid)
|
||||||
|
|
||||||
# Executa tudo
|
# Executa tudo
|
||||||
|
|||||||
Reference in New Issue
Block a user