Simple Item Details and Billing Application with Tkinter

 import tkinter as tk

from tkinter import ttk


# create a tkinter window

window = tk.Tk()


# set the window title

window.title("Item Details")


# create labels for the text boxes

item_name_label = ttk.Label(window, text="Item Name")

quantity_label = ttk.Label(window, text="Quantity")

price_label = ttk.Label(window, text="Price")


# create text boxes

item_name_entry = ttk.Entry(window)

quantity_entry = ttk.Entry(window)

price_entry = ttk.Entry(window)


# create a Treeview widget

tree = ttk.Treeview(window)


# define the Treeview columns

tree["columns"] = ("one", "two", "three")


# set the column headings

tree.heading("one", text="Item Name")

tree.heading("two", text="Quantity")

tree.heading("three", text="Price")


# add the Treeview to the window

tree.pack()


# add the labels and text boxes to the window

item_name_label.pack()

item_name_entry.pack()

quantity_label.pack()

quantity_entry.pack()

price_label.pack()

price_entry.pack()


# function to move data to Treeview when Down key is pressed

def on_down_key(event):

    # get the data from the text boxes

    item_name = item_name_entry.get()

    quantity = quantity_entry.get()

    price = price_entry.get()


    # insert the data into the Treeview

    tree.insert("", "end", values=(item_name, quantity, price))


# bind the Down key event to the price_entry widget

price_entry.bind("<Down>", on_down_key)


# function to generate bill

def generate_bill():

    # get the contents of the Treeview

    items = []

    for child in tree.get_children():

        item = tree.item(child)["values"]

        items.append(item)


    # print the bill to the console

    print("Item\tQuantity\tPrice")

    for item in items:

        print(f"{item[0]}\t{item[1]}\t\t{item[2]}")


# create a "Generate Bill" button

generate_bill_button = ttk.Button(window, text="Generate Bill", command=generate_bill)

generate_bill_button.pack()


# run the tkinter event loop

window.mainloop()


Comments