Python 3 Kivy Simple Arithmetic Popup Window Calculator GUI Script Desktop App Full Project For Beginners

Python 3 Kivy Simple Arithmetic Popup Window Calculator GUI Script Desktop App Full Project For Beginners

 

Welcome folks today in this blog post we will be building a simple arithmetic calculator in kivy using python. All the full source code of the application is given below.

 

 

Get Started

 

 

In order to get started you need to install the following library using the pip command as shown below

 

pip install kivy

 

After installing this library you need to make an app.py file and copy paste the following code

 

app.py

 

from kivy.app import App

from kivy.core.window import Window

from kivy.config import Config

from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.button import Button 
from kivy.uix.popup import Popup
from kivy.uix.scrollview import ScrollView

from kivy.properties import NumericProperty

class CalculatorWidget(GridLayout):
    int_button_width = 120
    int_button_height = 100
    int_button_font_size = 40

    button_width = NumericProperty(int_button_width)
    button_height = NumericProperty(int_button_height)
    button_font_size = NumericProperty(int_button_font_size)

    Window.size = (int_button_width * 2, 
        int_button_height * 3)

    def calc_error(self, error, calc_entry):
        content = BoxLayout(orientation='vertical')
        scrollview = ScrollView()

        close_popup = Button(text='Close this popup')
        error_message = Label(text=str(error))

        scrollview.add_widget(error_message)
        content.add_widget(scrollview)
        content.add_widget(close_popup)

        popup = Popup(title='An error occured',
            content=content, size_hint=(.8, .8))

        close_popup.bind(on_release=popup.dismiss)
        popup.open()

    def calculate(self, *args):
        calc_entry = self.ids.calc_input.text 
        if calc_entry != '':
            if calc_entry[0] in '1234567890-+':
                try:
                    ans = str(eval(calc_entry))
                    self.ids.calc_input.text = ans
                except Exception as error:
                    self.calc_error(error, calc_entry)
                    pass

    def delete(self, *args):
        self.ids.calc_input.text = self.ids.calc_input.text[:-1]

    def clear(self, *args):
        self.ids.calc_input.text = ''

    def switch(self, *args):
        calc_entry = self.ids.calc_input.text
        if calc_entry != '':
            if calc_entry[0] in '+1234567890':
                self.ids.calc_input.text = '-' + calc_entry
            if calc_entry[0] == '-':
                self.ids.calc_input.text = calc_entry[1:]

class CalculatorApp(App):
    def build(self):
        Config.set('graphics', 'resizable', '0')
        Config.set('graphics', 'width', '200')
        Config.set('graphics', 'height', '200')
        Config.write()
        return CalculatorWidget()

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

 

See also  Python 3 wxPython Script to Convert Bulk Images to PDF and PDF Merger Using Pillow and PyPDF2 GUI Desktop App Full Project For Beginners

 

Now you need to make another file called calculator.kv inside the root directory and copy paste the following code

 

calculator.kv

 

# width: self.parent.parent.button_width
# The above referencing used in the
# dynamic classes below follows like this:
# NumberButton (self), RowLayout (parent), 
# CalculatorWidget (parent), 120 (which is
# variable 'button_width' in Python code)

<NumberButton@Button>:
    on_release: self.parent.parent.ids.calc_input.text += self.text
    size_hint: None, None
    font_size: self.parent.parent.button_font_size
    width: self.parent.parent.button_width
    height: self.parent.parent.button_height

<ZeroButton@Button>:
    on_release: self.parent.parent.ids.calc_input.text += self.text
    size_hint: None, None
    font_size: self.parent.parent.button_font_size
    width: self.parent.parent.button_width * 2 
    height: self.parent.parent.button_height

<OperatorButton@Button>:
    on_release: self.parent.parent.ids.calc_input.text += self.text
    size_hint: None, None
    font_size: self.parent.parent.button_font_size
    width: self.parent.parent.button_width
    height: self.parent.parent.button_height
    

<EqualsButton@Button>:
    on_release: self.parent.parent.calculate()
    size_hint: None, None
    font_size: self.parent.parent.button_font_size
    width: self.parent.parent.button_width
    height: self.parent.parent.button_height


<SpecialButton@Button>:
    size_hint: None, None
    font_size: self.parent.parent.button_font_size
    width: self.parent.parent.button_width
    height: self.parent.parent.button_height
    background_color: [0, 0, 1, 1]

<RowLayout@BoxLayout>:
    size_hint: None, None

<CalculatorWidget>:
    rows: 6

    RowLayout:
        TextInput:
            id: calc_input
            font_size: 80
            size_hint: None, None
            height: root.button_height * 1.1
            width: root.button_width * 4
            readonly: True
            multiline: False

    RowLayout:
        SpecialButton:
            text: 'AC'
            on_release: self.parent.parent.clear()
        SpecialButton:
            text: '+/-'
            on_release: self.parent.parent.switch()
        SpecialButton:
            text: 'del'
            on_release: self.parent.parent.delete() 
        OperatorButton:
            text: '/'
    RowLayout: 
        NumberButton:
            text: '1'
        NumberButton:
            text: '2'
        NumberButton:
            text: '3'
        OperatorButton:
            text: '*'

    RowLayout:
        NumberButton:
            text: '4'
        NumberButton:
            text: '5'
        NumberButton:
            text: '6'
        OperatorButton:
            text: '-'

    RowLayout:
        NumberButton:
            text: '7'
        NumberButton:
            text: '8'
        NumberButton:
            text: '9'
        OperatorButton:
            text: '+'

    RowLayout:
        ZeroButton:
            text: '0'
        NumberButton:
            text: '.'
        EqualsButton:
            text: '='

 

See also  Python 3 Pandas Library Script to Plot Data Points in Graph Using Matplotlib From Excel File Full Project For Beginners

 

Now if you execute the above python script app.py by typing the below command as shown below

 

python app.py

 

 

Leave a Reply