# TypeError: calc_result() takes 1 positional argument but 2 were given
from kivy.app import App
from kivy.uix.button import Button
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', 400)
Config.set('graphics', 'height', 500)
class MyApp(App):
#обновление текста BoxLayout при нажатии на кнопки калькулятора
def update_label(self):
self.lbl.text = self.formula
#написание чисел
def add_number(self, instance):
if(self.formula == '0'):
self.formula = ''
self.formula += str(instance.text)
self.update_label()
#написание операций
def add_operation(self, instance):
if(str(instance.text).lower() == '*'):
self.formula += '*'
elif (str(instance.text).lower() == '-'):
self.formula += '-'
elif (str(instance.text).lower() == '/'):
self.formula += '/'
else:
self.formula += '+'
#расчёт результата
def calc_result(self):
self.lbl.text = str(eval(self.lbl.text))
self.formula = '0'
#внешний вид калькулятора
def build(self):
self.formula = '0'
bl = BoxLayout(orientation = 'vertical', padding=[25])
gl = GridLayout(cols=4, padding = 30, spacing=3, size_hint = (1 , .6))
self.lbl = Label(text='0', font_size=50, size_hint=(1, .4))
bl.add_widget(self.lbl)
#кнопки калькулятора
gl.add_widget(Button(text='7',on_press = self.add_number))
gl.add_widget(Button(text='8',on_press = self.add_number))
gl.add_widget(Button(text='9',on_press = self.add_number))
gl.add_widget(Button(text='/', on_press = self.add_operation))
gl.add_widget(Button(text='4',on_press = self.add_number))
gl.add_widget(Button(text='5',on_press = self.add_number))
gl.add_widget(Button(text='6',on_press = self.add_number))
gl.add_widget(Button(text='*',on_press = self.add_operation))
gl.add_widget(Button(text='1',on_press = self.add_number))
gl.add_widget(Button(text='2',on_press = self.add_number))
gl.add_widget(Button(text='3',on_press = self.add_number))
gl.add_widget(Button(text='-',on_press = self.add_operation))
gl.add_widget(Button(text='0',on_press = self.add_number))
gl.add_widget(Button(text='=',on_press = self.calc_result))
gl.add_widget(Button(text='+',on_press = self.add_operation))
bl.add_widget(gl)
return bl
#запуск программы
if __name__ == '__main__':
MyApp().run()