from tkinter import messagebox
import tkinter as tk
from tkinter import ttk
import sqlite3
class Database:
def init(self):
self.conn = sqlite3.connect(“items.db”)
self.cur = self.conn.cursor()
self.create_table()
def create_table(self):
# Check if the table already exists
self.cur.execute('''SELECT count(name) FROM sqlite_master WHERE type='table' AND name='items' ''')
if self.cur.fetchone()[0] == 0:
# Table doesn't exist, create it along with the IsSold column
self.cur.execute('''CREATE TABLE IF NOT EXISTS items (
ItemId INTEGER PRIMARY KEY,
Item TEXT,
SizeNumeric TEXT,
Size TEXT,
Year TEXT,
Price REAL,
Condition TEXT,
IsSold INTEGER )''')
self.conn.commit()
def insert_item(self, item, size_numeric, size, year, price, condition):
# SQL query to insert an item into the table
self.cur.execute('''INSERT INTO items (Item, SizeNumeric, Size, Year, Price, Condition, IsSold)
VALUES (?, ?, ?, ?, ?, ?, ?)''', (item, size_numeric, size, year, price, condition, 0))
self.conn.commit()
def close(self):
self.conn.close() # Closes the connection when done
code for add item page
from tkinter import messagebox
class BuyItemPage:
def init(self, master):
self.master = master
master.title(“Buy Item”)
master.configure(bg=”#A7C7E7″)
# Set the initial size of the window
master.geometry("600x600")
# Create a Treeview widget
self.create_treeview()
# Fetch items from the database and insert them into the Treeview
self.populate_treeview()
def create_treeview(self):
# Create a Treeview widget
self.treeview = ttk.Treeview(self.master)
# Add columns
self.treeview['columns'] = ('Item ID', 'Item', 'SizeNumeric', 'Size', 'Year', 'Price', 'Condition', 'Sold')
# Define column headings
self.treeview.heading('Item ID', text='Item ID', anchor=tk.CENTER)
self.treeview.heading('Item', text='Item', anchor=tk.CENTER)
self.treeview.heading('SizeNumeric', text='SizeNumeric', anchor=tk.CENTER)
self.treeview.heading('Size', text='Size', anchor=tk.CENTER)
self.treeview.heading('Year', text='Year', anchor=tk.CENTER)
self.treeview.heading('Price', text='Price', anchor=tk.CENTER)
self.treeview.heading('Condition', text='Condition', anchor=tk.CENTER)
self.treeview.heading('Sold', text='Sold', anchor=tk.CENTER)
# Pack the Treeview
self.treeview.pack(expand=True, fill='both')
# Bind double click event to the Treeview
self.treeview.bind("<Double-1>", self.on_item_double_click)
def populate_treeview(self):
# Connect to the database
conn = sqlite3.connect("items.db")
cursor = conn.cursor()
# Fetch items from the database
cursor.execute("SELECT * FROM items")
items = cursor.fetchall()
# Fetch unique categories from the database
cursor.execute("SELECT DISTINCT Item FROM items")
categories = [row[0] for row in cursor.fetchall() if row[0]] # Exclude empty categories
# Close the database connection
conn.close()
# Clear existing items from the Treeview
self.treeview.delete(*self.treeview.get_children())
# Dictionary to store items by category
categorized_items = {category: [] for category in categories}
# Organize items into categories
for item in items:
category = item[1] # Assuming the second column represents the category
if category in categorized_items:
categorized_items[category].append(item)
# Populate the Treeview with categorized items
for category, items in categorized_items.items():
category_item = self.treeview.insert('', 'end', text=category, tags=("Category.Text",))
for item in items:
self.treeview.insert(category_item, 'end', values=item)
def on_item_double_click(self, event):
selected_item = self.treeview.selection()[0]
# Retrieve Item ID from the selected item
item_id = self.treeview.item(selected_item)['values'][0]
# Retrieve the item name for displaying in the messagebox
item_name = self.treeview.item(selected_item)['values'][1]
# Display a messagebox confirmation
response = messagebox.askyesno("Buy Now", "Buy Item")
if response: # If the user clicks "Yes"
self.mark_as_sold(item_id)
def mark_as_sold(self, item_id):
# Update the Treeview to reflect the change
selected_item = self.treeview.selection()[0]
self.treeview.set(selected_item, column='Sold', value='Yes')
rest of code