Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Any improvements
#1
Hi

below is code for a VAT ( UK tax @ 20% ) calculator. Can calculate the tax and also back calculate from a final total.

It works, but was wondering if there is a more efficient coding for any of it.

Thanks

from tkinter import *

# set-up window
window = Tk()
window.geometry('280x290')
window.resizable(0, 0)
window.title('VAT calculator')
window.iconbitmap('Calculator.ico')


# Instructions for user
instruction = Label(window, text='Enter either net value in Net field or total \n value in Total field and press Calculate.', padx=10, pady=10)
instruction.grid(column=1, sticky=W)
instruction2 = Label(window, text='Press Clear for new calculation.', padx=5, pady=5)
instruction2.grid(column=1, sticky=W)


# clear function
def clear():
    net.delete(0, 'end')
    vat.delete(0, 'end')
    total.delete(0, 'end')


# calculate from net function
def net_calc(net_value):
    vat_plus_value = float(net_value) / 5
    total_plus_value = float(net_value) + float(vat_plus_value)

    net.delete(0, END)
    net_value_insert = "{: .2f}".format(float(net_value))
    net.insert(0, net_value_insert)

    vat.delete(0, END)
    vat_value_insert = "{: .2f}".format(float(vat_plus_value))
    vat.insert(0, vat_value_insert)

    total.delete(0, END)
    total_value_insert = "{: .2f}".format(float(total_plus_value))
    total.insert(0, total_value_insert)


# back calculate from total function
def total_calc(total_value):
    vat_minus_value = float(total_value) / 6
    net_minus_value = float(vat_minus_value) * 5

    net.delete(0, END)
    net_minus_value_insert = "{: .2f}".format(float(net_minus_value))
    net.insert(0, net_minus_value_insert)

    vat.delete(0, END)
    vat_minus_value_insert = "{: .2f}".format(float(vat_minus_value))
    vat.insert(0, vat_minus_value_insert)

    total.delete(0, END)
    total_minus_value_insert = "{: .2f}".format(float(total_value))
    total.insert(0, total_minus_value_insert)


# calculate add or minus function
# ensure only one field is used
# ensure only numbers entered in fields
def calculate():
    # acceptable character list
    char_ok = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',  '.']
    net_value = str(net.get())
    total_value = total.get()

    if len(net_value) > 0 and len(total_value) == 0:

        def split(net_entry):
            return list(net_entry)

        net_entry = str(net_value)
        net_value_split = (split(net_entry))

        check = any(item in char_ok for item in net_value_split)
        if check == True:
            net_calc(net_value)
        else:
            clear()

    elif len(net_value) == 0 and len(total_value) > 0:
        def split(total_entry):
            return list(total_entry)

        total_entry = str(total_value)
        total_value_split = (split(total_entry))

        check = any(item in char_ok for item in total_value_split)
        if check == True:
            total_calc(total_value)
        else:
            clear()

    else:
        clear()


# labels
label_1 = Label(window, text='Net', pady=10)
label_1.grid(row=3, sticky=W)

label_2 = Label(window, text='VAT', pady=10)
label_2.grid(row=4, sticky=W)

label_3 = Label(window, text='Total', pady=10)
label_3.grid(row=5, sticky=W)


# buttons
calculate_btn = Button(window, text='Calculate', width=10, bg='black', fg='white', command=calculate, padx=10, pady=5)
calculate_btn.grid(column=1, row=6, sticky=W)

clear_btn = Button(window, text='Clear', width=10,  bg='black', fg='white', command=clear, padx=10, pady=5)
clear_btn.grid(column=1, row=7, sticky=W)


# text fields
net = Entry(window)
net.grid(column=1, row=3, sticky=W)

vat = Entry(window)
vat.grid(column=1, row=4, sticky=W)

total = Entry(window)
total.grid(column=1, row=5, sticky=W)


# loop
window.mainloop()
Reply
#2
Program for calculating the VAT is interesting.
Somehow I can not open your program it only shown the blank windows with missing some file.

Many people overlook the "Business Percentage" program and rather go for somethings else and
some simply say that Business Percentage is a simple calculations and
rather avoiding it after all.

In the pass I have try to program the "Pricing Calculations" or
I personally called "Profit on Pricing" programed it on the programmable calculator.
With this there are four variable and user provide two knowns varibles the the program will find the remaining two variables from
[Cost] [Price] [Markup] and [Margin]

Program can solve in many combination pair on input.
1. Cost Price to Markup Margin
2. Cost Markup to Price Margin
3. Cost Margin to Price Markup
4....and more.................

I now got this program to work on Python and this program can solve for VAT as well. Simple imput your VAT% to [Markup] and Price include VAT to [PRICE] then solve for [COST] this will give result of the Price excluding VAT.

Gamo 7/2020

Here is the "Profit on Pricing" Calculator

Profit on Pricing Calulator

Gamo 7/2020
Reply
#3
Ok now your program is working just by remove the code
on line 8

Thank You

Gamo 7/2020
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Improvements for first script? alloydog 9 4,260 Jan-01-2021, 02:50 PM
Last Post: perfringo
  [split] Any improvements Adamstiffman 1 1,869 May-31-2020, 06:16 AM
Last Post: pyzyx3qwerty
  Coding improvements chris_drak 2 33,400 May-02-2020, 11:39 AM
Last Post: chris_drak

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020