From d25a35933427a6bfc2b40e1d1f8b229b85c513ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ot=C3=A1vio?= Date: Sat, 4 Mar 2023 12:30:28 -0300 Subject: [PATCH] =?UTF-8?q?Calculadora:=20corrigindo=20bugs=20na=20potenci?= =?UTF-8?q?a=C3=A7=C3=A3o=20introduzidos=20na=20aula=20anterior?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aula202-calculadora/buttons.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/aula202-calculadora/buttons.py b/aula202-calculadora/buttons.py index e5e154a..17a16ff 100644 --- a/aula202-calculadora/buttons.py +++ b/aula202-calculadora/buttons.py @@ -61,7 +61,7 @@ class ButtonsGrid(QGridLayout): def _makeGrid(self): self.display.eqPressed.connect(self._eq) - self.display.delPressed.connect(self.display.backspace) + self.display.delPressed.connect(self._backspace) self.display.clearPressed.connect(self._clear) self.display.inputPressed.connect(self._insertToDisplay) self.display.operatorPressed.connect(self._configLeftOp) @@ -127,6 +127,7 @@ class ButtonsGrid(QGridLayout): return self.display.insert(text) + self.display.setFocus() @Slot() def _clear(self): @@ -135,11 +136,13 @@ class ButtonsGrid(QGridLayout): self._op = None self.equation = self._equationInitialValue self.display.clear() + self.display.setFocus() @Slot() def _configLeftOp(self, text): displayText = self.display.text() # Deverá ser meu número _left self.display.clear() # Limpa o display + self.display.setFocus() # Se a pessoa clicou no operador sem # configurar qualquer número @@ -159,7 +162,7 @@ class ButtonsGrid(QGridLayout): def _eq(self): displayText = self.display.text() - if not isValidNumber(displayText): + if not isValidNumber(displayText) or self._left is None: self._showError('Conta incompleta.') return @@ -168,8 +171,9 @@ class ButtonsGrid(QGridLayout): result = 'error' try: - if '^' in self.equation and isinstance(self._left, float): + if '^' in self.equation and isinstance(self._left, (float, int)): result = math.pow(self._left, self._right) + result = converToNumber(str(result)) else: result = eval(self.equation) except ZeroDivisionError: @@ -181,10 +185,16 @@ class ButtonsGrid(QGridLayout): self.info.setText(f'{self.equation} = {result}') self._left = result self._right = None + self.display.setFocus() if result == 'error': self._left = None + @Slot() + def _backspace(self): + self.display.backspace() + self.display.setFocus() + def _makeDialog(self, text): msgBox = self.window.makeMsgBox() msgBox.setText(text) @@ -194,8 +204,10 @@ class ButtonsGrid(QGridLayout): msgBox = self._makeDialog(text) msgBox.setIcon(msgBox.Icon.Critical) msgBox.exec() + self.display.setFocus() def _showInfo(self, text): msgBox = self._makeDialog(text) msgBox.setIcon(msgBox.Icon.Information) msgBox.exec() + self.display.setFocus()