我最近开始学习Kivy并制作了一个计算器应用程序,但我不知道如何在按下一个计算按钮时从计算中删除以前的文本,并且只有在使用clear时才删除文本。
下面是代码https://github.com/Rakshan22/Calcy2。有人知道这个问题的答案吗?谢谢你的帮助!
您应该识别文本何时是最终答案,并在添加新文本之前重置文本。
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()