Create bill calculator GUI app using Tkinter in Python

This application allows the user to input the name, price, and quantity of items, and it calculates the total bill.

First, make sure you have Python installed on your machine. Tkinter comes pre-installed with Python, so you don’t need to install it separately.

import tkinter as tk

from tkinter import messagebox

class BillCalculatorApp:

def __init__(self, root):

self.root = root self.root.title(“Bill Calculator”)

self.items = []

self.create_widgets()

def create_widgets(self):

tk.Label(self.root, text=“Item Name”).grid(row=0, column=0)

self.item_name_entry = tk.Entry(self.root)

self.item_name_entry.grid(row=0, column=1)

tk.Label(self.root, text=“Item Price”).grid(row=1, column=0)

self.item_price_entry = tk.Entry(self.root)

self.item_price_entry.grid(row=1, column=1)

tk.Label(self.root, text=“Quantity”).grid(row=2, column=0)

self.item_quantity_entry = tk.Entry(self.root)

self.item_quantity_entry.grid(row=2, column=1)

self.add_item_button = tk.Button(self.root, text=“Add Item”, command=self.add_item)

self.add_item_button.grid(row=3, column=0, columnspan=2)

self.bill_text = tk.Text(self.root, width=50, height=15)

self.bill_text.grid(row=4, column=0, columnspan=2)

self.calculate_total_button = tk.Button(self.root, text=“Calculate Total”, command=self.calculate_total)

self.calculate_total_button.grid(row=5, column=0, columnspan=2)

def add_item(self):

try:

name = self.item_name_entry.get()

price = float(self.item_price_entry.get())

quantity = int(self.item_quantity_entry.get())

if not name:

raise ValueError(“Item name cannot be empty”)

self.items.append((name, price, quantity))

self.bill_text.insert(tk.END, f”Item: {name}, Price: {price}, Quantity: {quantity}\n”)

self.item_name_entry.delete(0, tk.END)

self.item_price_entry.delete(0, tk.END)

self.item_quantity_entry.delete(0, tk.END)

except ValueError as e:

messagebox.showerror(“Input Error”, str(e))

def calculate_total(self):

total = sum(price * quantity for name, price, quantity in self.items)

self.bill_text.insert(tk.END, f”\nTotal Bill: {total}\n”)

if __name__ == “__main__”:

root = tk.Tk()

app = BillCalculatorApp(root)

root.mainloop()

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top