Search code examples
pythontkinterattributeerror

AttributeError: 'Label' object has no attribute 'get' : creating a VAT calculator


I am creating a simple calculator to calculate the VAT of a product and then save to an SQLite3 database. The system will calculate perfectly but will not save to the database. The following error pops up when the save button is clicked:

line 22, in save_prod
    vat = vat_total_lbl.get()
          ^^^^^^^^^^^^^^^^^
AttributeError: 'Label' object has no attribute 'get'

The code is as follows:

import tkinter as tk
from tkinter import ttk
import sqlite3

def save_prod():
conn = sqlite3.connect('products.db')
c = conn.cursor()

    c.execute("""CREATE TABLE IF NOT EXISTS products (
                name text,
                price real,
                vat_perc real,
                vat real,
                total real
                )""")

    name = name_entry.get()
    price = price_entry.get()
    vat_perc = vat_perc_cmb.get()
    vat = vat_total_lbl.get()
    total = total_lbl.get()

    c.execute("""INSERT INTO products (name, price, vat_perc, vat, total)
            VALUES (?,?,?,?,?)""", (name, price, vat_perc, vat, total))

    conn.commit()
    conn.close()
    status_lbl.config(text="Product saved successfully")

def vat():
try:
price = float(price_entry.get())
vat_perc = float(vat_perc_cmb.get())
vat = price \* vat_perc / 100
total = price + vat
vat_total_lbl.config(text="VAT Amount: {:.2f}".format(vat))
total_lbl.config(text="Total Price: {:.2f}".format(total))
\#in case non numeric values entered
except ValueError:
vat_total_lbl.config(text="Invalid input!")
total_lbl.config(text="Invalid input!")

window = tk.Tk()
window.title("Create a New Product")

prod_create_label = tk.Label(text="Create a New Product")
prod_create_label.grid(row=0, column=2)

name_lbl = tk.Label(text="Item Name: ")
name_lbl.grid(row=2, column=0)

name_entry = tk.Entry()
name_entry.grid(row=3, column=0)

price_lbl = tk.Label(text="Item Price: ")
price_lbl.grid(row=2, column=1)

price_entry = tk.Entry()
price_entry.grid(row=3, column=1)

vat_perc_lbl = tk.Label(text="Item's VAT Percentage: ")
vat_perc_lbl.grid(row=2, column=2)

course=\["23", "13.5", "9"\]
vat_perc_cmb = ttk.Combobox(window, value=course, width=10)
vat_perc_cmb.grid(row=3, column=2)
vat_perc_cmb.current(2)

cal_vat_btn = tk.Button(text="Calculate VAT and Total Price", command=vat)
cal_vat_btn.grid(row=3, column=3)

vat_lbl = tk.Label(text="Item's VAT total: ")
vat_lbl.grid(row=4, column=0)

vat_total_lbl = tk.Label()
vat_total_lbl.grid(row=5, column=0)

total = tk.Label(text="Item's total price: ")
total.grid(row=4, column=1)

total_lbl = tk.Label()
total_lbl.grid(row=5, column=1)

save_btn = tk.Button(text="Save Product", command=save_prod)
save_btn.grid(row=6, column=0)

status_lbl = tk.Label(text="")
status_lbl.grid(row=6, column=1)

window.mainloop()

Solution

  • Label widgets don't have a get() method, which is why you're seeing this error. If you want to query the text of a given Label you can use the cget() method (configuration-get, as it were), e.g.:

    vat = vat_total_lbl.cget('text')
    total = total_lbl.cget('text')