{"id":1030158,"date":"2024-12-31T11:14:41","date_gmt":"2024-12-31T03:14:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1030158.html"},"modified":"2024-12-31T11:14:44","modified_gmt":"2024-12-31T03:14:44","slug":"python%e5%88%9b%e5%bb%ba%e7%9a%84%e7%aa%97%e5%8f%a3%e5%a6%82%e4%bd%95%e9%9a%90%e8%97%8fdos%e7%aa%97%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1030158.html","title":{"rendered":"python\u521b\u5efa\u7684\u7a97\u53e3\u5982\u4f55\u9690\u85cfdos\u7a97\u53e3"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/748bb680-e128-4548-9e9d-bb970d7cebb4.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u521b\u5efa\u7684\u7a97\u53e3\u5982\u4f55\u9690\u85cfdos\u7a97\u53e3\" \/><\/p>\n<p><p> <strong>\u5728Python\u521b\u5efa\u7684\u7a97\u53e3\u4e2d\uff0c\u9690\u85cfDOS\u7a97\u53e3\u7684\u65b9\u6cd5\u6709\u51e0\u79cd\uff0c\u5305\u62ec\u4f7f\u7528<code>pyinstaller<\/code>\u6253\u5305\u3001\u4f7f\u7528<code>pythonw<\/code>\u89e3\u91ca\u5668\u3001\u4ee5\u53ca\u901a\u8fc7\u4fee\u6539\u6587\u4ef6\u6269\u5c55\u540d\u7b49\u65b9\u5f0f\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528<code>pyinstaller<\/code>\u6253\u5305\u662f\u6700\u5e38\u7528\u7684\u4e00\u79cd\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u80fd\u591f\u9690\u85cfDOS\u7a97\u53e3\uff0c\u8fd8\u80fd\u5c06Python\u811a\u672c\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u65b9\u4fbf\u5206\u53d1\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528PyInstaller\u6253\u5305<\/h3>\n<\/p>\n<p><p><code>PyInstaller<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06Python\u811a\u672c\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u5b83\u6709\u4e00\u4e2a\u9009\u9879\u53ef\u4ee5\u9690\u85cfDOS\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5PyInstaller<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5<code>PyInstaller<\/code>\u3002\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyinstaller<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6253\u5305\u5e76\u9690\u85cfDOS\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6253\u5305\u4f60\u7684Python\u811a\u672c\u5e76\u9690\u85cfDOS\u7a97\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --noconsole your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>--noconsole<\/code>\u9009\u9879\u4f1a\u9690\u85cfDOS\u7a97\u53e3\uff0c\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u5728<code>dist<\/code>\u6587\u4ef6\u5939\u5185\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pythonw.exe\u89e3\u91ca\u5668<\/h3>\n<\/p>\n<p><p>\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0cPython\u81ea\u5e26\u4e86\u4e00\u4e2a<code>pythonw.exe<\/code>\u89e3\u91ca\u5668\uff0c\u5b83\u53ef\u4ee5\u8fd0\u884cPython\u811a\u672c\u800c\u4e0d\u663e\u793aDOS\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5c06\u4f60\u7684Python\u811a\u672c\u4fdd\u5b58\u4e3a<code>.pyw<\/code>\u6587\u4ef6\u6269\u5c55\u540d\u3002\u4f8b\u5982\uff0c\u5c06<code>your_script.py<\/code>\u91cd\u547d\u540d\u4e3a<code>your_script.pyw<\/code>\u3002\u7136\u540e\u53cc\u51fb\u8fd0\u884c\u8fd9\u4e2a<code>.pyw<\/code>\u6587\u4ef6\uff0cDOS\u7a97\u53e3\u5c31\u4e0d\u4f1a\u663e\u793a\u4e86\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4fee\u6539\u6587\u4ef6\u6269\u5c55\u540d<\/h3>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u7b80\u5355\u5730\u901a\u8fc7\u4fee\u6539Python\u811a\u672c\u7684\u6587\u4ef6\u6269\u5c55\u540d\u6765\u9690\u85cfDOS\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h4>\u4fee\u6539\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5c06\u4f60\u7684Python\u811a\u672c\u7684\u6587\u4ef6\u6269\u5c55\u540d\u4ece<code>.py<\/code>\u6539\u4e3a<code>.pyw<\/code>\u3002\u4f8b\u5982\uff0c\u5c06<code>your_script.py<\/code>\u91cd\u547d\u540d\u4e3a<code>your_script.pyw<\/code>\u3002\u8fd9\u6837\uff0c\u53cc\u51fb\u8fd0\u884c\u8fd9\u4e2a<code>.pyw<\/code>\u6587\u4ef6\u65f6\uff0cDOS\u7a97\u53e3\u5c31\u4e0d\u4f1a\u663e\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528ctypes\u5e93<\/h3>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528<code>ctypes<\/code>\u5e93\u6765\u9690\u85cfDOS\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h4>\u4ee3\u7801\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ctypes<\/p>\n<p>import sys<\/p>\n<p>if sys.platform == &quot;win32&quot;:<\/p>\n<p>    ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 0)<\/p>\n<h2><strong>\u4f60\u7684\u5176\u4ed6\u4ee3\u7801<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u5728Windows\u5e73\u53f0\u4e0a\u9690\u85cfDOS\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982tkinter\u6216PyQt<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\uff0c\u5982<code>tkinter<\/code>\u6216<code>PyQt<\/code>\uff0c\u8fd9\u4e9b\u5e93\u672c\u8eab\u5c31\u53ef\u4ee5\u521b\u5efa\u6ca1\u6709DOS\u7a97\u53e3\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>Tkinter\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;My Application&quot;)<\/p>\n<p>root.geometry(&quot;400x300&quot;)<\/p>\n<h2><strong>\u4f60\u7684\u5176\u4ed6\u4ee3\u7801<\/strong><\/h2>\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><h4>PyQt\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QMainWindow<\/p>\n<p>app = QApplication([])<\/p>\n<p>window = QMainWindow()<\/p>\n<p>window.setWindowTitle(&quot;My Application&quot;)<\/p>\n<p>window.setGeometry(100, 100, 400, 300)<\/p>\n<h2><strong>\u4f60\u7684\u5176\u4ed6\u4ee3\u7801<\/strong><\/h2>\n<p>window.show()<\/p>\n<p>app.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u9690\u85cfDOS\u7a97\u53e3\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u9009\u62e9\u9002\u5408\u4f60\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u3002<strong>\u4f7f\u7528<code>PyInstaller<\/code>\u6253\u5305\u3001\u4f7f\u7528<code>pythonw<\/code>\u89e3\u91ca\u5668\u3001\u4ee5\u53ca\u901a\u8fc7\u4fee\u6539\u6587\u4ef6\u6269\u5c55\u540d<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u5982\u679c\u4f60\u6b63\u5728\u5f00\u53d1\u4e00\u4e2a\u56fe\u5f62\u7528\u6237\u754c\u9762\u5e94\u7528\u7a0b\u5e8f\uff0c\u90a3\u4e48\u4f7f\u7528<code>tkinter<\/code>\u6216<code>PyQt<\/code>\u4e5f\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u65e0\u8bba\u4f60\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u6709\u6548\u5730\u9690\u85cfDOS\u7a97\u53e3\uff0c\u4f7f\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u770b\u8d77\u6765\u66f4\u4e13\u4e1a\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u7a97\u53e3\u800c\u4e0d\u663e\u793aDOS\u7a97\u53e3\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u521b\u5efaGUI\u7a97\u53e3\u65f6\uff0c\u901a\u5e38\u4f1a\u4f34\u968f\u4e00\u4e2a\u547d\u4ee4\u884c\u7a97\u53e3\uff08DOS\u7a97\u53e3\uff09\u7684\u51fa\u73b0\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pythonw.exe<\/code>\u6765\u8fd0\u884c\u4f60\u7684\u811a\u672c\u3002\u8fd9\u4e2a\u7248\u672c\u7684Python\u4e0d\u4f1a\u6253\u5f00\u547d\u4ee4\u884c\u7a97\u53e3\u3002\u4f60\u53ea\u9700\u5c06\u811a\u672c\u4fdd\u5b58\u4e3a<code>.pyw<\/code>\u683c\u5f0f\uff0c\u7136\u540e\u53cc\u51fb\u8fd0\u884c\uff0c\u6216\u8005\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528<code>pythonw your_script.pyw<\/code>\u6765\u6267\u884c\u5373\u53ef\u3002<\/p>\n<p><strong>\u5728Tkinter\u4e2d\u5982\u4f55\u9690\u85cf\u547d\u4ee4\u884c\u7a97\u53e3\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u4f7f\u7528Tkinter\u5e93\u6765\u521b\u5efaGUI\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u51e0\u884c\u6765\u9690\u85cfDOS\u7a97\u53e3\uff1a  <\/p>\n<pre><code class=\"language-python\">import tkinter as tk\nimport sys\nif sys.platform == &quot;win32&quot;:\n    import win32gui\n    import win32con\n    hwnd = win32gui.GetConsoleWindow()\n    win32gui.ShowWindow(hwnd, win32con.SW_HIDE)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u9690\u85cf\u547d\u4ee4\u884c\u7a97\u53e3\uff0c\u53ea\u663e\u793a\u4f60\u7684Tkinter\u7a97\u53e3\u3002<\/p>\n<p><strong>\u4f7f\u7528PyQt\u65f6\u5982\u4f55\u907f\u514dDOS\u7a97\u53e3\u7684\u663e\u793a\uff1f<\/strong><br \/>\u5728\u4f7f\u7528PyQt\u6846\u67b6\u5f00\u53d1\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u5efa\u8bae\u5c06\u5e94\u7528\u7a0b\u5e8f\u4fdd\u5b58\u4e3a<code>.pyw<\/code>\u6587\u4ef6\u3002\u5982\u679c\u4f60\u4f7f\u7528PyInstaller\u6253\u5305\u5e94\u7528\u7a0b\u5e8f\uff0c\u786e\u4fdd\u5728\u6253\u5305\u65f6\u8bbe\u7f6e<code>--noconsole<\/code>\u53c2\u6570\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5728\u8fd0\u884c\u65f6\u663e\u793a\u547d\u4ee4\u884c\u7a97\u53e3\uff0c\u786e\u4fdd\u4f60\u7684PyQt\u7a97\u53e3\u72ec\u7acb\u8fd0\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u521b\u5efa\u7684\u7a97\u53e3\u4e2d\uff0c\u9690\u85cfDOS\u7a97\u53e3\u7684\u65b9\u6cd5\u6709\u51e0\u79cd\uff0c\u5305\u62ec\u4f7f\u7528pyinstaller\u6253\u5305\u3001\u4f7f\u7528pythonw [&hellip;]","protected":false},"author":3,"featured_media":1030172,"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\/1030158"}],"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=1030158"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1030158\/revisions"}],"predecessor-version":[{"id":1030177,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1030158\/revisions\/1030177"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1030172"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1030158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1030158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1030158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}