Calculadora: corrigindo bugs na potenciação introduzidos na aula anterior

This commit is contained in:
Luiz Otávio
2023-03-04 12:30:28 -03:00
parent 1287ce5b5c
commit d25a359334

View File

@@ -61,7 +61,7 @@ class ButtonsGrid(QGridLayout):
def _makeGrid(self): def _makeGrid(self):
self.display.eqPressed.connect(self._eq) 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.clearPressed.connect(self._clear)
self.display.inputPressed.connect(self._insertToDisplay) self.display.inputPressed.connect(self._insertToDisplay)
self.display.operatorPressed.connect(self._configLeftOp) self.display.operatorPressed.connect(self._configLeftOp)
@@ -127,6 +127,7 @@ class ButtonsGrid(QGridLayout):
return return
self.display.insert(text) self.display.insert(text)
self.display.setFocus()
@Slot() @Slot()
def _clear(self): def _clear(self):
@@ -135,11 +136,13 @@ class ButtonsGrid(QGridLayout):
self._op = None self._op = None
self.equation = self._equationInitialValue self.equation = self._equationInitialValue
self.display.clear() self.display.clear()
self.display.setFocus()
@Slot() @Slot()
def _configLeftOp(self, text): def _configLeftOp(self, text):
displayText = self.display.text() # Deverá ser meu número _left displayText = self.display.text() # Deverá ser meu número _left
self.display.clear() # Limpa o display self.display.clear() # Limpa o display
self.display.setFocus()
# Se a pessoa clicou no operador sem # Se a pessoa clicou no operador sem
# configurar qualquer número # configurar qualquer número
@@ -159,7 +162,7 @@ class ButtonsGrid(QGridLayout):
def _eq(self): def _eq(self):
displayText = self.display.text() displayText = self.display.text()
if not isValidNumber(displayText): if not isValidNumber(displayText) or self._left is None:
self._showError('Conta incompleta.') self._showError('Conta incompleta.')
return return
@@ -168,8 +171,9 @@ class ButtonsGrid(QGridLayout):
result = 'error' result = 'error'
try: 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 = math.pow(self._left, self._right)
result = converToNumber(str(result))
else: else:
result = eval(self.equation) result = eval(self.equation)
except ZeroDivisionError: except ZeroDivisionError:
@@ -181,10 +185,16 @@ class ButtonsGrid(QGridLayout):
self.info.setText(f'{self.equation} = {result}') self.info.setText(f'{self.equation} = {result}')
self._left = result self._left = result
self._right = None self._right = None
self.display.setFocus()
if result == 'error': if result == 'error':
self._left = None self._left = None
@Slot()
def _backspace(self):
self.display.backspace()
self.display.setFocus()
def _makeDialog(self, text): def _makeDialog(self, text):
msgBox = self.window.makeMsgBox() msgBox = self.window.makeMsgBox()
msgBox.setText(text) msgBox.setText(text)
@@ -194,8 +204,10 @@ class ButtonsGrid(QGridLayout):
msgBox = self._makeDialog(text) msgBox = self._makeDialog(text)
msgBox.setIcon(msgBox.Icon.Critical) msgBox.setIcon(msgBox.Icon.Critical)
msgBox.exec() msgBox.exec()
self.display.setFocus()
def _showInfo(self, text): def _showInfo(self, text):
msgBox = self._makeDialog(text) msgBox = self._makeDialog(text)
msgBox.setIcon(msgBox.Icon.Information) msgBox.setIcon(msgBox.Icon.Information)
msgBox.exec() msgBox.exec()
self.display.setFocus()