Python Forum
[Tkinter] Entry box not showing 2 decimal places
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Entry box not showing 2 decimal places
#1
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()
Reply
#2
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)
Reply
#3
Oh my g!
Was looking and looking and couldn't see it.
thanks.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Update variable using tkinter entry methon drSlump 6 280 Oct-15-2021, 08:01 AM
Last Post: drSlump
  Tkinter | entry output. Sap2ch 1 177 Sep-25-2021, 12:38 AM
Last Post: Yoriz
  [Tkinter] Dropdown box showing weird entry cybertooth 4 427 Aug-16-2021, 03:45 PM
Last Post: deanhystad
  tkinter showing image in button rwahdan 3 936 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  .get() from generated Entry widgets in tkinter snakes 4 895 May-03-2021, 11:26 PM
Last Post: snakes
  [Tkinter] Tkinter Textbox only showing one character Xylianth 1 697 Jan-29-2021, 02:59 AM
Last Post: Xylianth
  Entry Validation in tkinter shahulvk 4 2,257 Oct-28-2020, 10:12 PM
Last Post: joe_momma
  [Tkinter] Getting Input from Tkinter Entry juliabrushett 6 15,246 May-30-2020, 03:29 PM
Last Post: Larz60+
  Converting Entry field value to integer in tkinter scratchmyhead 2 2,432 May-11-2020, 03:41 PM
Last Post: scratchmyhead
  [Tkinter] Tkinter adding entry values scratchmyhead 1 898 May-04-2020, 05:21 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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