Untitled

mail@pastecode.io avatar
unknown
plain_text
17 days ago
3.4 kB
1
Indexable
Never
import tkinter as tk
from tkinter import ttk, messagebox
import sqlite3
from datetime import datetime

# Database setup
conn = sqlite3.connect('calendar_app.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS events (
                id INTEGER PRIMARY KEY,
                title TEXT NOT NULL,
                description TEXT,
                date TEXT NOT NULL,
                time TEXT NOT NULL)''')
conn.commit()

# Functions
def add_event():
    title = title_entry.get()
    description = description_entry.get("1.0", tk.END).strip()
    date = date_entry.get()
    time = time_entry.get()
    
    if not title or not date or not time:
        messagebox.showwarning("Input Error", "Title, Date, and Time are required!")
        return
    
    c.execute("INSERT INTO events (title, description, date, time) VALUES (?, ?, ?, ?)",
              (title, description, date, time))
    conn.commit()
    refresh_events()
    clear_inputs()

def delete_event(event_id):
    c.execute("DELETE FROM events WHERE id=?", (event_id,))
    conn.commit()
    refresh_events()

def clear_inputs():
    title_entry.delete(0, tk.END)
    description_entry.delete("1.0", tk.END)
    date_entry.delete(0, tk.END)
    time_entry.delete(0, tk.END)

def refresh_events():
    for item in events_tree.get_children():
        events_tree.delete(item)
    c.execute("SELECT * FROM events")
    for row in c.fetchall():
        events_tree.insert("", tk.END, values=row)

# GUI setup
root = tk.Tk()
root.title("Simple Calendar App")

# Input frame
input_frame = ttk.Frame(root, padding="10")
input_frame.grid(row=0, column=0, sticky=(tk.W, tk.E))

# Title
ttk.Label(input_frame, text="Title:").grid(row=0, column=0, sticky=tk.W, pady=5)
title_entry = ttk.Entry(input_frame, width=30)
title_entry.grid(row=0, column=1, pady=5)

# Description
ttk.Label(input_frame, text="Description:").grid(row=1, column=0, sticky=tk.W, pady=5)
description_entry = tk.Text(input_frame, width=30, height=4)
description_entry.grid(row=1, column=1, pady=5)

# Date
ttk.Label(input_frame, text="Date (YYYY-MM-DD):").grid(row=2, column=0, sticky=tk.W, pady=5)
date_entry = ttk.Entry(input_frame, width=30)
date_entry.grid(row=2, column=1, pady=5)

# Time
ttk.Label(input_frame, text="Time (HH:MM):").grid(row=3, column=0, sticky=tk.W, pady=5)
time_entry = ttk.Entry(input_frame, width=30)
time_entry.grid(row=3, column=1, pady=5)

# Add button
add_button = ttk.Button(input_frame, text="Add Event", command=add_event)
add_button.grid(row=4, column=1, pady=5)

# Events frame
events_frame = ttk.Frame(root, padding="10")
events_frame.grid(row=1, column=0, sticky=(tk.W, tk.E))

# Events treeview
columns = ("ID", "Title", "Description", "Date", "Time")
events_tree = ttk.Treeview(events_frame, columns=columns, show="headings")
for col in columns:
    events_tree.heading(col, text=col)
events_tree.grid(row=0, column=0, sticky=(tk.W, tk.E))

# Delete button
def on_delete_event():
    selected_item = events_tree.selection()[0]
    event_id = events_tree.item(selected_item)['values'][0]
    delete_event(event_id)

delete_button = ttk.Button(events_frame, text="Delete Event", command=on_delete_event)
delete_button.grid(row=1, column=0, pady=5)

refresh_events()

# Main loop
root.mainloop()
Leave a Comment