33

今天编程四个小时,用python做了一个日历记事本

import tkinter as tk

from tkinter import messagebox

from tkinter import ttk

from datetime import datetime

import sqlite3

 

def init_db():

    conn = sqlite3.connect('calendar.db')

    c = conn.cursor()

    c.execute('''CREATE TABLE IF NOT EXISTS events

                 (id INTEGER PRIMARY KEY,

                  title TEXT,

                  description TEXT,

                  date TEXT,

                  time TEXT)''')

    conn.commit()

    conn.close()

 

def add_event(title, description, date, time):

    conn = sqlite3.connect('calendar.db')

    c = conn.cursor()

    c.execute("INSERT INTO events (title, description, date, time) VALUES (?, ?, ?, ?)", (title, description, date, time))

    conn.commit()

    conn.close()

 

def edit_event(event_id, title, description, date, time):

    conn = sqlite3.connect('calendar.db')

    c = conn.cursor()

    c.execute("UPDATE events SET title = ?, description = ?, date = ?, time = ? WHERE id = ?", (title, description, date, time, event_id))

    conn.commit()

    conn.close()

 

def delete_event(event_id):

    conn = sqlite3.connect('calendar.db')

    c = conn.cursor()

    c.execute("DELETE FROM events WHERE id = ?", (event_id,))

    conn.commit()

    conn.close()

 

def get_events():

    conn = sqlite3.connect('calendar.db')

    c = conn.cursor()

    c.execute("SELECT * FROM events")

    events = c.fetchall()

    conn.close()

    return events

 

def check_overlapping_events(title, date, time):

    conn = sqlite3.connect('calendar.db')

    c = conn.cursor()

    c.execute("SELECT * FROM events WHERE date = ? AND time = ?", (date, time))

    existing_events = c.fetchall()

    conn.close()

    if len(existing_events) > 0:

        messagebox.showwarning("警告", "存在过于紧张的日程安排")

 

class CalendarApp:

    def __init__(self, root):

        self.root = root

        self.root.title("日历记事本")

        self.create_widgets()

        

    def create_widgets(self):

        # Main Frame

        main_frame = ttk.Frame(self.root, padding="10")

        main_frame.pack(fill=tk.BOTH, expand=True)

        

        # Title Entry

        self.title_label = ttk.Label(main_frame, text="标题  ")

        self.title_label.grid(row=0, column=0, sticky=tk.W, pady=5)

        self.title_entry = ttk.Entry(main_frame)

        self.title_entry.grid(row=0, column=1, sticky=tk.EW, pady=5)

        

        # Description Entry

        self.description_label = ttk.Label(main_frame, text="描述  ")

        self.description_label.grid(row=1, column=0, sticky=tk.W, pady=5)

        self.description_entry = ttk.Entry(main_frame)

        self.description_entry.grid(row=1, column=1, sticky=tk.EW, pady=5)

        

        # Date Entry

        self.date_label = ttk.Label(main_frame, text="日期  ")

        self.date_label.grid(row=2, column=0, sticky=tk.W, pady=5)

        self.date_entry = ttk.Entry(main_frame)

        self.date_entry.insert(0, datetime.now().strftime('%Y-%m-%d'))  # Default to current date

        self.date_entry.grid(row=2, column=1, sticky=tk.EW, pady=5)

        

        # Time Entry

        self.time_label = ttk.Label(main_frame, text="时间  ")

        self.time_label.grid(row=3, column=0, sticky=tk.W, pady=5)

        self.time_entry = ttk.Entry(main_frame)

        self.time_entry.insert(0, datetime.now().strftime('%H:%M'))  # Default to current time

        self.time_entry.grid(row=3, column=1, sticky=tk.EW, pady=5)

        

        # Button Frame

        button_frame = ttk.Frame(main_frame, padding="10")

        button_frame.grid(row=4, column=0, columnspan=2, pady=10)

        

        # Add Event Button

        self.add_button = ttk.Button(button_frame, text="添加事件", command=self.add_event)

        self.add_button.pack(side=tk.LEFT, padx=5)

        

        # View Events Button

        self.view_button = ttk.Button(button_frame, text="查看事件", command=self.view_events)

        self.view_button.pack(side=tk.LEFT, padx=5)

        

        # Edit Event Button

        self.edit_button = ttk.Button(button_frame, text="编辑事件", command=self.edit_event)

        self.edit_button.pack(side=tk.LEFT, padx=5)

        

        # Delete Event Button

        self.delete_button = ttk.Button(button_frame, text="删除事件", command=self.delete_event)

        self.delete_button.pack(side=tk.LEFT, padx=5)

        

        

        

        # Configure grid weights

        main_frame.columnconfigure(1, weight=1)

        

    def add_event(self):

        title = self.title_entry.get()

        description = self.description_entry.get()

        date = self.date_entry.get()

        time = self.time_entry.get()

        

        # Add the event to the database

        add_event(title, description, date, time)

        

        # Check for overlapping events

        check_overlapping_events(title, date, time)

 

    def view_events(self):

        events = get_events()

        event_list = "\n".join([f"ID: {event[0]}, 标题: {event[1]}, 描述: {event[2]}, 日期: {event[3]}, 时间: {event[4]}" for event in events])

        messagebox.showinfo("事件列表", event_list)

        

    def edit_event(self):

        pass

        

    def delete_event(self):

        pass

 

# Initialize database and create table if not exists

init_db()

 

root = tk.Tk()

app = CalendarApp(root)

root.mainloop()

posted @ 2024-05-22 22:09  不会JAVA的小袁  阅读(11)  评论(0编辑  收藏  举报