{"id":19938,"date":"2025-05-03T21:23:09","date_gmt":"2025-05-03T13:23:09","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=19938"},"modified":"2025-05-04T09:59:26","modified_gmt":"2025-05-04T01:59:26","slug":"python-%e4%b8%ad%e4%bd%bf%e7%94%a8-tkinter-%e5%92%8c-pystray-%e5%ae%9e%e7%8e%b0%e6%9c%89%e6%89%98%e7%9b%98%e5%9b%be%e6%a0%87%e7%9a%84%e5%9b%be%e5%bd%a2%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=19938","title":{"rendered":"Python \u4e2d\u4f7f\u7528 Tkinter \u548c Pystray \u5b9e\u73b0\u5177\u6709\u6258\u76d8\u56fe\u6807\u7684\u56fe\u5f62\u7a0b\u5e8f"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title ez-toc-toggle\" style=\"cursor:pointer\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.iyatt.com\/?p=19938\/#%E6%B5%8B%E8%AF%95%E7%8E%AF%E5%A2%83\" >\u6d4b\u8bd5\u73af\u5883<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.iyatt.com\/?p=19938\/#%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81\" >\u793a\u4f8b\u4ee3\u7801<\/a><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E6%B5%8B%E8%AF%95%E7%8E%AF%E5%A2%83\"><\/span>\u6d4b\u8bd5\u73af\u5883<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<ul>\n<li>Windows 11 24H2<\/li>\n<li>Python 3.13.1<\/li>\n<\/ul>\n<p>Python \u5305\uff1a<\/p>\n<ul>\n<li>pystray==0.19.5<\/li>\n<li>pillow==11.2.1<\/li>\n<\/ul>\n<h1><span class=\"ez-toc-section\" id=\"%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81\"><\/span>\u793a\u4f8b\u4ee3\u7801<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Python<\/p>\n<pre><code class=\"language-python\">import tkinter\nfrom PIL import Image\nimport pystray\nfrom threading import Thread\n\niconPath = &#039;.\/icon.ico&#039; # \u56fe\u6807\u6587\u4ef6\u8def\u5f84\u3002\u8fd9\u91cc\u7a97\u53e3\u6807\u9898\u56fe\u6807\u548c\u6258\u76d8\u56fe\u6807\u90fd\u4f7f\u7528\u5b83\n\nclass MainUI(tkinter.Frame):\n    def __init__(self, root: tkinter.Tk=None):\n        super().__init__(root)\n        self.root = root\n        root.protocol(&#039;WM_DELETE_WINDOW&#039;, self.onClosing) # \u7ed1\u5b9a\u5173\u95ed\u7a97\u53e3\u4e8b\u4ef6\n        self.root.withdraw() # \u9690\u85cf\u4e3b\u7a97\u53e3\n        self.pack()\n\n    def createWindow(self):\n        &quot;&quot;&quot;\u521b\u5efa\u4e3b\u754c\u9762\u5185\u5bb9&quot;&quot;&quot;\n        tkinter.Label(self, text=&quot;Hello World!&quot;).pack()\n\n    def onClosing(self):\n        &quot;&quot;&quot;\u5173\u95ed\u7a97\u53e3\u65f6\u89e6\u53d1&quot;&quot;&quot;\n        self.root.withdraw()  # \u9690\u85cf\u4e3b\u7a97\u53e3\n\nclass SystemTray(Thread):\n    def __init__(self, root: tkinter.Tk):\n        super().__init__(daemon=True) # \u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\n        self.root = root\n\n    def showWindows(self):\n        &quot;&quot;&quot;\u663e\u793a\u4e3b\u7a97\u53e3&quot;&quot;&quot;\n        self.root.after(0, self.root.deiconify)  # \u5728\u4e3b\u7ebf\u7a0b\u4e2d\u663e\u793a\u4e3b\u7a97\u53e3\n\n    def exitApp(self):\n        &quot;&quot;&quot;\u9000\u51fa\u7a0b\u5e8f&quot;&quot;&quot;\n        self.icon.stop() # \u505c\u6b62\u6258\u76d8\u56fe\u6807\n        self.root.after(0, self.root.destroy)  # \u5728\u4e3b\u7ebf\u7a0b\u4e2d\u9500\u6bc1\u4e3b\u7a97\u53e3\n\n    def run(self):\n        &quot;&quot;&quot;\u542f\u52a8\u6258\u76d8\u56fe\u6807&quot;&quot;&quot;\n        # \u521b\u5efa\u83dc\u5355\u9879\n        menu = pystray.Menu(\n            pystray.MenuItem(&#039;\u663e\u793a\u7a97\u53e3&#039;, self.showWindows, default=True), # \u9ed8\u8ba4\u5355\u673a\u6258\u76d8\u56fe\u6807\u6267\u884c\u663e\u793a\u64cd\u4f5c\n            pystray.MenuItem(&#039;\u9000\u51fa\u7a0b\u5e8f&#039;, self.exitApp),\n        )\n\n        # \u52a0\u8f7d\u56fe\u6807\n        img = Image.open(iconPath)\n        # \u521b\u5efa\u6258\u76d8\u56fe\u6807\n        self.icon = pystray.Icon(&#039;\u7528\u4e8e\u7cfb\u7edf\u8bc6\u522b\u56fe\u6807\u7684\u540d\u79f0&#039;, img, &#039;\u7b80\u77ed\u7684\u540d\u79f0&#039;, menu)\n        # \u542f\u52a8\n        self.icon.run()\n\ndef main():\n    root = tkinter.Tk() # \u521b\u5efa\u4e3b\u7a97\u53e3\n    root.title(&#039;Tkinter \u548c Pystray \u7ed3\u5408\u793a\u4f8b&#039;) # \u8bbe\u7f6e\u7a97\u53e3\u6807\u9898\n    root.geometry(&#039;500x200&#039;)  # \u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f\n    root.iconbitmap(iconPath) # \u8bbe\u7f6e\u7a97\u53e3\u56fe\u6807\n    mainUI = MainUI(root) # \u521b\u5efa\u4e3b\u754c\u9762\n    mainUI.createWindow()\n    stary = SystemTray(root) # \u521b\u5efa\u7cfb\u7edf\u6258\u76d8\n    stary.start() # \u542f\u52a8\u7cfb\u7edf\u6258\u76d8\n    root.mainloop() # \u542f\u52a8\u4e3b\u7a97\u53e3\n\nif __name__ == &quot;__main__&quot;:\n    main()<\/code><\/pre>\n<p>\u7a0b\u5e8f\u8fd0\u884c\u540e\u9ed8\u8ba4\u9690\u85cf\u7a97\u53e3\uff0c\u53ef\u4ee5\u770b\u5230\u6258\u76d8\u56fe\u6807<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2025\/05\/image-1746278357433.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 89px; --smush-placeholder-aspect-ratio: 89\/105;\" \/><\/p>\n<p>\u5355\u51fb\u6258\u76d8\u56fe\u6807\u53ef\u4ee5\u663e\u793a\u7a97\u53e3<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2025\/05\/image-1746278443607.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 552px; --smush-placeholder-aspect-ratio: 552\/297;\" \/><\/p>\n<p>\u70b9\u51fb\u7a97\u53e3\u7684\u5173\u95ed\uff0c\u7531\u4e8e\u5df2\u7ecf\u628a\u5173\u95ed\u64cd\u4f5c\u7ed1\u5b9a\u4e3a\u9690\u85cf\u7a97\u53e3\uff0c\u5b9e\u9645\u4e0d\u4f1a\u5173\u95ed\u7a97\u53e3\uff0c\u53ea\u662f\u770b\u4e0d\u5230\u4e86\uff0c\u8fd8\u662f\u53ef\u4ee5\u901a\u8fc7\u6258\u76d8\u56fe\u6807\u6253\u5f00\u3002<br \/>\n\u5728\u6258\u76d8\u56fe\u6807\u4e0a\u53f3\u952e\u6709\u4e24\u4e2a\u83dc\u5355\u9879\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd9\u91cc\u7684\u9000\u51fa\u7a0b\u5e8f\u6267\u884c\u9000\u51fa<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2025\/05\/image-1746278520104.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 175px; --smush-placeholder-aspect-ratio: 175\/92;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6d4b\u8bd5\u73af\u5883 Windows 11 24H2 Python 3.13.1 Python \u5305\uff1a pystray==0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"zakra_page_container_layout":"customizer","zakra_page_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":"","zakra_menu_item_active_color":"","zakra_menu_active_style":"","zakra_page_header":true,"_lmt_disableupdate":"no","_lmt_disable":"no","footnotes":""},"categories":[1,592],"tags":[],"class_list":["post-19938","post","type-post","status-publish","format-standard","hentry","category-all","category-python"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/19938","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=19938"}],"version-history":[{"count":5,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/19938\/revisions"}],"predecessor-version":[{"id":19956,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/19938\/revisions\/19956"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=19938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=19938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=19938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}