{"id":1151838,"date":"2025-01-13T17:18:22","date_gmt":"2025-01-13T09:18:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1151838.html"},"modified":"2025-01-13T17:18:25","modified_gmt":"2025-01-13T09:18:25","slug":"%e5%a6%82%e4%bd%95python%e6%89%93%e5%8c%85%e6%88%90pyw","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1151838.html","title":{"rendered":"\u5982\u4f55python\u6253\u5305\u6210pyw"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25181957\/a97ddc6f-0b79-4751-9c18-9ba7f3ba1652.webp\" alt=\"\u5982\u4f55python\u6253\u5305\u6210pyw\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5c06\u811a\u672c\u6253\u5305\u6210pyw\u6587\u4ef6\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efaPython\u811a\u672c\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5de5\u5177\u3001\u4fee\u6539\u6587\u4ef6\u540e\u7f00\u3001\u914d\u7f6e\u65e0\u63a7\u5236\u53f0\u8f93\u51fa\u3001\u4f7f\u7528pyinstaller\u6253\u5305\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u7684\u4e00\u70b9\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u201c\u914d\u7f6e\u65e0\u63a7\u5236\u53f0\u8f93\u51fa\u201d\uff1a<\/p>\n<p><strong>\u914d\u7f6e\u65e0\u63a7\u5236\u53f0\u8f93\u51fa\u662f\u786e\u4fdd\u6253\u5305\u540e\u7684\u5e94\u7528\u7a0b\u5e8f\u5728\u6267\u884c\u65f6\u4e0d\u4f1a\u5f39\u51fa\u547d\u4ee4\u884c\u7a97\u53e3\uff0c\u4ee5\u4fbf\u7ed9\u7528\u6237\u66f4\u597d\u7684\u4f53\u9a8c\u3002<\/strong> \u5177\u4f53\u64cd\u4f5c\u662f\u5c06\u6587\u4ef6\u7684\u540e\u7f00\u4ece.py\u6539\u4e3a.pyw\u3002\u8fd9\u6837\u5728Windows\u7cfb\u7edf\u4e0b\u6267\u884c\u8be5\u6587\u4ef6\u65f6\uff0cPython\u89e3\u91ca\u5668\u5c06\u4e0d\u4f1a\u5f39\u51fa\u63a7\u5236\u53f0\u7a97\u53e3\u3002\u8fd9\u4e2a\u6b65\u9aa4\u975e\u5e38\u91cd\u8981\uff0c\u5c24\u5176\u662f\u5728\u5f00\u53d1GUI\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u80fd\u591f\u4f7f\u5e94\u7528\u7a0b\u5e8f\u770b\u8d77\u6765\u66f4\u52a0\u4e13\u4e1a\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u521b\u5efaPython\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u6253\u5305\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2aPython\u811a\u672c\u3002\u786e\u4fdd\u4f60\u7684Python\u811a\u672c\u8fd0\u884c\u6b63\u5e38\u5e76\u4e14\u529f\u80fd\u5b8c\u5584\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8bPython\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def say_hello():<\/p>\n<p>    print(&quot;Hello, World!&quot;)<\/p>\n<p>root = tk.Tk()<\/p>\n<p>button = tk.Button(root, text=&quot;Say Hello&quot;, command=say_hello)<\/p>\n<p>button.pack()<\/p>\n<p>root.m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u811a\u672c\u7b80\u5355\u5730\u521b\u5efa\u4e86\u4e00\u4e2aTkinter\u7a97\u53e3\uff0c\u5305\u542b\u4e00\u4e2a\u6309\u94ae\uff0c\u70b9\u51fb\u6309\u94ae\u540e\u5c06\u5728\u63a7\u5236\u53f0\u6253\u5370\u201cHello, World!\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u8981\u5c06Python\u811a\u672c\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u5de5\u5177\uff0c\u4f8b\u5982PyInstaller\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5PyInstaller\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyinstaller<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>PyInstaller\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4fbf\u4e8e\u53d1\u5e03\u548c\u5206\u53d1\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4fee\u6539\u6587\u4ef6\u540e\u7f00<\/h3>\n<\/p>\n<p><p>\u5c06\u4f60\u7684Python\u811a\u672c\u6587\u4ef6\u7684\u540e\u7f00\u4ece<code>.py<\/code>\u6539\u4e3a<code>.pyw<\/code>\u3002\u4f8b\u5982\uff0c\u5c06<code>my_script.py<\/code>\u6539\u4e3a<code>my_script.pyw<\/code>\u3002\u8fd9\u662f\u56e0\u4e3a<code>.pyw<\/code>\u6587\u4ef6\u5728Windows\u7cfb\u7edf\u4e0b\u6267\u884c\u65f6\u4e0d\u4f1a\u5f39\u51fa\u547d\u4ee4\u884c\u7a97\u53e3\uff0c\u800c<code>.py<\/code>\u6587\u4ef6\u4f1a\u5f39\u51fa\u4e00\u4e2a\u9ed1\u8272\u7684\u547d\u4ee4\u884c\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u914d\u7f6e\u65e0\u63a7\u5236\u53f0\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u7684\u811a\u672c\u4e0d\u4f9d\u8d56\u4e8e\u63a7\u5236\u53f0\u8f93\u51fa\u3002\u4f60\u53ef\u4ee5\u5c06\u6240\u6709\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u65e5\u5fd7\u6587\u4ef6\u6216\u8005\u5176\u4ed6\u7684\u8f93\u51fa\u65b9\u5f0f\u3002\u5982\u679c\u4f60\u7684\u811a\u672c\u53ea\u5305\u542b\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\uff0c\u90a3\u4e48\u5c06\u5176\u540e\u7f00\u6539\u4e3a<code>.pyw<\/code>\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(filename=&#39;app.log&#39;, level=logging.INFO)<\/p>\n<p>def main():<\/p>\n<p>    logging.info(&#39;Application started&#39;)<\/p>\n<p>    # Your code here<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6240\u6709\u8f93\u51fa\u90fd\u88ab\u91cd\u5b9a\u5411\u5230\u4e86<code>app.log<\/code>\u6587\u4ef6\u4e2d\uff0c\u800c\u4e0d\u662f\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528PyInstaller\u6253\u5305<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528PyInstaller\u5c06\u4f60\u7684Python\u811a\u672c\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --windowed my_script.pyw<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u7684<code>--onefile<\/code>\u9009\u9879\u8868\u793a\u5c06\u6240\u6709\u5185\u5bb9\u6253\u5305\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c<code>--windowed<\/code>\u9009\u9879\u8868\u793a\u4e0d\u4f1a\u521b\u5efa\u63a7\u5236\u53f0\u7a97\u53e3\u3002\u6253\u5305\u5b8c\u6210\u540e\uff0c\u4f60\u5c06\u5728<code>dist<\/code>\u76ee\u5f55\u4e0b\u627e\u5230\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6d4b\u8bd5\u6253\u5305\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u6700\u540e\u4e00\u6b65\u662f\u6d4b\u8bd5\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u53cc\u51fb\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u786e\u8ba4\u662f\u5426\u80fd\u591f\u6b63\u786e\u8fd0\u884c\u3002\u5982\u679c\u6ca1\u6709\u95ee\u9898\uff0c\u4f60\u7684Python\u811a\u672c\u5c31\u6210\u529f\u5730\u88ab\u6253\u5305\u6210\u4e86\u4e00\u4e2a\u65e0\u63a7\u5236\u53f0\u7a97\u53e3\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u6253\u5305\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u9047\u5230\u7684\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5728\u6253\u5305\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u95ee\u9898\uff0c\u6bd4\u5982\u4f9d\u8d56\u5305\u627e\u4e0d\u5230\u3001\u6587\u4ef6\u8def\u5f84\u95ee\u9898\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<\/p>\n<p><p><strong>1. \u7f3a\u5c11\u4f9d\u8d56\uff1a<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u7684\u811a\u672c\u4f9d\u8d56\u4e8e\u67d0\u4e9b\u7b2c\u4e09\u65b9\u5e93\uff0c\u786e\u4fdd\u8fd9\u4e9b\u5e93\u90fd\u5df2\u7ecf\u5b89\u88c5\u5728\u4f60\u7684Python\u73af\u5883\u4e2d\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u5b89\u88c5\u7f3a\u5c11\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u6587\u4ef6\u8def\u5f84\uff1a<\/strong><\/p>\n<p>\u6709\u65f6\u5728\u6253\u5305\u8fc7\u7a0b\u4e2d\uff0c\u6587\u4ef6\u8def\u5f84\u4f1a\u51fa\u73b0\u95ee\u9898\u3002\u786e\u4fdd\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6216\u8005\u5904\u7406\u597d\u76f8\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p><strong>3. \u517c\u5bb9\u6027\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u4e0d\u540c\u7248\u672c\u7684Python\u548c\u7b2c\u4e09\u65b9\u5e93\u53ef\u80fd\u4f1a\u5f15\u53d1\u517c\u5bb9\u6027\u95ee\u9898\u3002\u786e\u4fdd\u4f60\u4f7f\u7528\u7684\u6240\u6709\u5de5\u5177\u548c\u5e93\u7248\u672c\u90fd\u662f\u517c\u5bb9\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f18\u5316\u548c\u7ef4\u62a4<\/h3>\n<\/p>\n<p><p>\u6210\u529f\u6253\u5305\u540e\uff0c\u8fd8\u9700\u8981\u5bf9\u5e94\u7528\u7a0b\u5e8f\u8fdb\u884c\u4f18\u5316\u548c\u7ef4\u62a4\u3002\u786e\u4fdd\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u90fd\u80fd\u591f\u6b63\u5e38\u8fd0\u884c\uff0c\u5e76\u5b9a\u671f\u66f4\u65b0\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u4f18\u5316\u6027\u80fd\uff1a<\/strong><\/p>\n<p>\u68c0\u67e5\u4f60\u7684\u4ee3\u7801\uff0c\u4f18\u5316\u6027\u80fd\uff0c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u8fd0\u884c\u6d41\u7545\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u66f4\u65b0\u4f9d\u8d56\uff1a<\/strong><\/p>\n<p>\u5b9a\u671f\u68c0\u67e5\u5e76\u66f4\u65b0\u7b2c\u4e09\u65b9\u5e93\uff0c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>3. \u6d4b\u8bd5\uff1a<\/strong><\/p>\n<p>\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u548c\u73af\u5883\u4e0b\u8fdb\u884c\u6d4b\u8bd5\uff0c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u7684\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u6210\u529f\u5730\u5c06Python\u811a\u672c\u6253\u5305\u6210\u65e0\u63a7\u5236\u53f0\u7a97\u53e3\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u63d0\u4f9b\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u811a\u672c\u6253\u5305\u4e3a\u53ef\u6267\u884c\u7684pyw\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3apyw\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u7b80\u5355\u5730\u5c06\u811a\u672c\u7684\u6269\u5c55\u540d\u4ece.py\u66f4\u6539\u4e3a.pyw\u3002\u8fd9\u79cd\u683c\u5f0f\u5141\u8bb8\u60a8\u5728\u6ca1\u6709\u547d\u4ee4\u884c\u7a97\u53e3\u7684\u60c5\u51b5\u4e0b\u8fd0\u884cPython\u7a0b\u5e8f\uff0c\u9002\u5408\u56fe\u5f62\u7528\u6237\u754c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002\u786e\u4fdd\u60a8\u7684\u4ee3\u7801\u4e2d\u6ca1\u6709print\u8bed\u53e5\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u4f1a\u5728\u540e\u53f0\u7a97\u53e3\u4e2d\u663e\u793a\u3002<\/p>\n<p><strong>pyw\u6587\u4ef6\u4e0epy\u6587\u4ef6\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><br \/>pyw\u6587\u4ef6\u548cpy\u6587\u4ef6\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u6267\u884c\u73af\u5883\u3002py\u6587\u4ef6\u5728\u547d\u4ee4\u884c\u7a97\u53e3\u4e2d\u6267\u884c\uff0c\u9002\u5408\u9700\u8981\u8f93\u51fa\u7684\u63a7\u5236\u53f0\u5e94\u7528\u7a0b\u5e8f\uff0c\u800cpyw\u6587\u4ef6\u5219\u4e0d\u663e\u793a\u63a7\u5236\u53f0\u7a97\u53e3\uff0c\u66f4\u52a0\u9002\u7528\u4e8e\u56fe\u5f62\u754c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002\u4f7f\u7528pyw\u6587\u4ef6\uff0c\u7528\u6237\u53ef\u4ee5\u83b7\u5f97\u66f4\u6d41\u7545\u7684\u4f53\u9a8c\uff0c\u7279\u522b\u662f\u5728\u4e0d\u9700\u8981\u4ea4\u4e92\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u5c06Python\u7a0b\u5e8f\u6253\u5305\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff1f<\/strong><br \/>\u9664\u4e86\u76f4\u63a5\u66f4\u6539\u6269\u5c55\u540d\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u8bf8\u5982PyInstaller\u3001cx_Freeze\u6216py2exe\u7b49\u5de5\u5177\u5c06Python\u7a0b\u5e8f\u6253\u5305\u4e3a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5c06\u60a8\u7684\u4ee3\u7801\u548c\u6240\u9700\u7684\u5e93\u6253\u5305\u5728\u4e00\u8d77\uff0c\u521b\u5efa\u4e00\u4e2a\u65e0\u9700Python\u73af\u5883\u5373\u53ef\u8fd0\u884c\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u5177\u4f53\u6b65\u9aa4\u901a\u5e38\u5305\u62ec\u5b89\u88c5\u5de5\u5177\u3001\u7f16\u5199\u914d\u7f6e\u6587\u4ef6\u548c\u8fd0\u884c\u6253\u5305\u547d\u4ee4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5c06\u811a\u672c\u6253\u5305\u6210pyw\u6587\u4ef6\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efaPython\u811a\u672c\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5de5\u5177\u3001\u4fee\u6539\u6587\u4ef6\u540e\u7f00\u3001\u914d\u7f6e\u65e0\u63a7 [&hellip;]","protected":false},"author":3,"featured_media":1151845,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151838"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1151838"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151838\/revisions"}],"predecessor-version":[{"id":1151848,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151838\/revisions\/1151848"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1151845"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1151838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1151838"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1151838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}