Python GUI Day Book Application with Database Integration

Answered using GPT 40 by GPT40

Question:

I want GUI based python code should open by desk top of the computer and should have database with name BD. When run the code maximize the window full should show Day Book button and it should be left side corner top of the window and size little big. When Click Day book next frame should open in the same window. There should be Back button left side corner Top. And below of Back button should show following details Horizontally but good looking also. Date, Annual Rent, Building, Unit, Area, Payment Method should have option Cash or Cheque that should can choose, Cheque No, Cash/Cheque Date, Bank, Amount. Just below should have Add, Alter, Delete, Save & Save PDF, Display Table button. And below should be a table. In the table right side corner down should show Total Amount AED:, it is for to show total amount in the table. And there is Number of Transaction in the left side corner down. It is for to show number of transaction in the table. In the table should show only Cash/Cheuqe Da

Answer:

Creating a GUI application in Python that meets your specifications involves using a library like Tkinter for the GUI and SQLite for the database. Below is a sample code that demonstrates how to create a basic application with the features you described.

Make sure you have Python installed on your system. You can run this code in any Python environment that supports Tkinter.

Prerequisites

You need to have Python installed on your machine. Tkinter comes pre-installed with Python, but if you are using a virtual environment, ensure it is included.

Sample Code

import tkinter as tk
from tkinter import ttk, messagebox
import sqlite3

# Database setup
def setup_database():
    conn = sqlite3.connect('BD.db')
    cursor = conn.cursor()
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS transactions (
            id INTEGER PRIMARY KEY,
            date TEXT,
            annual_rent REAL,
            building TEXT,
            unit TEXT,
            area TEXT,
            payment_method TEXT,
            cheque_no TEXT,
            cash_cheque_date TEXT,
            bank TEXT,
            amount REAL
        )
    ''')
    conn.commit()
    conn.close()

# Main Application Class
class DayBookApp:
    def __init__(self, root):
        self.root = root
        self.root.title("Day Book Application")
        self.root.geometry("800x600")
        self.root.state('zoomed')  # Maximize the window

        self.create_widgets()
        setup_database()

    def create_widgets(self):
        # Day Book Button
        self.day_book_button = tk.Button(self.root, text="Day Book", font=("Arial", 16), command=self.open_day_book)
        self.day_book_button.place(x=10, y=10)

    def open_day_book(self):
        self.clear_window()
        
        # Back Button
        self.back_button = tk.Button(self.root, text="Back", command=self.create_widgets)
        self.back_button.place(x=10, y=10)

        # Input Fields
        self.date_label = tk.Label(self.root, text="Date:")
        self.date_label.place(x=10, y=50)
        self.date_entry = tk.Entry(self.root)
        self.date_entry.place(x=100, y=50)

        self.annual_rent_label = tk.Label(self.root, text="Annual Rent:")
        self.annual_rent_label.place(x=10, y=80)
        self.annual_rent_entry = tk.Entry(self.root)
        self.annual_rent_entry.place(x=100, y=80)

        self.building_label = tk.Label(self.root, text="Building:")
        self.building_label.place(x=10, y=110)
        self.building_entry = tk.Entry(self.root)
        self.building_entry.place(x=100, y=110)

        self.unit_label = tk.Label(self.root, text="Unit:")
        self.unit_label.place(x=10, y=140)
        self.unit_entry = tk.Entry(self.root)
        self.unit_entry.place(x=100, y=140)

        self.area_label = tk.Label(self.root, text="Area:")
        self.area_label.place(x=10, y=170)
        self.area_entry = tk.Entry(self.root)
        self.area_entry.place(x=100, y=170)

        self.payment_method_label = tk.Label(self.root, text="Payment Method:")
        self.payment_method_label.place(x=10, y=200)
        self.payment_method = ttk.Combobox(self.root, values=["Cash", "Cheque"])
        self.payment_method.place(x=150, y=200)

        self.cheque_no_label = tk.Label(self.root, text="Cheque No:")
        self.cheque_no_label.place(x=10, y=230)
        self.cheque_no_entry = tk.Entry(self.root)
        self.cheque_no_entry.place(x=100, y=230)

        self.cash_cheque_date_label = tk.Label(self.root, text="Cash/Cheque Date:")
        self.cash_cheque_date_label.place(x=10, y=260)
        self.cash_cheque_date_entry = tk.Entry(self.root)
        self.cash_cheque_date_entry.place(x=150, y=260)

        self.bank_label = tk.Label(self.root, text="Bank:")
        self.bank_label.place(x=10, y=290)
        self.bank_entry = tk.Entry(self.root)
        self.bank_entry.place(x=100, y=290)

        self.amount_label = tk.Label(self.root, text="Amount:")
        self.amount_label.place(x=10, y=320)
        self.amount_entry = tk.Entry(self.root)
        self.amount_entry.place(x=100, y=320)

        # Buttons
        self.add_button = tk.Button(self.root, text="Add", command=self.add_transaction)
        self.add_button.place(x=10, y=350)

        self.alter_button = tk.Button(self.root, text="Alter")
        self.alter_button.place(x=70, y=350)

        self.delete_button = tk.Button(self.root, text="Delete")
        self.delete_button.place(x=130, y=350)

        self.save_button = tk.Button(self.root, text="Save")
        self.save_button.place(x=190, y=350)

        self.save_pdf_button = tk.Button(self.root, text="Save PDF")
        self.save_pdf_button.place(x=250, y=350)

        self.display_table_button = tk.Button(self.root, text="Display Table", command=self.display_table)
        self.display_table_button.place(x=320, y=350)

        # Table
        self.table = ttk.Treeview(self.root, columns=("Date", "Annual Rent", "Building", "Unit", "Area", "Payment Method", "Cheque No", "Cash/Cheque Date", "Bank", "Amount"), show='headings')
        for col in self.table["columns"]:
            self.table.heading(col, text=col)
        self.table.place(x=10, y=400, width=780, height=150)

        # Total Amount and Number of Transactions
        self.total_amount_label = tk.Label(self.root, text="Total Amount AED: 0")
        self.total_amount_label.place(x=600, y=570)

        self.transaction_count_label = tk.Label(self.root, text="Number of Transactions: 0")
        self.transaction_count_label.place(x=10, y=570)

    def clear_window(self):
        for widget in self.root.winfo_children():
            widget.destroy()

    def add_transaction(self):
        # Add transaction to the database
        conn = sqlite3.connect('BD.db')
        cursor = conn.cursor()
        cursor.execute('''
            INSERT INTO transactions (date, annual_rent, building, unit, area, payment_method, cheque_no, cash_cheque_date, bank, amount)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
        ''', (
            self.date_entry.get(),
            self.annual_rent_entry.get(),
            self.building_entry.get(),
            self.unit_entry.get(),
            self.area_entry.get(),
            self.payment_method.get(),
            self.cheque_no_entry.get(),
            self.cash_cheque_date_entry.get(),
            self.bank_entry.get(),
            self.amount_entry.get()
        ))
        conn.commit()
        conn.close()
        messagebox.showinfo("Success", "Transaction added successfully!")
        self.display_table()

    def display_table(self):
        # Clear the table
        for row in self.table.get_children():
            self.table.delete(row)

        # Fetch data from the database
        conn = sqlite3.connect('BD.db')
        cursor = conn.cursor()
        cursor.execute('SELECT * FROM transactions')
        rows = cursor.fetchall()
        conn.close()

        total_amount = 0
        for row in rows:
            self.table.insert("", "end", values=row[1:])  # Skip the ID column
            total_amount += row[10]  # Amount is the last column

        self.total_amount_label.config(text=f"Total Amount AED: {total_amount}")
        self.transaction_count_label.config(text=f"Number of Transactions: {len(rows)}")

if __name__ == "__main__":
    root = tk.Tk()
    app = DayBookApp(root)
    root.mainloop()

Explanation of the Code:

  1. Database Setup: The setup_database function creates a SQLite database named BD.db and a table for transactions if it doesn't already exist.
  2. Main Application Class: The DayBookApp class initializes the main window and creates the necessary widgets.
  3. Widgets: The application has buttons, labels, and entry fields for user input. The "Day Book" button opens the transaction entry form.
  4. Transaction Management: The add_transaction method inserts a new transaction into the database, and the display_table method fetches and displays all transactions in a table format.
  5. Total Amount and Transaction Count: The application calculates and displays the total amount and the number of transactions.

Running the Code:

  1. Copy the code into a Python file (e.g., day_book.py).
  2. Run the file using Python: python day_book.py.
  3. The application window will open, and you can interact with it as specified.

Note:

  • This is a basic implementation. You may want to add error handling, validation, and additional features as needed.
  • The "Alter", "Delete", "Save", and "Save PDF" buttons are placeholders and do not have functionality implemented in this example. You can expand on these features as per your requirements.