I want GUI based python code should open by desk top of the computer. When Open maximize the window should show button Day Book. When Click Day book next frame should open in the same window. There should be Back button left side corner Top. And below of
To create a GUI application in Python that fulfills your requirements, we will use the `tkinter` library for the GUI and `sqlite3` as the database to store the Day Book entries. Below is a complete example of how this can be implemented.
### Requirements:
1. Install `tkinter` (usually comes pre-installed with Python).
2. Install `sqlite3` (also comes with standard Python library).
### Complete Code:
```python
import tkinter as tk
from tkinter import messagebox, ttk
import sqlite3
# Setting up the database
def init_db():
conn = sqlite3.connect('daybook.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS daybook (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date TEXT,
unit TEXT,
payment_mode TEXT,
cheque_no TEXT,
cheque_date TEXT,
bank TEXT,
amount REAL
)''')
conn.commit()
conn.close()
# Function to add data to the database
def add_entry():
conn = sqlite3.connect('daybook.db')
c = conn.cursor()
c.execute("INSERT INTO daybook (date, unit, payment_mode, cheque_no, cheque_date, bank, amount) VALUES (?, ?, ?, ?, ?, ?, ?)",
(entry_date.get(), entry_unit.get(), payment_mode.get(), entry_cheque_no.get(), entry_cheque_date.get(), entry_bank.get(), entry_amount.get()))
conn.commit()
conn.close()
load_data()
clear_fields()
# Function to load data into the table
def load_data():
for row in tree.get_children():
tree.delete(row)
conn = sqlite3.connect('daybook.db')
c = conn.cursor()
c.execute("SELECT * FROM daybook")
for row in c.fetchall():
tree.insert("", tk.END, values=row)
conn.close()
# Function to clear input fields
def clear_fields():
entry_date.delete(0, tk.END)
entry_unit.delete(0, tk.END)
entry_cheque_no.delete(0, tk.END)
entry_cheque_date.delete(0, tk.END)
entry_bank.delete(0, tk.END)
entry_amount.delete(0, tk.END)
payment_mode.set("")
# Create main application class
class DayBookApp:
def __init__(self, master):
self.master = master
self.master.title("Day Book")
self.master.geometry("800x600")
self.frame_home = tk.Frame(self.master)
self.frame_home.pack()
# Day Book Button
btn_daybook = tk.Button(self.frame_home, text="Day Book", command=self.open_daybook)
btn_daybook.pack(pady=20)
def open_daybook(self):
for widget in self.frame_home.winfo_children():
widget.destroy()
self.frame_daybook = tk.Frame(self.master)
self.frame_daybook.pack()
# Back Button
btn_back = tk.Button(self.frame_daybook, text="Back", command=self.back_to_home)
btn_back.grid(row=0, column=0, padx=10, pady=10)
# Entry Fields
tk.Label(self.frame_daybook, text="Date").grid(row=1, column=0)
tk.Label(self.frame_daybook, text="Unit").grid(row=1, column=1)
tk.Label(self.frame_daybook, text="Payment Mode").grid(row=1, column=2)
self.entry_date = tk.Entry(self.frame_daybook)
self.entry_unit = tk.Entry(self.frame_daybook)
self.payment_mode = tk.StringVar(value="Cash")
self.option_payment = tk.OptionMenu(self.frame_daybook, self.payment_mode, "Cash", "Cheque")
self.entry_date.grid(row=2, column=0)
self.entry_unit.grid(row=2, column=1)
self.option_payment.grid(row=2, column=2)
# Additional Fields for Cheque
tk.Label(self.frame_daybook, text="Cheque No").grid(row=3, column=0)
tk.Label(self.frame_daybook, text="Cheque Date").grid(row=3, column=1)
tk.Label(self.frame_daybook, text="Bank").grid(row=3, column=2)
tk.Label(self.frame_daybook, text="Amount").grid(row=3, column=3)
self.entry_cheque_no = tk.Entry(self.frame_daybook)
self.entry_cheque_date = tk.Entry(self.frame_daybook)
self.entry_bank = tk.Entry(self.frame_daybook)
self.entry_amount = tk.Entry(self.frame_daybook)
self.entry_cheque_no.grid(row=4, column=0)
self.entry_cheque_date.grid(row=4, column=1)
self.entry_bank.grid(row=4, column=2)
self.entry_amount.grid(row=4, column=3)
# Buttons
btn_add = tk.Button(self.frame_daybook, text="Add", command=add_entry)
btn_add.grid(row=5, column=0, pady=10)
self.load_button = tk.Button(self.frame_daybook, text="Load", command=load_data)
self.load_button.grid(row=5, column=1, pady=10)
# Treeview for the table
self.tree = ttk.Treeview(self.frame_daybook, columns=("ID", "Date", "Unit", "Payment Mode", "Cheque No", "Cheque Date", "Bank", "Amount"), show='headings')
self.tree.heading("ID", text="ID")
self.tree.heading("Date", text="Date")
self.tree.heading("Unit", text="Unit")
self.tree.heading("Payment Mode", text="Payment Mode")
self.tree.heading("Cheque No", text="Cheque No")
self.tree.heading("Cheque Date", text="Cheque Date")
self.tree.heading("Bank", text="Bank")
self.tree.heading("Amount", text="Amount")
self.tree.grid(row=6, column=0, columnspan=4, padx=10, pady=10)
load_data()
def back_to_home(self):
for widget in self.frame_daybook.winfo_children():
widget.destroy()
self.frame_home.pack()
if __name__ == "__main__":
init_db()
root = tk.Tk()
app = DayBookApp(root)
root.mainloop()
```
### Explanation:
1. **Database Initialization**: We use SQLite to create a database for the daybook entries.
2. **GUI Structure**: We create a main application class `DayBookApp`, which holds the primary structure of the GUI.
3. **Navigating UI**: The "Day Book" button leads to a new frame containing the data entry form and the table to display entries.
4. **Adding Data**: Users can fill in the data in the entry fields and click the "Add" button to insert the data into the database and update the table displayed.
5. **Buttons**: Besides the "Add" button, additional buttons can be created for Alter, Delete, and Save functionalities (currently, only Add works; you would add similar functions for alter and delete).
6. **Data Display**: The data is shown in a `Treeview` widget from `tkinter.ttk`.
### Note:
This example focuses on the basic functionalities according to your requirements. You can enhance it further by adding error handling and implementing the Alter, Delete, Save, and Search functionalities as needed.