{"id":243,"date":"2025-02-05T14:59:52","date_gmt":"2025-02-05T06:59:52","guid":{"rendered":"https:\/\/blog.keruone.cn\/?p=243"},"modified":"2025-02-05T14:59:52","modified_gmt":"2025-02-05T06:59:52","slug":"python-to-exe","status":"publish","type":"post","link":"https:\/\/blog.keruone.cn\/index.php\/2025\/02\/05\/python-to-exe\/","title":{"rendered":"python \u7a0b\u5e8f\u6253\u5305\u4e3a exe"},"content":{"rendered":"<h1>Python\u7a0b\u5e8f\u6253\u5305\u4e3aEXE<\/h1>\n\n<h2>1. \u7b80\u4ecb<\/h2>\n<p>\u5728Python\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5c06Python\u7a0b\u5e8f\u6253\u5305\u6210Windows\u53ef\u6267\u884c\u6587\u4ef6\uff08.exe\uff09\uff0c\u4ee5\u4fbf\u5206\u53d1\u7ed9\u4e0d\u5177\u5907Python\u73af\u5883\u7684\u7528\u6237\u3002\u672c\u6587\u5c06\u901a\u8fc7\u4e00\u4e2a\u5b9e\u9645\u7684\u6848\u4f8b\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PyInstaller\u5c06Python\u7a0b\u5e8f\u6253\u5305\u6210exe\u6587\u4ef6\u3002<\/p>\n<h2>2. \u793a\u4f8b\u7a0b\u5e8f<\/h2>\n<p>\u6211\u4eec\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684GUI\u7a0b\u5e8f\u4f5c\u4e3a\u793a\u4f8b\uff0c\u8fd9\u662f\u4e00\u4e2a\u57fa\u4e8etkinter\u7684\u6587\u672c\u7f16\u8f91\u5668\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">import tkinter as tk\nfrom tkinter import ttk\n\nclass SimpleEditor(tk.Tk):\n    def __init__(self):\n        super().__init__()\n\n        self.title(\"test\")\n        self.geometry(\"800x600\")\n\n        # \u521b\u5efa\u6587\u672c\u7f16\u8f91\u533a\n        self.editor = tk.Text(self, wrap=tk.WORD)\n        self.editor.pack(expand=True, fill='both')\n\n        # \u521b\u5efa\u6eda\u52a8\u6761\n        scrollbar = ttk.Scrollbar(self.editor)\n        scrollbar.pack(side=tk.RIGHT, fill=tk.Y)\n\n        # \u914d\u7f6e\u6eda\u52a8\u6761\n        self.editor.config(yscrollcommand=scrollbar.set)\n        scrollbar.config(command=self.editor.yview)\n\nif __name__ == \"__main__\":\n    app = SimpleEditor()\n    app.mainloop()\n<\/code><\/pre>\n<h2>3. \u6253\u5305\u51c6\u5907\u5de5\u4f5c<\/h2>\n<h3>3.1 \u5b89\u88c5\u5fc5\u8981\u5de5\u5177<\/h3>\n<p>\u9996\u5148\u9700\u8981\u5b89\u88c5PyInstaller\uff1a<\/p>\n<pre><code class=\"language-bash line-numbers\">pip install pyinstaller\n<\/code><\/pre>\n<h3>3.2 \u51c6\u5907\u8d44\u6e90\u6587\u4ef6<\/h3>\n<p>\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u9700\u8981\u56fe\u6807\u6216\u5176\u4ed6\u8d44\u6e90\u6587\u4ef6\uff0c\u8bf7\u786e\u4fdd\u5b83\u4eec\u653e\u5728\u6b63\u786e\u7684\u4f4d\u7f6e\u3002\u4f8b\u5982\uff1a<br \/>\n&#8211; \u7a0b\u5e8f\u56fe\u6807\uff1a<code>icon.ico<\/code><br \/>\n&#8211; \u5176\u4ed6\u8d44\u6e90\u6587\u4ef6\uff1a\u653e\u5728<code>resources<\/code>\u6587\u4ef6\u5939<\/p>\n<blockquote><p>\n  \u672c\u8d28\u4e0a\u90fd\u662f\u63d0\u4f9b\u8def\u5f84\n<\/p><\/blockquote>\n<h2>4. \u6253\u5305\u6b65\u9aa4<\/h2>\n<h3>4.1 \u57fa\u672c\u6253\u5305\u547d\u4ee4<\/h3>\n<p>\u6700\u7b80\u5355\u7684\u6253\u5305\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-bash line-numbers\">pyinstaller --name \"test\" --onefile --noconsole --icon=icon.ico main.py\n<\/code><\/pre>\n<p>\u63d0\u53ca\u7684\u53c2\u6570\uff1a<br \/>\n&#8211; <code>--name<\/code>\uff1a\u6307\u5b9a\u6253\u5305\u540e\u7684exe\u6587\u4ef6\u540d<br \/>\n&#8211; <code>--onefile<\/code>\uff1a\u5c06\u6240\u6709\u4f9d\u8d56\u6253\u5305\u6210\u4e00\u4e2a\u5355\u72ec\u7684exe\u6587\u4ef6<br \/>\n&#8211; <code>--noconsole<\/code>\uff1a\u4e0d\u663e\u793a\u63a7\u5236\u53f0\u7a97\u53e3<br \/>\n&#8211; <code>--icon<\/code>\uff1a\u6307\u5b9a\u7a0b\u5e8f\u56fe\u6807<br \/>\n&#8211; <code>main.py<\/code>\uff1a\u8981\u6253\u5305\u7684Python\u811a\u672c<\/p>\n<p>\u672a\u63d0\u53ca\u7684\u53c2\u6570\uff1a<br \/>\n&#8211; <code>--hidden-import<\/code>\uff1a\u6307\u5b9a\u9700\u8981\u9690\u85cf\u5bfc\u5165\u7684\u6a21\u5757<br \/>\n&#8211; <code>--exclude-module<\/code>\uff1a\u6307\u5b9a\u9700\u8981\u6392\u9664\u7684\u6a21\u5757<br \/>\n&#8211; <code>--add-data<\/code>\uff1a\u6dfb\u52a0\u989d\u5916\u7684\u6570\u636e\u6587\u4ef6<br \/>\n&#8211; <code>--upx-dir<\/code>\uff1a\u6307\u5b9aUPX\u538b\u7f29\u5de5\u5177\u7684\u8def\u5f84<\/p>\n<h3>4.2 spec<\/h3>\n<p>\u672c\u4eba\u672a\u505a\u76f8\u5173\u4e86\u89e3\uff0c\u5f85\u8865\u5145\u3002<br \/>\n\u611f\u5174\u8da3\u7684\u81ea\u5df1\u641c\u7d22\u7f51\u7edc\u6216AI\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u7a0b\u5e8f\u6253\u5305\u4e3aEXE 1. \u7b80\u4ecb \u5728Python\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5c06Python\u7a0b\u5e8f\u6253\u5305\u6210Windo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[17],"tags":[],"class_list":["post-243","post","type-post","status-publish","format-standard","hentry","category-small-tools"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/posts\/243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/comments?post=243"}],"version-history":[{"count":1,"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/posts\/243\/revisions"}],"predecessor-version":[{"id":244,"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/posts\/243\/revisions\/244"}],"wp:attachment":[{"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/media?parent=243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/categories?post=243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/tags?post=243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}