In this video we’ll build out secondary buttons for our calculator app with Kivy and Python.
So let’s build out the “+/-“, the “.”, and the “<<" buttons for our calculator. 
The "+/-" button will toggle our textbox numbers positive or negative, the "." button will add a decimal to our number, and the "<<" button will remove the last number from the text box.
These are pretty basic buttons and we should be able to knock them out in just a few minutes!
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 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
		
		if "." 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
		# 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()
	
	
	

 
         
                               
                               
                              
Add comment