@Klamet

Как калькулятору добавить очистку строки? Python, framework kivy?

Как добавить кнопку AC, как бывает в калькуляторах для очистки строки?

from kivy.app import App

from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout

from kivy.config import Config
Config.set('graphics', 'resizable', 0)
Config.set('graphics', 'width', 350)
Config.set('graphics', 'height', 500)

class CalculatorApp(App):
	def update_label(self):
		self.lbl.text = self.formula

	def clear(instance, self):
		self.formula = 0

	def add_number(self, instance):
		if( self.formula == "0" ):
			self.formula = ""
			
		self.formula += str(instance.text)
		self.update_label()

	def add_operation(self, instance):
		self.formula += str(instance.text)
		self.update_label()

	def calc_result(self, instance):
		self.lbl.text = str(eval(self.lbl.text))
		self.formula = "0"

	def build(self):
		self.formula = "0"

		bl = BoxLayout(orientation = 'vertical', padding=10)
		gl = GridLayout(cols = 4, spacing=3, size_hint = (1, .6))

		self.lbl = Label(text="0", font_size=40, halign="right", valign="center", size_hint=(1, .4), text_size=(375 - 50, 500 * .4 - 75))
		bl.add_widget( self.lbl )

		gl.add_widget( Button(text="7", font_size=18, on_press = self.add_number) )
		gl.add_widget( Button(text="8", font_size=18, on_press = self.add_number) )
		gl.add_widget( Button(text="9", font_size=18, on_press = self.add_number) )
		gl.add_widget( Button(text="*", font_size=18, on_press = self.add_operation) )

		gl.add_widget( Button(text="4", font_size=18, on_press = self.add_number) )
		gl.add_widget( Button(text="5", font_size=18, on_press = self.add_number) )
		gl.add_widget( Button(text="6", font_size=18, on_press = self.add_number) )
		gl.add_widget( Button(text="-", font_size=18, on_press = self.add_operation) )

		gl.add_widget( Button(text="1", font_size=18, on_press = self.add_number) )
		gl.add_widget( Button(text="2", font_size=18, on_press = self.add_number) )
		gl.add_widget( Button(text="3", font_size=18, on_press = self.add_number) )
		gl.add_widget( Button(text="+", font_size=18, on_press = self.add_operation) )

		gl.add_widget( Button(text="/", font_size=18, on_press = self.add_operation) )
		gl.add_widget( Button(text="0", font_size=18, on_press = self.add_number) )
		gl.add_widget( Button(text=".", font_size=18, on_press = self.add_number) )
		gl.add_widget( Button(text="=", font_size=18, on_press = self.calc_result) )

		bl.add_widget( gl )
		return bl

if __name__ == "__main__":
	CalculatorApp().run()
  • Вопрос задан
  • 70 просмотров
Решения вопроса 1
@Umpiro
gl.add_widget( Button(text="AC", font_size=18, on_press=self.clear) )
, может быть?
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы