In this video we’ll build out the addition functionality of our calculator with Kivy and Python.

In the last video, we built out the basic gui for our calculator app. In this video we’ll add in some functionality.

First, we’ll write code to deal with pressing number buttons and entering them into the text box at the top of our calculator.
Then we’ll work on the math buttons (plus, minus, multiplication, and division).

After that we’ll start to work on the equal button, but only for addition.

Python Code: calc.py
GitHub Code: calc.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.core.window import Window

# Set the app size
Window.size = (500,700)

# Designate Our .kv design file 
Builder.load_file('calc.kv')

class MyLayout(Widget):
	def clear(self):
		self.ids.calc_input.text = '0'

	# Create a button pressing function
	def button_press(self, button):
		# create a variable that contains whatever was in the text box already
		prior = self.ids.calc_input.text
		
		# determine if 0 is sitting there
		if prior == "0":
				self.ids.calc_input.text = ''
				self.ids.calc_input.text = f'{button}'
		else: 
			self.ids.calc_input.text = f'{prior}{button}'

	# create addition function
	def add(self):
		# create a variable that contains whatever was in the text box already
		prior = self.ids.calc_input.text
		
		# slap a plus sign to the text box
		self.ids.calc_input.text = f'{prior}+'

	# create addition function
	def subtract(self):
		# create a variable that contains whatever was in the text box already
		prior = self.ids.calc_input.text
		
		# slap a plus sign to the text box
		self.ids.calc_input.text = f'{prior}-'

	# create addition function
	def multiply(self):
		# create a variable that contains whatever was in the text box already
		prior = self.ids.calc_input.text
		
		# slap a plus sign to the text box
		self.ids.calc_input.text = f'{prior}*'

	# create addition function
	def divide(self):
		# create a variable that contains whatever was in the text box already
		prior = self.ids.calc_input.text
		
		# slap a plus sign to the text box
		self.ids.calc_input.text = f'{prior}/'

	# create equals to function
	def equals(self):
		prior = self.ids.calc_input.text

		# Addition
		if "+" in prior:
			num_list = prior.split("+")
			answer = 0
			# loop thru our list
			for number in num_list:
				answer = answer + int(number)

			# print the answer in the text box
			self.ids.calc_input.text = str(answer)


class CalculatorApp(App):
	def build(self):
		return MyLayout()

if __name__ == '__main__':
	CalculatorApp().run()



Kivy Design Code: calc.kv
GitHub Code: calc.kv

<MyLayout>
	BoxLayout:
		orientation: "vertical"
		size: root.width, root.height
		
		TextInput:
			id: calc_input
			text: "0"
			halign: "right"
			font_size: 65
			size_hint: (1, .15)

		GridLayout:
			cols: 4
			rows: 5

			# Row
			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "%"

			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "CE"

			Button:
				id: clear
				size_hint: (.2, .2)
				font_size: 32
				text: "C"
				on_press: root.clear()
				
			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "/"
				on_press: root.divide()

			# Row
			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "7"
				background_color: (157/255, 157/255, 157/255, 1)
				on_press: root.button_press(7)

			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "8"
				background_color: (157/255, 157/255, 157/255, 1)
				on_press: root.button_press(8)

			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "9"
				background_color: (157/255, 157/255, 157/255, 1)
				on_press: root.button_press(9)

			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "x"
				on_press: root.multiply()

			# Row
			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "4"
				background_color: (157/255, 157/255, 157/255, 1)
				on_press: root.button_press(4)

			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "5"
				background_color: (157/255, 157/255, 157/255, 1)
				on_press: root.button_press(5)

			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "6"
				background_color: (157/255, 157/255, 157/255, 1)
				on_press: root.button_press(6)

			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "-"
				on_press: root.subtract()

			# Row
			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "1"
				background_color: (157/255, 157/255, 157/255, 1)
				on_press: root.button_press(1)

			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "2"
				background_color: (157/255, 157/255, 157/255, 1)
				on_press: root.button_press(2)

			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "3"
				background_color: (157/255, 157/255, 157/255, 1)
				on_press: root.button_press(3)

			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "+"
				on_press: root.add()

			# Row
			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "+/-"
				background_color: (157/255, 157/255, 157/255, 1)

			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "0"
				background_color: (157/255, 157/255, 157/255, 1)
				on_press: root.button_press(0)

			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "."
				background_color: (157/255, 157/255, 157/255, 1)


			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "="
				on_press: root.equals()


John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of the first dot com boom. After that he developed the award-winning Submission-Spider search engine submission software that's been used by over 3 million individuals, businesses, and governments in over 42 countries. He's written several Amazon #1 best selling books on coding, and runs a popular Youtube coding channel.

View all posts

Add comment

Your email address will not be published. Required fields are marked *

John Elder