In this video we’ll finish our calculator’s math buttons using the Python eval() function.

Eval() will take a string and break it apart and evaluate it, ie do the math.

So we don’t have to write code for each of the buttons (add, subtract, multiply, divide), eval will take care of all of it for us.

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
		
		# Test for error first
		if "Error" in prior:
			prior = ''

		# 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 Function to remove last character in text box
	def remove(self):
		prior = self.ids.calc_input.text
		# Remove The last item in the textbox
		prior = prior[:-1]
		# Output back to the textbox
		self.ids.calc_input.text = prior
	
	# Create function to make text box positive or negative
	def pos_neg(self):
		prior = self.ids.calc_input.text
		# Test to see if there's a - sign already
		if "-" in prior:
			 self.ids.calc_input.text = f'{prior.replace("-", "")}'
		else:
			self.ids.calc_input.text = f'-{prior}'

	# Create decimal function
	def dot(self):
		prior = self.ids.calc_input.text
		# Split out text box by +
		num_list = prior.split("+")
		
		if "+" in prior and "." not in num_list[-1]:
			# Add a decimal to the end of the text
			prior = f'{prior}.'
			# Output back to the text box
			self.ids.calc_input.text = prior

		elif "." in prior:
			pass
		else:
			# Add a decimal to the end of the text
			prior = f'{prior}.'
			# Output back to the text box
			self.ids.calc_input.text = prior

	# create addition function
	def math_sign(self, sign):
		# 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}{sign}'

	
	# create equals to function
	def equals(self):
		prior = self.ids.calc_input.text
		 # Error Handling
		try:
		 	# Evaluate the math from the text box
			answer = eval(prior)
			# Output the answer
			self.ids.calc_input.text = str(answer)
		except:
			self.ids.calc_input.text = "Error"

		'''
		# Addition
		if "+" in prior:
			num_list = prior.split("+")
			answer = 0.0
			# loop thru our list
			for number in num_list:
				answer = answer + float(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: "C"
				on_press: root.clear()

			Button:
				id: clear
				size_hint: (.2, .2)
				font_size: 32
				text: u"\u00AB"
				on_press: root.remove()
				
			Button:
				size_hint: (.2, .2)
				font_size: 32
				text: "/"
				on_press: root.math_sign("/")

			# 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.math_sign("*")

			# 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.math_sign("-")

			# 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.math_sign("+")

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

			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)
				on_press: root.dot()

			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