Python:
#Imports
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.config import Config
#Set window size and adding unresizable function
Config.set('graphics', 'resizable', '0', );
Config.set('graphics', 'width', 400);
Config.set('graphics', 'height', 500);
#The word App must be in the end of the class name
class CalcApp(App):
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() == "x"):
self.formula += "*"
else:
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"
#creating a boxlayout for the label
bl = BoxLayout(orientation = 'vertical', padding = 25)
#creating a gridlayout for the buttons(4 coloums, space of 3)
gl = GridLayout(cols = 4, spacing = 3, size_hint = (1, .6))
#creating label for the text(halign means where the text should be aligned horizontally and valign vertically, text_size-makes the texture of the text bigger to align the text correctly)
self.lbl = Label(text = "0", font_size = 40, halign = "right", valign = "center", size_hint = (1, .4), text_size = (400 - 50, 500 * .4 - 50))
bl.add_widget( self.lbl )
#creating buttons
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 = 'X', 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(Widget())
gl.add_widget(Button(text = '0', on_press = self.add_number))
gl.add_widget(Button(text = ',', on_press = self.add_number))
gl.add_widget(Button(text = '=', on_press = self.calc_result))
bl.add_widget(gl)
#return bl-showing the label with the text
return bl
#Make sure file is starting from console
if __name__ == "__main__":
CalcApp().run()
Вложения
Последнее редактирование: