Superiortime
Superiortime

Reputation: 1

How to remove previous text from a calculation?

I have recently started learning Kivy and made a calculator app but I can't figure out how to remove the previous text from a calculation when a button is pressed for the next calculation and the text only gets removed when clear is used.

Here is the code https://github.com/Rakshan22/Calcy2 . So does anyone here know the answer to this question? Thanks for the assistance!

Upvotes: 0

Views: 94

Answers (1)

Erick T Oliveira
Erick T Oliveira

Reputation: 124

You should identify when the text is the final answer and reset the text before adding new one.

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.widget import Widget

Window.size = (350, 450)


class MainWidget(Widget):
    def __init__(self):
        self.textIsResult = false

    def clear(self):
        self.ids.input.text=""
        

    def back(self):
        expression = self.ids.input.text
        expression = expression[:1]
        self.ids.input.text = expression


    def pressed(self, button):
        expression = self.ids.input.text

        if self.textIsResult:
            self.ids.input.text = f"{button}"

        if "Fault" in expression:
            expression = ""
            self.textIsResult = false

        if expression == "0":
            self.ids.input.text = ""
            self.ids.input.text = f"{button}"

        else:
            self.ids.input.text = f"{expression}{button}"

    def answer(self):
        expression = self.ids.input.text
        try:
            self.ids.input.text = str(eval(expression))
            self.textIsResult = true

        except:
            self.ids.input.text = "Fault"

class TheLabApp(App):
    pass

TheLabApp().run()

Upvotes: 0

Related Questions