add stop function to systray icon
authorAndrew Lorimer <andrew@lorimer.id.au>
Sat, 22 May 2021 03:59:54 +0000 (13:59 +1000)
committerAndrew Lorimer <andrew@lorimer.id.au>
Sat, 22 May 2021 03:59:54 +0000 (13:59 +1000)
ppt_control/ppt_control.py
index 5ff80efbee1277b7dfad8630091a5933565cac62..d6a06d1ee2c295bfd05b573dba4a94c782376333 100755 (executable)
@@ -469,7 +469,7 @@ def open_settings(_=None):
     global interface_root\r
     interface_root = tk.Tk()\r
     interface_root.protocol("WM_DELETE_WINDOW", on_closing)\r
     global interface_root\r
     interface_root = tk.Tk()\r
     interface_root.protocol("WM_DELETE_WINDOW", on_closing)\r
-    interface_root.iconphoto(False, tk.PhotoImage(file="static/icons/ppt.png"))\r
+    interface_root.iconphoto(False, tk.PhotoImage(file="static\icons\ppt.png"))\r
     interface_root.geometry("600x300+300+300")\r
     app = Interface(interface_root)\r
     interface_thread = threading.Thread(target=interface_root.mainloop())\r
     interface_root.geometry("600x300+300+300")\r
     app = Interface(interface_root)\r
     interface_thread = threading.Thread(target=interface_root.mainloop())\r
@@ -529,13 +529,21 @@ class Interface(ttk.Frame):
         refresh_status()\r
         \r
         \r
         refresh_status()\r
         \r
         \r
+def exit_action(icon):\r
+    logger.debug("User requested shutdown")\r
+    icon.visible = False\r
+    icon.stop()\r
 \r
 def show_icon():\r
     logger.debug("Starting system tray icon")\r
 \r
 def show_icon():\r
     logger.debug("Starting system tray icon")\r
-    menu = (pystray.MenuItem("Status", lambda: null_action(), enabled=False),\r
+    icon = pystray.Icon("ppt-control")\r
+    icon.menu = (pystray.MenuItem("Status", lambda: null_action(), enabled=False),\r
             pystray.MenuItem("Restart", lambda: start()),\r
             pystray.MenuItem("Restart", lambda: start()),\r
-            pystray.MenuItem("Settings", lambda: open_settings()))\r
-    icon = pystray.Icon("ppt-control", Image.open(os.path.dirname(os.path.realpath(__file__)) + r'''\static\icons\ppt.ico'''), "ppt-control", menu)\r
+            pystray.MenuItem("Stop", lambda: exit_action(icon)),\r
+            pystray.MenuItem("Settings", lambda: open_settings())\r
+            )\r
+    icon.icon = Image.open(os.path.dirname(os.path.realpath(__file__)) + r'''\static\icons\ppt.ico''')\r
+    icon.title = "ppt-control"\r
     icon.visible = True\r
     icon.run(setup=start)\r
 \r
     icon.visible = True\r
     icon.run(setup=start)\r
 \r
@@ -579,6 +587,7 @@ def start_interface():
 \r
     # Start systray icon and server\r
     show_icon()\r
 \r
     # Start systray icon and server\r
     show_icon()\r
+    sys.exit(0)\r
 \r
 if __name__ == "__main__":\r
     start_interface()\r
 \r
 if __name__ == "__main__":\r
     start_interface()\r