Skip to content

Instantly share code, notes, and snippets.

@davidgilbertson
Created April 21, 2025 05:36
Show Gist options
  • Save davidgilbertson/0ceee3cfee2237e881aecee6d4c86303 to your computer and use it in GitHub Desktop.
Save davidgilbertson/0ceee3cfee2237e881aecee6d4c86303 to your computer and use it in GitHub Desktop.
from pystray import Icon, Menu, MenuItem
from PIL import Image, ImageDraw
import threading
class TrayIcon:
def __init__(self, title="Recording..."):
self.title = title
self.icon = None
self._thread = None
@staticmethod
def _create_icon_image(color="red"):
size = 64
image = Image.new("RGBA", (size, size), (0, 0, 0, 0))
draw = ImageDraw.Draw(image)
draw.ellipse((0, 0, size, size), fill=color)
return image
def _run_icon(self):
self.icon = Icon("transcriber")
self.icon.icon = self._create_icon_image()
self.icon.title = self.title
self.icon.menu = Menu(MenuItem("Quit", self._quit))
self.icon.run()
def _quit(self, icon, item):
self.icon.stop()
def show(self):
if self._thread and self._thread.is_alive():
return # already running
self._thread = threading.Thread(target=self._run_icon, daemon=True)
self._thread.start()
def hide(self):
if self.icon:
self.icon.stop()
self.icon = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment