import sys import json import sqlite3 import datetime # ========================================== # 0. 開啟 Chromium GPU 高幀率與渲染優化參數 # ========================================== sys.argv.extend([ '--ignore-gpu-blocklist', '--enable-gpu-rasterization', '--enable-zero-copy', '--disable-frame-rate-limit', ]) from PyQt6.QtCore import Qt, QObject, pyqtSlot, QUrl from PyQt6.QtWidgets import QApplication, QMainWindow from PyQt6.QtWebEngineWidgets import QWebEngineView from PyQt6.QtWebChannel import QWebChannel # ========================================== # 1. Python 後端 API # ========================================== class CalendarAPI(QObject): def __init__(self, window): super().__init__() self.window = window self.conn = sqlite3.connect("widget_tasks.db", check_same_thread=False) self.cursor = self.conn.cursor() # 建立 待辦事項 (Tasks) 資料表 self.cursor.execute(""" CREATE TABLE IF NOT EXISTS tasks ( id INTEGER PRIMARY KEY AUTOINCREMENT, date TEXT NOT NULL, content TEXT NOT NULL, color TEXT NOT NULL DEFAULT '#00f0ff', category TEXT NOT NULL DEFAULT '工作', is_done INTEGER NOT NULL DEFAULT 0 ) """) # 建立 節日與行程 (Events) 資料表 self.cursor.execute(""" CREATE TABLE IF NOT EXISTS events ( id INTEGER PRIMARY KEY AUTOINCREMENT, event_date TEXT NOT NULL, content TEXT NOT NULL, color TEXT NOT NULL DEFAULT '#ff007f', category TEXT NOT NULL DEFAULT '節日' ) """) # 向後相容舊資料庫檢查 self.cursor.execute("PRAGMA table_info(tasks)") columns = [col[1] for col in self.cursor.fetchall()] if 'is_done' not in columns: self.cursor.execute("ALTER TABLE tasks ADD COLUMN is_done INTEGER NOT NULL DEFAULT 0") self.conn.commit() @pyqtSlot() def start_drag(self): if self.window.windowHandle(): self.window.windowHandle().startSystemMove() @pyqtSlot() def finish_minimize(self): curr_geom = self.window.geometry() self.window.setGeometry(curr_geom.x(), curr_geom.y(), 110, 110) @pyqtSlot() def prepare_expand(self): screen = self.window.screen().availableGeometry() curr_geom = self.window.geometry() exp_w, exp_h = 550, 1000 new_x = min(curr_geom.x(), screen.x() + screen.width() - exp_w) new_y = min(curr_geom.y(), screen.y() + screen.height() - exp_h) new_x = max(screen.x(), new_x) new_y = max(screen.y(), new_y) self.window.setGeometry(int(new_x), int(new_y), exp_w, exp_h) @pyqtSlot() def close_window(self): QApplication.quit() # --- 待辦事項 API --- @pyqtSlot(result=str) def get_all_tasks_json(self): """讀取所有待辦事項(依日期時間順序排列)""" self.cursor.execute( "SELECT id, date, content, color, category, is_done FROM tasks ORDER BY date ASC, id DESC" ) rows = self.cursor.fetchall() tasks = [ {"id": r[0], "date": r[1], "content": r[2], "color": r[3], "category": r[4], "is_done": bool(r[5])} for r in rows ] return json.dumps(tasks) @pyqtSlot(str, str, str, str, result=bool) def add_task(self, date_str, content, color, category): if not content.strip(): return False self.cursor.execute( "INSERT INTO tasks (date, content, color, category, is_done) VALUES (?, ?, ?, ?, 0)", (date_str, content, color, category) ) self.conn.commit() return True @pyqtSlot(int, result=bool) def toggle_task_done(self, task_id): self.cursor.execute("UPDATE tasks SET is_done = CASE WHEN is_done = 1 THEN 0 ELSE 1 END WHERE id = ?", (task_id,)) self.conn.commit() return True @pyqtSlot(int, result=bool) def delete_task(self, task_id): self.cursor.execute("DELETE FROM tasks WHERE id = ?", (task_id,)) self.conn.commit() return True # --- 節日與行程 API --- @pyqtSlot(result=str) def get_all_events_json(self): """讀取所有節日與行程事件(依日期升冪排序)""" self.cursor.execute( "SELECT id, event_date, content, color, category FROM events ORDER BY event_date ASC, id DESC" ) rows = self.cursor.fetchall() events = [ {"id": r[0], "event_date": r[1], "content": r[2], "color": r[3], "category": r[4]} for r in rows ] return json.dumps(events) @pyqtSlot(str, str, str, str, result=bool) def add_event(self, event_date, content, color, category): if not content.strip() or not event_date: return False self.cursor.execute( "INSERT INTO events (event_date, content, color, category) VALUES (?, ?, ?, ?)", (event_date, content, color, category) ) self.conn.commit() return True @pyqtSlot(int, result=bool) def delete_event(self, event_id): self.cursor.execute("DELETE FROM events WHERE id = ?", (event_id,)) self.conn.commit() return True # --- 日曆圓點與小球跑馬燈 --- @pyqtSlot(str, result=str) def get_month_dots_json(self, year_month): result = {} # 載入待辦事項點點 self.cursor.execute("SELECT date, color FROM tasks WHERE date LIKE ?", (f"{year_month}%",)) for date, color in self.cursor.fetchall(): if date not in result: result[date] = [] if color not in result[date]: result[date].append(color) # 載入節日行程點點 self.cursor.execute("SELECT event_date, color FROM events WHERE event_date LIKE ?", (f"{year_month}%",)) for date, color in self.cursor.fetchall(): if date not in result: result[date] = [] if color not in result[date]: result[date].append(color) return json.dumps(result) @pyqtSlot(result=str) def get_upcoming_task_json(self): today_str = datetime.date.today().strftime("%Y-%m-%d") # 優先找近期未完成待辦 self.cursor.execute( "SELECT date, content FROM tasks WHERE date >= ? AND is_done = 0 ORDER BY date ASC, id ASC LIMIT 1", (today_str,) ) row = self.cursor.fetchone() # 次之找最近的節日行程 if not row: self.cursor.execute( "SELECT event_date, content FROM events WHERE event_date >= ? ORDER BY event_date ASC, id ASC LIMIT 1", (today_str,) ) row = self.cursor.fetchone() if row: d_parts = row[0].split('-') short_date = f"{int(d_parts[1])}/{int(d_parts[2])}" return json.dumps({"found": True, "date": short_date, "content": row[1]}) return json.dumps({"found": False, "content": "無待辦事項"}) # ========================================== # 2. 前端 HTML / CSS / JS # ========================================== HTML_CONTENT = """
📅 桌面行事曆
📌 待辦事項
📅 節日與行程
⚙️ 小工具設定 ✕
選擇背景風格:
🌑 深邃黑曜石 (預設)
🌌 賽博霓虹
🌌 極光森林
""" # ========================================== # 3. Qt6 主視窗與啟動 # ========================================== class GlassWidgetWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowFlags(Qt.WindowType.FramelessWindowHint | Qt.WindowType.WindowStaysOnTopHint) self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground) self.resize(550, 1000) self.web = QWebEngineView(self) self.web.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground) self.web.setStyleSheet("background: transparent;") self.web.page().setBackgroundColor(Qt.GlobalColor.transparent) self.setCentralWidget(self.web) self.channel = QWebChannel() self.api = CalendarAPI(self) self.channel.registerObject("pyapi", self.api) self.web.page().setWebChannel(self.channel) self.web.setHtml(HTML_CONTENT, QUrl("file:///")) if __name__ == "__main__": app = QApplication(sys.argv) window = GlassWidgetWindow() window.show() sys.exit(app.exec())