Kivy simple calculator | Python kivy | Code

preview_player
Показать описание
This is a simple calculator made with Python library called kivy.

The whole calculator code is present in the comments section.

___________
|subscribe|
````````````
Music used in the video-
Song: Aeden & Joellé - I Feel Crazy [NCS Release]
Music provided by NoCopyrightSounds

#python #code #kivy #programming #coding #GUI
Рекомендации по теме
Комментарии
Автор

#calculator.py code
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget



class Box(Widget):

def show(self, num):
display = self.ids.display_result

if display.text == "0":
display.text = ""
display.text += num

def clear(self):
display = self.ids.display_result
display.text = "0"

def CE(self):
display = self.ids.display_result
l = len(display.text)
digit = display.text[:l-1]
display.text = digit

def equal(self):
display = self.ids.display_result
result = eval(display.text)
display.text = str(result)

if display.text == "69":
display.text += " OMG!"

class MyApp(App):
def build(self):
return Box()
MyApp().run()





#design.kv file

#:import utils kivy.utils

<Box>
BoxLayout:
orientation: "vertical"
size: root.width, root.height
TextInput:
id: display_result
text: "0"
foreground_color:
halign: "right"
background_color:
font_size: 62
size_hint: (1, 0.3)

GridLayout:
cols: 4
row: 5


Button:
id: btn
text: "AC"
font_size: 38
background_normal: ""
background_color:
color:
on_press: root.clear()


Button:
id: btn
text: "<"
font_size: 38
color:
background_normal: ""
background_color:
on_press: root.CE()

Button:
id: btn
text: "%"
font_size: 38
color:
background_normal: ""
background_color:
on_press: root.show("%")

Button:
id: btn
text: "÷"
color:
font_size: 38
background_normal:""
background_color:
on_press: root.show("/")


Button:
id: btn
text: "7"
font_size: 38
background_color:
on_press: root.show("7")

Button:
id: btn
text: "8"
font_size: 38
background_color:
on_press: root.show("8")

Button:
id: btn
text: "9"
font_size: 38
background_color:
on_press: root.show("9")

Button:
id: btn
text: "×"
color:
font_size: 38
background_normal:""
background_color:
on_press: root.show("*")


Button:
id: btn
text: "4"
font_size: 38
background_color:
on_press: root.show("4")

Button:
id: btn
text: "5"
font_size: 38
background_color:
on_press: root.show("5")

Button:
id: btn
text: "6"
font_size: 38
background_color:
on_press: root.show("6")

Button:
id: btn
text: "-"
color:
background_normal:""
font_size: 38
background_color:
on_press: root.show("-")


Button:
id: btn
text: "1"
font_size: 38
background_color:
on_press: root.show("1")

Button:
id: btn
text: "2"
font_size: 38
background_color:
on_press: root.show("2")

Button:
id: btn
text: "3"
font_size: 38
background_color:
on_press: root.show("3")

Button:
id: btn
text: "+"
color:
font_size: 38
background_normal:""
background_color:
on_press: root.show("+")


Button:
id: btn
text: "0"
font_size: 38
background_color:
on_press: root.show("0")

Button:
id: btn
text: "00"
font_size: 38
background_color:
on_press: root.show("00")

Button:
id: btn_dot
text: "."
color:
font_size: 38
background_normal:""
background_color:
on_press: root.show(".")

Button:
id: btn_equal
text: "="
color:
font_size: 38
background_normal:""
background_color:
on_press: root.equal()

PaperPython