Python Forum

Full Version: [Tkinter] Entry box not showing 2 decimal places
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
below is code(still a work in progress so commented out code that has been played with etc) that will be a VAT calculator. VAT being Value Added Tax in the UK, 20% on many items.Enter your net value in the net box and press calculate to do the maths. The back calculation of enter the gross amount in the total Entry box and back calculate the tax is not done yet.

The part I have trouble with is:- Line 36 and 37, changes the net amount to 2 decimal places and enter into Net entry field. This works fine.
However, lines 40 and 41, and lines 45 and 46 should do the same for the vat field and the total field. But they only end up with 1 decimal place, even though the code is the same. There are extra print statements as I wanted to see what the code output was and it is correct, but doesn't transfer to the GUI.

Any ideas? Really does not make sense to me.

Thanks in advance.

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)


# calculate add or minus function
def calculate():
    # acceptable character list
    char_ok = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',  '.']
    net_value = net.get()
    #total_value = total.get()

    if float(net_value) in char_ok:
        return True
    # from net value calculate vata and total values
    if True:
        vat_plus_value = float(net_value)/5
        total_plus_value = float(net_value) + float(vat_plus_value)
        print(net_value)
        print(vat_plus_value)
        print(total_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, float(vat_value_insert))
        print(vat_value_insert)

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


    # if int(total_value) in char_ok:
        # return True

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


# 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)
# net.insert(0, '0.00')

vat = Entry(window)
vat.grid(column=1, row=4, sticky=W)
# vat.insert(0, '0.00')

total = Entry(window)
total.grid(column=1, row=5, sticky=W)
# total.insert(0, '0.00')


# loop
window.mainloop()
For vat and and total you are not using the formatted string. Do the same thing you are doing for net.
        vat.delete(0, END)
        vat_value_insert = "{: .2f}".format(float(vat_plus_value))
        vat.insert(0, float(vat_value_insert))  # inserting a float, not the formatted string
        print(vat_value_insert)
Oh my g!
Was looking and looking and couldn't see it.
thanks.
It is tough to see what is really there. So much easier to see what you expect to see. That is the advantage of a fresh set of eyes. They have no expectations.