{"id":6881,"date":"2023-11-23T08:48:47","date_gmt":"2023-11-23T08:48:47","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=6881"},"modified":"2023-11-23T09:21:21","modified_gmt":"2023-11-23T09:21:21","slug":"tkinter-system-tray","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-system-tray\/","title":{"rendered":"Tkinter System Tray"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to develop a Tkinter system tray application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-tkinter-system-tray-applications'>Introduction to Tkinter system tray applications <a href=\"#introduction-to-tkinter-system-tray-applications\" class=\"anchor\" id=\"introduction-to-tkinter-system-tray-applications\" title=\"Anchor for Introduction to Tkinter system tray applications\">#<\/a><\/h2>\n\n\n\n<p>A system tray application is a type of application that runs in the background and provides quick access to some functions.<\/p>\n\n\n\n<p>The system tray application typically appears as a small icon in the system tray or notification areas in the taskbar of the Windows, macOS, or Linux Desktop environment.<\/p>\n\n\n\n<p>Some common examples of system tray applications are antivirus software, instant messaging clients, or system monitoring software.<\/p>\n\n\n\n<p>Users can interact with the system tray application by clicking on it to access a menu and perform specific actions.<\/p>\n\n\n\n<p>Tkinter lacks built-in support for the system tray application, but you can use the <code>PyStray<\/code> third-party library to create it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='tkinter-system-tray-application-example'>Tkinter System Tray Application example <a href=\"#tkinter-system-tray-application-example\" class=\"anchor\" id=\"tkinter-system-tray-application-example\" title=\"Anchor for Tkinter System Tray Application example\">#<\/a><\/h2>\n\n\n\n<p>Creating a system tray application using the Tkinter and <code>PyStray<\/code> package involves the following steps:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='step-1-install-the-pystray-and-pillow-libraries'>Step 1. Install the Pystray and Pillow libraries <a href=\"#step-1-install-the-pystray-and-pillow-libraries\" class=\"anchor\" id=\"step-1-install-the-pystray-and-pillow-libraries\" title=\"Anchor for Step 1. Install the Pystray and Pillow libraries\">#<\/a><\/h3>\n\n\n\n<p>Install the Pystray and Pillow libraries using the <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-pip\/\">pip<\/a> command. The Pystray enables you to create a tray application, and the Pillow helps you design an icon placed in the system tray.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">pip install pystray pillow<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='step-2-create-a-tkinter-system-tray-application'>Step 2. Create a Tkinter system tray application <a href=\"#step-2-create-a-tkinter-system-tray-application\" class=\"anchor\" id=\"step-2-create-a-tkinter-system-tray-application\" title=\"Anchor for Step 2. Create a Tkinter system tray application\">#<\/a><\/h3>\n\n\n\n<p>Create a Python script and use the following code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n<span class=\"hljs-keyword\">import<\/span> pystray\n<span class=\"hljs-keyword\">from<\/span> PIL <span class=\"hljs-keyword\">import<\/span> Image\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyApp<\/span><span class=\"hljs-params\">(tk.Tk)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        super().__init__()\n\n        self.title(<span class=\"hljs-string\">\"System Tray App\"<\/span>)\n        self.geometry(<span class=\"hljs-string\">'500x250'<\/span>)\n        self.protocol(<span class=\"hljs-string\">'WM_DELETE_WINDOW'<\/span>, self.minimize_to_tray)\n    \n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">minimize_to_tray<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.withdraw()\n        image = Image.open(<span class=\"hljs-string\">\"app.ico\"<\/span>)\n        menu = (pystray.MenuItem(<span class=\"hljs-string\">'Quit'<\/span>,  self.quit_window), \n                pystray.MenuItem(<span class=\"hljs-string\">'Show'<\/span>,self.show_window))\n        icon = pystray.Icon(<span class=\"hljs-string\">\"name\"<\/span>, image, <span class=\"hljs-string\">\"My App\"<\/span>, menu)\n        icon.run()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">quit_window<\/span><span class=\"hljs-params\">(self, icon)<\/span>:<\/span>\n        icon.stop()\n        self.destroy()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">show_window<\/span><span class=\"hljs-params\">(self, icon)<\/span>:<\/span>\n        icon.stop()\n        self.after(<span class=\"hljs-number\">0<\/span>,self.deiconify)\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">\"__main__\"<\/span>:\n    app = MyApp()\n    app.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"197\" height=\"113\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2023\/11\/tkinter-system-tray-icon.png\" alt=\"Tkinter System Tray Icon\" class=\"wp-image-6883\"\/><\/figure>\n\n\n\n<p>The <code>app.ico<\/code> can be downloaded here:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"60\" height=\"60\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2023\/11\/app.ico\" alt=\"\" class=\"wp-image-6886\"\/><\/figure>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, import the necessary libraries:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n<span class=\"hljs-keyword\">import<\/span> pystray\n<span class=\"hljs-keyword\">from<\/span> PIL <span class=\"hljs-keyword\">import<\/span> Image<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, define a new <code>MyApp<\/code> class that extends the <code>tk.Tk<\/code> class. In the constructor <code>__init__()<\/code>, bind the close event with the method <code>minimize_to_tray()<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.protocol(<span class=\"hljs-string\">'WM_DELETE_WINDOW'<\/span>, self.minimize_to_tray)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you click the close button, the <code>minimize_to_tray()<\/code> method will execute creating a system tray icon.<\/p>\n\n\n\n<p>Third, define the <code>minimize_to_tray()<\/code> method that minimizes the main window to the system tray:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">minimize_to_tray<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n    self.withdraw()\n    image = Image.open(<span class=\"hljs-string\">\"app.ico\"<\/span>)\n    menu = (pystray.MenuItem(<span class=\"hljs-string\">'Quit'<\/span>,  self.quit_window), \n            pystray.MenuItem(<span class=\"hljs-string\">'Show'<\/span>,self.show_window))\n    icon = pystray.Icon(<span class=\"hljs-string\">\"name\"<\/span>, image, <span class=\"hljs-string\">\"My App\"<\/span>, menu)\n    icon.run()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the method:<\/p>\n\n\n\n<p>The <code>self.withdraw()<\/code> hides the main window without destroying it. This is the necessary step for minimizing the main window to the system tray:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">minimize_to_tray<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n    <span class=\"hljs-comment\"># Hide the main window<\/span>\n    self.withdraw()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following calls the <code>open()<\/code> method of the Image class to load the <code>app.ico<\/code> that will be used as the icon for the system tray:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">    <span class=\"hljs-comment\"># Open the image for the system tray icon<\/span>\n    image = Image.open(<span class=\"hljs-string\">\"app.ico\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following defines a menu for the system tray icon, which contains two <code>PyStray<\/code> <code>MenuItem<\/code> objects:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">    menu = (pystray.MenuItem(<span class=\"hljs-string\">'Quit'<\/span>, self.quit_window), \n            pystray.MenuItem(<span class=\"hljs-string\">'Show'<\/span>, self.show_window))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The menu items <code>Quit<\/code> and <code>Show<\/code> are associated with the <code>self.quit_window<\/code> and <code>self.show_window<\/code> methods respectively.<\/p>\n\n\n\n<p>The following line creates a <code>PyStray<\/code> icon with the specified image and menu:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">    icon = pystray.Icon(<span class=\"hljs-string\">\"name\"<\/span>, image, <span class=\"hljs-string\">\"My App\"<\/span>, menu)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following calls the <code>run()<\/code> method of the <code>Icon<\/code> object to make the icon active in the system tray:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">    icon.run()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>icon.run<\/code>() line starts the <code>PyStray<\/code> event loop, making the icon active in the system tray. It will now respond to user interactions, such as clicks on the menu items.<\/p>\n\n\n\n<p>Fourth, define the <code>quit_window()<\/code> associated with the <code>Quit<\/code> menu item in the system tray. When you select the <code>Quit<\/code> option from the system tray icon menu, it stops the <code>PyStray<\/code> event loop (<code>icon.stop<\/code>()) and destroys the main window, ensuring a clean exit of the application (<code>self.destroy<\/code>())<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">quit_window<\/span><span class=\"hljs-params\">(self, icon)<\/span>:<\/span>\n        icon.stop()\n        self.destroy()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fifth, define the <code>show_window()<\/code> method associated with the <code>Show<\/code> menu item in the system tray  icon: <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">show_window<\/span><span class=\"hljs-params\">(self, icon)<\/span>:<\/span>\n        icon.stop()\n        self.after(<span class=\"hljs-number\">0<\/span>,self.deiconify)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you select the Show option from the system tray icon menu it closes the system tray icon (stops the <code>PyStray<\/code> event loop) and uses the <code><a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-after\/\">after()<\/a><\/code> method to schedule the <code>deiconify()<\/code> method to be called immediately, resulting in the main application being restored and becoming visible again.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>PyStray<\/code> library to create a Tkinter system tray application.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"6881\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-system-tray\/\"\n\t\t\t\tdata-post-title=\"Tkinter System Tray\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"6881\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-system-tray\/\"\n\t\t\t\tdata-post-title=\"Tkinter System Tray\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to develop a Tkinter system tray application using the pstray library.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":61,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6881","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/6881","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=6881"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/6881\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1232"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=6881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}