Created
March 16, 2025 10:07
-
-
Save monperrus/8066244330ac01308a6695aa18aedf50 to your computer and use it in GitHub Desktop.
a python systray application that adds a list of items in system tray icon
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
| #!/usr/bin/python | |
| # a systray app for my notes (tomboy + joplin) | |
| import pystray | |
| from PIL import Image | |
| import tkinter as tk | |
| from tkinter import simpledialog | |
| import os | |
| import joplin_notes | |
| import tomboy | |
| class SystemTrayApp: | |
| def __init__(self): | |
| self.icon = None | |
| self.create_icon() | |
| def create_icon(self): | |
| # load image tomboy-ng.png for the system tray icon | |
| image = Image.open("tomboy-ng.png") | |
| # image = Image.new('RGB', (64, 64), color='blue') | |
| # Create the system tray icon | |
| self.icon = pystray.Icon( | |
| "list_app", | |
| image, | |
| "My List App", | |
| self.create_menu() | |
| ) | |
| def notes(self): | |
| l = tomboy.list_tomboy_notes_sorted_by_time() + joplin_notes.list_joplin_notes() | |
| # sort by updated_time | |
| l.sort(key=lambda x: x["updated_time"], reverse=True) | |
| return l | |
| def items(): | |
| return [x["title"] for x in self.notes()] | |
| def create_menu(self): | |
| menu_items = [] | |
| # Add the main actions | |
| # menu_items.append(pystray.MenuItem("Add Item", self.add_item)) | |
| # Add a separator if there are items | |
| menu_items.append(pystray.MenuItem("Items:", None, enabled=False)) | |
| # Add all items in the list | |
| for i, item in enumerate(self.notes()): | |
| id=item["id"] | |
| open_command=item["open_command"] | |
| f=eval(f"lambda: os.system('{open_command}&')") | |
| # print(item) | |
| menu_items.append( | |
| pystray.MenuItem( | |
| f"{item["title"]+" ("+item["type"]+")"}", | |
| f, | |
| # partial(self.remove_item, i), # Makes items clickable to remove them | |
| enabled=True | |
| ) | |
| ) | |
| # Add a separator | |
| menu_items.append(pystray.MenuItem("-", None)) | |
| # Add the remaining actions | |
| menu_items.extend([ | |
| pystray.MenuItem("Clear All", self.clear_items), | |
| # pystray.MenuItem("Exit", self.icon.stop) | |
| ]) | |
| return pystray.Menu(*menu_items) | |
| def add_item(self): | |
| # Create a temporary root window | |
| root = tk.Tk() | |
| root.withdraw() # Hide the root window | |
| # Show input dialog | |
| new_item = simpledialog.askstring("Add Item", "Enter new item:") | |
| if new_item: | |
| print(new_item) | |
| raise Exception("implement adding a note") | |
| # Update the icon's menu | |
| self.icon.menu = self.create_menu() | |
| root.destroy() | |
| def show_items(self): | |
| # Create a temporary root window | |
| root = tk.Tk() | |
| root.withdraw() | |
| message = "Items:\n" + "\n".join(f"{i+1}. {item}" for i, item in enumerate(self.items())) | |
| # Show message box with items | |
| tk.messagebox.showinfo("Items List", message) | |
| root.destroy() | |
| def clear_items(self): | |
| raise Exception("not supported") | |
| def run(self): | |
| self.icon.run() | |
| if __name__ == "__main__": | |
| app = SystemTrayApp() | |
| app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment