Calculadora: permitindo apenas números válidos no display
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
from display import Display
|
from display import Display
|
||||||
from PySide6.QtCore import Slot
|
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, isValidNumber
|
||||||
from variables import MEDIUM_FONT_SIZE
|
from variables import MEDIUM_FONT_SIZE
|
||||||
|
|
||||||
|
|
||||||
@@ -15,7 +15,6 @@ 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):
|
||||||
@@ -54,5 +53,10 @@ class ButtonsGrid(QGridLayout):
|
|||||||
return realSlot
|
return realSlot
|
||||||
|
|
||||||
def _insertButtonTextToDisplay(self, button):
|
def _insertButtonTextToDisplay(self, button):
|
||||||
button_text = button.text()
|
buttonText = button.text()
|
||||||
self.display.insert(button_text)
|
newDisplayValue = self.display.text() + buttonText
|
||||||
|
|
||||||
|
if not isValidNumber(newDisplayValue):
|
||||||
|
return
|
||||||
|
|
||||||
|
self.display.insert(buttonText)
|
||||||
|
|||||||
@@ -7,5 +7,15 @@ def isNumOrDot(string: str):
|
|||||||
return bool(NUM_OR_DOT_REGEX.search(string))
|
return bool(NUM_OR_DOT_REGEX.search(string))
|
||||||
|
|
||||||
|
|
||||||
|
def isValidNumber(string: str):
|
||||||
|
valid = False
|
||||||
|
try:
|
||||||
|
float(string)
|
||||||
|
valid = True
|
||||||
|
except ValueError:
|
||||||
|
valid = False
|
||||||
|
return valid
|
||||||
|
|
||||||
|
|
||||||
def isEmpty(string: str):
|
def isEmpty(string: str):
|
||||||
return len(string) == 0
|
return len(string) == 0
|
||||||
|
|||||||
Reference in New Issue
Block a user