{"id":1161932,"date":"2025-01-13T19:22:28","date_gmt":"2025-01-13T11:22:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1161932.html"},"modified":"2025-01-13T19:22:30","modified_gmt":"2025-01-13T11:22:30","slug":"python%e6%95%b2%e5%ae%8c%e4%bb%a3%e7%a0%81%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1161932.html","title":{"rendered":"python\u6572\u5b8c\u4ee3\u7801\u5982\u4f55\u8f93\u51fa"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25203058\/634bd7b5-89d3-453e-98ec-d9c008255af5.webp\" alt=\"python\u6572\u5b8c\u4ee3\u7801\u5982\u4f55\u8f93\u51fa\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6572\u5b8c\u4ee3\u7801\u540e\u7684\u8f93\u51fa\u53ef\u4ee5\u4f7f\u7528print\u51fd\u6570\u3001logging\u6a21\u5757\u3001\u4fdd\u5b58\u5230\u6587\u4ef6\u3001\u4f7f\u7528IDE\u7684\u63a7\u5236\u53f0\u3001\u521b\u5efaGUI\u5e94\u7528\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002<\/strong>\u5176\u4e2d\uff0c<strong>print\u51fd\u6570<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u3002\u4f7f\u7528print\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5728\u63a7\u5236\u53f0\u6216\u7ec8\u7aef\u7a97\u53e3\u4e2d\u67e5\u770b\u8f93\u51fa\u7ed3\u679c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u5f0f\u2014\u2014\u5982\u4f55\u4f7f\u7528print\u51fd\u6570\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7print\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u53d8\u91cf\u3001\u5b57\u7b26\u4e32\u3001\u6570\u636e\u7ed3\u6784\u7b49\u5185\u5bb9\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002print\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u5c06\u9700\u8981\u8f93\u51fa\u7684\u5185\u5bb9\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9print\u51fd\u6570\u5373\u53ef\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, World!&quot;)<\/p>\n<p>x = 10<\/p>\n<p>print(&quot;The value of x is:&quot;, x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u7b2c\u4e00\u884c\u4ee3\u7801\u4f1a\u8f93\u51fa\u5b57\u7b26\u4e32\u201cHello, World!\u201d\uff0c\u800c\u7b2c\u4e8c\u884c\u4ee3\u7801\u4f1a\u8f93\u51fa\u53d8\u91cfx\u7684\u503c\u4ee5\u53ca\u4e00\u6bb5\u63cf\u8ff0\u6027\u6587\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6253\u5370\u8f93\u51fa\uff08print\u51fd\u6570\uff09<\/h3>\n<\/p>\n<p><p><strong>print\u51fd\u6570<\/strong>\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u8f93\u51fa\u65b9\u5f0f\u4e4b\u4e00\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u5c06\u7a0b\u5e8f\u7684\u8fd0\u884c\u7ed3\u679c\u3001\u53d8\u91cf\u503c\u7b49\u4fe1\u606f\u76f4\u63a5\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u4e0b\u9762\u662f\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528print\u51fd\u6570\u8fdb\u884c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cprint\u51fd\u6570\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u3001\u53d8\u91cf\u3001\u8868\u8fbe\u5f0f\u7b49\u5185\u5bb9\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u5176\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(value, ..., sep=&#39; &#39;, end=&#39;\\n&#39;, file=sys.stdout, flush=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>value<\/strong>\uff1a\u8981\u8f93\u51fa\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u662f\u591a\u4e2a\uff0c\u4f7f\u7528\u9017\u53f7\u5206\u9694\u3002<\/li>\n<li><strong>sep<\/strong>\uff1a\u591a\u4e2a\u5185\u5bb9\u4e4b\u95f4\u7684\u5206\u9694\u7b26\uff0c\u9ed8\u8ba4\u4e3a\u7a7a\u683c\u3002<\/li>\n<li><strong>end<\/strong>\uff1a\u8f93\u51fa\u7ed3\u675f\u65f6\u7684\u5b57\u7b26\uff0c\u9ed8\u8ba4\u4e3a\u6362\u884c\u7b26\u3002<\/li>\n<li><strong>file<\/strong>\uff1a\u8f93\u51fa\u7684\u76ee\u6807\u6587\u4ef6\u5bf9\u8c61\uff0c\u9ed8\u8ba4\u4e3asys.stdout\uff0c\u5373\u63a7\u5236\u53f0\u3002<\/li>\n<li><strong>flush<\/strong>\uff1a\u662f\u5426\u7acb\u5373\u5237\u65b0\u8f93\u51fa\u7f13\u51b2\u533a\uff0c\u9ed8\u8ba4\u4e3aFalse\u3002<\/li>\n<\/ul>\n<p><p>\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>print(&quot;Name:&quot;, name, &quot;Age:&quot;, age)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: Alice Age: 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u683c\u5f0f\u5316\u8f93\u51fa<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u8f93\u51fa\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u8bfb\u7684\u4fe1\u606f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5305\u62ec%\u683c\u5f0f\u5316\u3001str.format()\u65b9\u6cd5\u548cf-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>%\u683c\u5f0f\u5316<\/strong>\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>print(&quot;Name: %s, Age: %d&quot; % (name, age))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>str.format()\u65b9\u6cd5<\/strong>\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>print(&quot;Name: {}, Age: {}&quot;.format(name, age))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>f-string<\/strong>\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>print(f&quot;Name: {name}, Age: {age}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u591a\u884c\u8f93\u51fa<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8f93\u51fa\u591a\u884c\u5185\u5bb9\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728print\u51fd\u6570\u4e2d\u4f7f\u7528\u6362\u884c\u7b26\uff08\\n\uff09\u6216\u591a\u6b21\u8c03\u7528print\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Line 1\\nLine 2\\nLine 3&quot;)<\/p>\n<p>print(&quot;Line 1&quot;)<\/p>\n<p>print(&quot;Line 2&quot;)<\/p>\n<p>print(&quot;Line 3&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528logging\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u9664\u4e86print\u51fd\u6570\uff0cPython\u8fd8\u63d0\u4f9b\u4e86logging\u6a21\u5757\u7528\u4e8e\u8f93\u51fa\u65e5\u5fd7\u4fe1\u606f\u3002logging\u6a21\u5757\u6bd4print\u51fd\u6570\u66f4\u7075\u6d3b\u548c\u5f3a\u5927\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u8f93\u51fa\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528logging\u6a21\u5757\u8f93\u51fa\u65e5\u5fd7\u4fe1\u606f\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165logging\u6a21\u5757\u3002<\/li>\n<li>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668\uff08Logger\uff09\u3002<\/li>\n<li>\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u5668\u8f93\u51fa\u65e5\u5fd7\u4fe1\u606f\u3002<\/li>\n<\/ol>\n<p><p>\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logging.basicConfig(level=logging.DEBUG, format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u65e5\u5fd7\u4fe1\u606f<\/strong><\/h2>\n<p>logging.debug(&quot;This is a debug message&quot;)<\/p>\n<p>logging.info(&quot;This is an info message&quot;)<\/p>\n<p>logging.warning(&quot;This is a warning message&quot;)<\/p>\n<p>logging.error(&quot;This is an error message&quot;)<\/p>\n<p>logging.critical(&quot;This is a critical message&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u65e5\u5fd7\u914d\u7f6e<\/h4>\n<\/p>\n<p><p>logging\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65e5\u5fd7\u914d\u7f6e\u9009\u9879\uff0c\u53ef\u4ee5\u63a7\u5236\u65e5\u5fd7\u7684\u8f93\u51fa\u7ea7\u522b\u3001\u683c\u5f0f\u3001\u76ee\u6807\u7b49\u3002\u5e38\u7528\u7684\u914d\u7f6e\u9009\u9879\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>level<\/strong>\uff1a\u65e5\u5fd7\u8f93\u51fa\u7ea7\u522b\uff0c\u5e38\u89c1\u7ea7\u522b\u5305\u62ecDEBUG\u3001INFO\u3001WARNING\u3001ERROR\u3001CRITICAL\u3002<\/li>\n<li><strong>format<\/strong>\uff1a\u65e5\u5fd7\u8f93\u51fa\u683c\u5f0f\u3002<\/li>\n<li><strong>datefmt<\/strong>\uff1a\u65e5\u671f\u683c\u5f0f\u3002<\/li>\n<li><strong>filename<\/strong>\uff1a\u65e5\u5fd7\u8f93\u51fa\u6587\u4ef6\u3002<\/li>\n<li><strong>filemode<\/strong>\uff1a\u65e5\u5fd7\u6587\u4ef6\u7684\u6253\u5f00\u6a21\u5f0f\u3002<\/li>\n<\/ul>\n<p><p>\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logging.basicConfig(level=logging.DEBUG,<\/p>\n<p>                    format=&#39;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#39;,<\/p>\n<p>                    datefmt=&#39;%Y-%m-%d %H:%M:%S&#39;,<\/p>\n<p>                    filename=&#39;app.log&#39;,<\/p>\n<p>                    filemode=&#39;w&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u65e5\u5fd7\u4fe1\u606f<\/strong><\/h2>\n<p>logging.debug(&quot;This is a debug message&quot;)<\/p>\n<p>logging.info(&quot;This is an info message&quot;)<\/p>\n<p>logging.warning(&quot;This is a warning message&quot;)<\/p>\n<p>logging.error(&quot;This is an error message&quot;)<\/p>\n<p>logging.critical(&quot;This is a critical message&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4fdd\u5b58\u5230\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7\u91cd\u5b9a\u5411print\u51fd\u6570\u7684\u8f93\u51fa\u6216\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>1. \u91cd\u5b9a\u5411print\u51fd\u6570\u7684\u8f93\u51fa<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>sys.stdout<\/code>\u91cd\u5b9a\u5411print\u51fd\u6570\u7684\u8f93\u51fa\u5230\u6587\u4ef6\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as f:<\/p>\n<p>    # \u91cd\u5b9a\u5411print\u51fd\u6570\u7684\u8f93\u51fa\u5230\u6587\u4ef6<\/p>\n<p>    sys.stdout = f<\/p>\n<p>    print(&quot;This will be written to the file&quot;)<\/p>\n<p>    # \u6062\u590dprint\u51fd\u6570\u7684\u8f93\u51fa\u5230\u63a7\u5236\u53f0<\/p>\n<p>    sys.stdout = sys.__stdout__<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\uff08\u5982open\u3001write\u7b49\uff09\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as f:<\/p>\n<p>    f.write(&quot;This will be written to the file\\n&quot;)<\/p>\n<p>    f.write(&quot;Another line\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528IDE\u7684\u63a7\u5236\u53f0<\/h3>\n<\/p>\n<p><p>\u5927\u591a\u6570\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u90fd\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684\u63a7\u5236\u53f0\uff0c\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u4e2d\u67e5\u770b\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u3002\u5e38\u89c1\u7684IDE\u5305\u62ecPyCharm\u3001Visual Studio Code\u3001Jupyter Notebook\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. PyCharm<\/h4>\n<\/p>\n<p><p>\u5728PyCharm\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u8fd0\u884c\u7a97\u53e3\u4e2d\u67e5\u770bprint\u51fd\u6570\u7684\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, PyCharm!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. Visual Studio Code<\/h4>\n<\/p>\n<p><p>\u5728Visual Studio Code\u4e2d\uff0c\u53ef\u4ee5\u5728\u7ec8\u7aef\u7a97\u53e3\u4e2d\u67e5\u770bprint\u51fd\u6570\u7684\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, VS Code!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. Jupyter Notebook<\/h4>\n<\/p>\n<p><p>\u5728Jupyter Notebook\u4e2d\uff0c\u53ef\u4ee5\u5728\u5355\u5143\u683c\u7684\u8f93\u51fa\u533a\u57df\u67e5\u770bprint\u51fd\u6570\u7684\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, Jupyter Notebook!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u521b\u5efaGUI\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5728\u63a7\u5236\u53f0\u4e2d\u8f93\u51fa\u7ed3\u679c\uff0c\u8fd8\u53ef\u4ee5\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e94\u7528\uff0c\u5c06\u8f93\u51fa\u7ed3\u679c\u663e\u793a\u5728GUI\u7a97\u53e3\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684GUI\u5e93\uff08\u5982Tkinter\u3001PyQt\u7b49\uff09\u6765\u521b\u5efaGUI\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528Tkinter<\/h4>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u7b80\u5355\u7684GUI\u5e94\u7528\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Output Example&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6587\u672c\u6846<\/strong><\/h2>\n<p>text = tk.Text(root, height=10, width=40)<\/p>\n<p>text.pack()<\/p>\n<h2><strong>\u8f93\u51fa\u5185\u5bb9\u5230\u6587\u672c\u6846<\/strong><\/h2>\n<p>text.insert(tk.END, &quot;Hello, Tkinter!\\n&quot;)<\/p>\n<p>text.insert(tk.END, &quot;This is another line\\n&quot;)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u5faa\u73af<\/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>2. \u4f7f\u7528PyQt<\/h4>\n<\/p>\n<p><p>PyQt\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python GUI\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u590d\u6742\u7684GUI\u5e94\u7528\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QWidget, QTextEdit, QVBoxLayout<\/p>\n<h2><strong>\u521b\u5efa\u5e94\u7528<\/strong><\/h2>\n<p>app = QApplication(sys.argv)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>window = QWidget()<\/p>\n<p>window.setWindowTitle(&quot;Output Example&quot;)<\/p>\n<p>window.resize(400, 300)<\/p>\n<h2><strong>\u521b\u5efa\u6587\u672c\u7f16\u8f91\u6846<\/strong><\/h2>\n<p>text_edit = QTextEdit()<\/p>\n<p>text_edit.append(&quot;Hello, PyQt!&quot;)<\/p>\n<p>text_edit.append(&quot;This is another line&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5e03\u5c40<\/strong><\/h2>\n<p>layout = QVBoxLayout()<\/p>\n<p>layout.addWidget(text_edit)<\/p>\n<p>window.setLayout(layout)<\/p>\n<h2><strong>\u663e\u793a\u7a97\u53e3<\/strong><\/h2>\n<p>window.show()<\/p>\n<h2><strong>\u8fd0\u884c\u5e94\u7528<\/strong><\/h2>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6572\u5b8c\u4ee3\u7801\u540e\u7684\u8f93\u51fa\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u5305\u62ec\u4f7f\u7528print\u51fd\u6570\u3001logging\u6a21\u5757\u3001\u4fdd\u5b58\u5230\u6587\u4ef6\u3001\u4f7f\u7528IDE\u7684\u63a7\u5236\u53f0\u3001\u521b\u5efaGUI\u5e94\u7528\u7b49\u3002\u6bcf\u79cd\u65b9\u5f0f\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u52bf\u3002<strong>print\u51fd\u6570<\/strong>\u662f\u6700\u5e38\u7528\u7684\u8f93\u51fa\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u8f93\u51fa\u9700\u6c42\uff1b<strong>logging\u6a21\u5757<\/strong>\u9002\u7528\u4e8e\u590d\u6742\u7684\u65e5\u5fd7\u8bb0\u5f55\u9700\u6c42\uff1b<strong>\u4fdd\u5b58\u5230\u6587\u4ef6<\/strong>\u9002\u7528\u4e8e\u9700\u8981\u6301\u4e45\u5316\u5b58\u50a8\u8f93\u51fa\u7ed3\u679c\u7684\u573a\u666f\uff1b<strong>IDE\u7684\u63a7\u5236\u53f0<\/strong>\u9002\u7528\u4e8e\u5f00\u53d1\u548c\u8c03\u8bd5\u9636\u6bb5\u67e5\u770b\u8f93\u51fa\u7ed3\u679c\uff1b<strong>\u521b\u5efaGUI\u5e94\u7528<\/strong>\u9002\u7528\u4e8e\u9700\u8981\u56fe\u5f62\u754c\u9762\u663e\u793a\u8f93\u51fa\u7ed3\u679c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u8f93\u51fa\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u6765\u67e5\u770b\u548c\u5904\u7406\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u60a8\u5728Python\u7f16\u7a0b\u7684\u9053\u8def\u4e0a\u53d6\u5f97\u66f4\u597d\u7684\u6210\u7ee9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u67e5\u770b\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u6765\u67e5\u770b\u4ee3\u7801\u7684\u8f93\u51fa\u3002\u5c06\u4f60\u60f3\u8981\u8f93\u51fa\u7684\u5185\u5bb9\u6216\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>print()<\/code>\uff0c\u5373\u53ef\u5728\u63a7\u5236\u53f0\u4e0a\u663e\u793a\u7ed3\u679c\u3002\u4f8b\u5982\uff0c<code>print(&quot;Hello, World!&quot;)<\/code>\u5c06\u8f93\u51fa\u201cHello, World!\u201d\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06Python\u7684\u8f93\u51fa\u7ed3\u679c\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u652f\u6301\u5c06\u8f93\u51fa\u7ed3\u679c\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u521b\u5efa\u6216\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u7ed3\u5408<code>write()<\/code>\u65b9\u6cd5\u5c06\u8f93\u51fa\u5199\u5165\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u7ed3\u679c\u5199\u5165\u6587\u672c\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;w&#39;) as f:\n    f.write(&quot;Hello, World!&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u4f60\u7684\u8f93\u51fa\u5c06\u88ab\u4fdd\u5b58\u5230<code>output.txt<\/code>\u6587\u4ef6\u4e2d\u3002<\/p>\n<p><strong>\u5982\u4f55\u8c03\u8bd5Python\u4ee3\u7801\u4ee5\u67e5\u770b\u6bcf\u4e00\u6b65\u7684\u8f93\u51fa\uff1f<\/strong><br \/>\u8c03\u8bd5Python\u4ee3\u7801\u7684\u65b9\u6cd5\u4e4b\u4e00\u662f\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u5728\u5173\u952e\u4f4d\u7f6e\u8f93\u51fa\u53d8\u91cf\u7684\u503c\u3002\u8fd9\u6709\u52a9\u4e8e\u8ddf\u8e2a\u7a0b\u5e8f\u7684\u6267\u884c\u8fc7\u7a0b\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u4e2d\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u8bbe\u7f6e\u65ad\u70b9\u5e76\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u4ee5\u89c2\u5bdf\u6bcf\u4e00\u6b65\u7684\u8f93\u51fa\u548c\u53d8\u91cf\u72b6\u6001\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6572\u5b8c\u4ee3\u7801\u540e\u7684\u8f93\u51fa\u53ef\u4ee5\u4f7f\u7528print\u51fd\u6570\u3001logging\u6a21\u5757\u3001\u4fdd\u5b58\u5230\u6587\u4ef6\u3001\u4f7f\u7528IDE\u7684\u63a7\u5236\u53f0\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1161937,"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\/1161932"}],"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=1161932"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161932\/revisions"}],"predecessor-version":[{"id":1161939,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161932\/revisions\/1161939"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1161937"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1161932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1161932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1161932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}