创建VAT计算器遇AttributeError:Label对象无get属性
增值税计算器数据库保存问题排查与解决
问题描述
我正在开发一款计算产品VAT(增值税)并将数据保存至SQLite3数据库的简易计算器。计算功能正常,但点击保存按钮时无法将数据存入数据库,弹出如下错误:
line 22, in save_prod vat = vat_total_lbl.get() ^^^^^^^^^^^^^^^^^ AttributeError: 'Label' object has no attribute 'get'
相关代码
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()
问题根源
vat_total_lbl和total_lbl是Tkinter的Label组件,Label没有get()方法,你错误地沿用了Entry组件的取值方式,导致抛出AttributeError。
修复方案
方案1:重新计算VAT和总价(推荐)
无需从Label提取数据,保存时直接基于输入的价格和税率重新计算,避免依赖Label的文本内容,同时增加输入验证:
def save_prod(): conn = sqlite3.connect('products.db') c = conn.cursor() try: name = name_entry.get().strip() if not name: status_lbl.config(text="Item name cannot be empty!") return price = float(price_entry.get()) if price <= 0: status_lbl.config(text="Price must be a positive number!") return vat_perc = float(vat_perc_cmb.get()) # 重新计算vat和total vat = price * vat_perc / 100 total = price + vat c.execute("""INSERT INTO products (name, price, vat_perc, vat, total) VALUES (?,?,?,?,?)""", (name, price, vat_perc, vat, total)) conn.commit() status_lbl.config(text="Product saved successfully") except ValueError: status_lbl.config(text="Invalid input! Check your price value.") finally: conn.close()
方案2:从Label文本中提取数值
如果一定要从Label取值,需要先获取Label的text属性,再提取其中的数值部分:
def save_prod(): conn = sqlite3.connect('products.db') c = conn.cursor() try: name = name_entry.get().strip() if not name: status_lbl.config(text="Item name cannot be empty!") return price = float(price_entry.get()) vat_perc = float(vat_perc_cmb.get()) # 提取Label中的数值 vat_text = vat_total_lbl.cget("text") if "Invalid input" in vat_text: status_lbl.config(text="Please calculate VAT first!") return vat = float(vat_text.split(": ")[1]) total_text = total_lbl.cget("text") total = float(total_text.split(": ")[1]) c.execute("""INSERT INTO products (name, price, vat_perc, vat, total) VALUES (?,?,?,?,?)""", (name, price, vat_perc, vat, total)) conn.commit() status_lbl.config(text="Product saved successfully") except (ValueError, IndexError): status_lbl.config(text="Invalid data! Ensure you've calculated VAT correctly.") finally: conn.close()
额外优化建议
- 将
CREATE TABLE逻辑移到程序初始化部分,无需每次保存时执行,提升效率 - 增加输入验证,比如检查名称是否为空、价格是否为正数
- 可以使用全局变量或类属性存储计算后的vat和total,避免重复计算或文本解析
内容的提问来源于stack exchange,提问作者Kate Murphy
相关产品推荐
相关产品推荐

