Created
August 21, 2025 15:21
-
-
Save ochilab/8c0932f6b9fb340140d9e8a1e15f6b7b to your computer and use it in GitHub Desktop.
メニューバー(タスクトレイ)常駐アプリのサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import flet as ft | |
| import threading | |
| from PIL import Image, ImageDraw | |
| import pystray | |
| import asyncio | |
| import time | |
| def main(page: ft.Page): | |
| page.title = "Flet MenuBar App" | |
| page.add(ft.Text("メニューバー常駐のサンプル")) | |
| def run_flet(): | |
| try: | |
| ft.app(target=main, port=8552, view=ft.AppView.WEB_BROWSER) | |
| except Exception as e: | |
| print(f"Flet app error: {e}") | |
| def create_icon(): | |
| # アイコンを生成 | |
| image = Image.new('RGB', (64, 64), (0, 0, 0)) | |
| draw = ImageDraw.Draw(image) | |
| draw.rectangle((16, 16, 48, 48), fill=(255, 255, 0)) | |
| def on_quit(icon, item): | |
| print("アプリケーションを終了します...") | |
| icon.stop() | |
| import os | |
| os._exit(0) | |
| icon = pystray.Icon("flet_app", image, "Flet App", menu=pystray.Menu( | |
| pystray.MenuItem("Quit", on_quit) | |
| )) | |
| icon.run() | |
| if __name__ == "__main__": | |
| # フレットアプリをスレッドで開始 | |
| flet_thread = threading.Thread(target=run_flet, daemon=True) | |
| flet_thread.start() | |
| # 少し待ってからシステムトレイを開始 | |
| time.sleep(2) | |
| create_icon() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment