Categories
By Aman Kharwal

Python Kivy Tutorial – Accounting Vouchers

In this tutorial we will create a Accounting Vouchers application using Python and the Kivy language.

Kivy is a free, open source Python library that allows for quick and easy development of highly interactive multi platform applications.

Kivy’s execution speed is comparable to the native mobile alternative, Java for Android or Objective C for iOS.

Moreover, Kivy has the huge advantage of being able to run on multiple platforms, just as HTML does; in which case, Kivy performs better because it doesn’t rely on a heavy browser, and many of its components are implemented in C using the Cython library in such a way that most of the graphics processing runs directly in the GPU.

In this tutorial we will create a Accounting Vouchers application using Python and the Kivy language. If you are new here then I will recommend you to first see my tutorial on creating a Clock application with python kivy.

Creating an Accounting Vouchers application

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label

import re

class OperatorWindow(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.cart = []
        self.qty = []
        self.total = 0.00

    def update_purchases(self):
        pcode = self.ids.code_inp.text
        products_container = self.ids.products
        if pcode == '1234' or pcode == '2345':
            details = BoxLayout(size_hint_y=None,height=30,pos_hint={'top': 1})
            products_container.add_widget(details)

            code = Label(text=pcode,size_hint_x=.2,color=(.06,.45,.45,1))
            name = Label(text='python',size_hint_x=.3,color=(.06,.45,.45,1))
            qty = Label(text='1',size_hint_x=.1,color=(.06,.45,.45,1))
            disc = Label(text='0.00',size_hint_x=.1,color=(.06,.45,.45,1))
            price = Label(text='0.00',size_hint_x=.1,color=(.06,.45,.45,1))
            total = Label(text='0.00',size_hint_x=.2,color=(.06,.45,.45,1))
            details.add_widget(code)
            details.add_widget(name)
            details.add_widget(qty)
            details.add_widget(disc)
            details.add_widget(price)
            details.add_widget(total)

            #Update Preview
            pname = "Product One"
            if pcode == '2345':
                pname = "Product Two"
            pprice = 1.00
            pqty = str(1)
            self.total += pprice
            purchase_total = '`\n\nTotal\t\t\t\t\t\t\t\t'+str(self.total)
            self.ids.cur_product.text = pname
            self.ids.cur_price.text = str(pprice)
            preview = self.ids.receipt_preview
            prev_text = preview.text
            _prev = prev_text.find('`')
            if _prev > 0:
                prev_text = prev_text[:_prev]

            ptarget = -1
            for i,c in enumerate(self.cart):
                if c == pcode:
                    ptarget = i

            if ptarget >= 0:
                pqty = self.qty[ptarget]+1
                self.qty[ptarget] = pqty
                expr = '%s\t\tx\d\t'%(pname)
                rexpr = pname+'\t\tx'+str(pqty)+'\t'
                nu_text = re.sub(expr,rexpr,prev_text)
                preview.text = nu_text + purchase_total
            else:
                self.cart.append(pcode)
                self.qty.append(1)
                nu_preview = '\n'.join([prev_text,pname+'\t\tx'+pqty+'\t\t'+str(pprice),purchase_total])
                preview.text = nu_preview



class OperatorApp(App):
    def build(self):
        return OperatorWindow()

if __name__=="__main__":
    oa = OperatorApp()
    oa.run()
<FlatButton@ButtonBehavior+Label>:
    font_size: 14

<OperatorWindow>:
    id: main_win
    orientation: 'vertical'
    canvas.before:
        Color:
            rgba: (1,1,1, 1)
        Rectangle:
            size: self.size
            pos: self.pos

    BoxLayout:
        id: header
        size_hint_y: None
        height: 40
        canvas.before:
            Color:
                rgba: (.90, .20, .20, .90)
            Rectangle:
                size: self.size
                pos: self.pos
        Label:
            text: 'Accounting Vouchers'
            size_hint_x: .9
            bold: True
            color: (1,1,1,1)
        FlatButton:
            id: loggedin_user
            text: 'thecleverprogrammer'
            color: (1,1,1,1)

    BoxLayout:
        id: current
        size_hint_y: None
        height: 50
        canvas.before:
            Color:
                rgba: (1,1,1, 1)
            Rectangle:
                size: self.size
                pos: self.pos
        Button:
            text: 'Current Item:'
            background_normal: ''
            background_color: (.90, .20, .20, .90)
            size_hint_x: .4
        Button:
            id: cur_product
            text: 'Default Product'
            background_normal: ''
            background_color: (.90, .20, .20, .90)
        Button:
            id: cur_price
            text: '0.00'
            background_normal: ''
            background_color: (.90, .20, .20, .90)
            size_hint_x: .2

    BoxLayout:
        padding: 10
        BoxLayout:
            id: product_details
            orientation: "vertical"
            size_hint_x: .8
            spacing: 10

            BoxLayout:
                id: product_labels
                size_hint_y: None
                height: 40
                canvas.before:
                    Color:
                        rgba: (.90,.20,.20, .90)
                    Rectangle:
                        size: self.size
                        pos: self.pos

                FlatButton:
                    text: 'Quantity'
                    size_hint_x: .1
                FlatButton:
                    text: 'Product Code'
                    size_hint_x: .3
                FlatButton:
                    text: 'Discount'
                    size_hint_x: .1
                FlatButton:
                    text: 'Disc %'
                    size_hint_x: .1
                FlatButton:
                    text: 'VAT'
                    size_hint_x: .1
                FlatButton:
                    text: 'Price'
                    size_hint_x: .1
                FlatButton:
                    text: 'Product Total'
                    size_hint_x: .2

            BoxLayout:
                id: product_inputs
                size_hint_y: None
                height: 30
                spacing: 5
                TextInput:
                    id: qty_inp
                    size_hint_x: .1
                TextInput:
                    id: code_inp
                    size_hint_x: .3
                    multiline: False
                    on_text_validate: root.update_purchases()
                TextInput:
                    id: disc_inp
                    size_hint_x: .1
                TextInput:
                    id: disc_perc_inp
                    size_hint_x: .1
                TextInput:
                    id: vat_inp
                    size_hint_x: .1
                TextInput:
                    id: price_inp
                    size_hint_x: .1
                TextInput:
                    id: total_inp
                    size_hint_x: .2
            BoxLayout:
                id: add_to_cart
                orientation: 'vertical'
                BoxLayout:
                    size_hint_y: None
                    height: 30
                    canvas.before:
                        Color:
                            rgba: (.90, .20,.20, .90)
                        Rectangle:
                            size: self.size
                            pos: self.pos
                    Label:
                        text: 'Code'
                        size_hint_x: .2
                    Label:
                        text: 'Product name'
                        size_hint_x: .3
                    Label:
                        text: 'Qty'
                        size_hint_x: .1
                    Label:
                        text: 'Disc'
                        size_hint_x: .1
                    Label:
                        text: 'Price'
                        size_hint_x: .1
                    Label:
                        text: 'Total'
                        size_hint_x: .2
                GridLayout:
                    id: products
                    cols: 1


        BoxLayout:
            id: preview
            orientation: 'vertical'
            size_hint_x: .2

            TextInput:
                id: receipt_preview
                readonly: True
                text: 'thecleverprogrammer\nKalka ji,\nNew Delhi(110019\nIndia\nTel: 8587992208\nReceipt No: \nDate: \n\n'
    BoxLayout:
        id: footer
        size_hint_y: None
        height:30
        canvas.before:
            Color:
                rgba: (.90, .20, .20, .90)
            Rectangle:
                pos: self.pos
                size: self.size
        Label:
            text: 'Maintained By Aman Kharwal'

#Output

Leave a Reply