{"id":1014222,"date":"2024-12-27T11:54:26","date_gmt":"2024-12-27T03:54:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1014222.html"},"modified":"2024-12-27T11:54:29","modified_gmt":"2024-12-27T03:54:29","slug":"python%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e4%b8%ad%e6%96%87%e6%a0%87%e9%a2%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1014222.html","title":{"rendered":"python\u5982\u4f55\u663e\u793a\u4e2d\u6587\u6807\u9898"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094422\/caa906e7-5a73-4da5-b9ca-6323003a0458.webp\" alt=\"python\u5982\u4f55\u663e\u793a\u4e2d\u6587\u6807\u9898\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u663e\u793a\u4e2d\u6587\u6807\u9898\u7684\u65b9\u6cd5\u6709\uff1a\u786e\u4fdd\u6e90\u4ee3\u7801\u6587\u4ef6\u4f7f\u7528UTF-8\u7f16\u7801\u3001\u5728\u4ee3\u7801\u4e2d\u663e\u5f0f\u58f0\u660e\u7f16\u7801\u683c\u5f0f\u3001\u4f7f\u7528\u5408\u9002\u7684\u5b57\u4f53\u4ee5\u652f\u6301\u4e2d\u6587\u663e\u793a\u3001\u5728\u7ed8\u56fe\u65f6\u4f7f\u7528\u9002\u5f53\u7684\u5e93\u548c\u65b9\u6cd5\u6765\u652f\u6301\u4e2d\u6587\u663e\u793a<\/strong>\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u786e\u4fdd\u6e90\u4ee3\u7801\u6587\u4ef6\u4f7f\u7528UTF-8\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728Python\u4e2d\u6b63\u786e\u663e\u793a\u4e2d\u6587\u6807\u9898\uff0c\u9996\u5148\u8981\u786e\u4fdd\u4f60\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u662f\u4f7f\u7528UTF-8\u7f16\u7801\u4fdd\u5b58\u7684\u3002\u8fd9\u662f\u56e0\u4e3aUTF-8\u662f\u4e00\u4e2a\u80fd\u517c\u5bb9\u591a\u79cd\u8bed\u8a00\u5b57\u7b26\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u80fd\u591f\u5f88\u597d\u5730\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u68c0\u67e5\u6587\u4ef6\u7f16\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5927\u591a\u6570\u73b0\u4ee3\u7684\u4ee3\u7801\u7f16\u8f91\u5668\u4e2d\uff0c\u5982Visual Studio Code\u3001PyCharm\u6216\u8005Sublime Text\uff0c\u4fdd\u5b58\u6587\u4ef6\u65f6\u53ef\u4ee5\u9009\u62e9\u7f16\u7801\u683c\u5f0f\u3002\u786e\u4fdd\u9009\u62e9UTF-8\u7f16\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728\u4ee3\u7801\u5f00\u5934\u58f0\u660e\u7f16\u7801\u683c\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python 2\u4e2d\uff0c\u4f60\u9700\u8981\u5728\u6587\u4ef6\u7684\u7b2c\u4e00\u884c\u6dfb\u52a0\u7f16\u7801\u58f0\u660e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Python 3\u4e2d\uff0c\u9ed8\u8ba4\u652f\u6301UTF-8\u7f16\u7801\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u663e\u5f0f\u58f0\u660e\uff0c\u4f46\u4fdd\u6301\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u4e3aUTF-8\u4ecd\u7136\u662f\u4e00\u4e2a\u826f\u597d\u7684\u5b9e\u8df5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5408\u9002\u7684\u5b57\u4f53\u4ee5\u652f\u6301\u4e2d\u6587\u663e\u793a<\/p>\n<\/p>\n<p><p>\u5728\u7ed8\u56fe\u6216GUI\u5e94\u7528\u7a0b\u5e8f\u4e2d\u663e\u793a\u4e2d\u6587\u65f6\uff0c\u9009\u62e9\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u7684\u5b57\u4f53\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u5f88\u591a\u7cfb\u7edf\u9ed8\u8ba4\u7684\u5b57\u4f53\u5e76\u4e0d\u652f\u6301\u4e2d\u6587\u5b57\u7b26\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u663e\u793a\u4e71\u7801\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u5b57\u4f53<\/strong><\/p>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u5e38\u7528\u7684\u4e2d\u6587\u5b57\u4f53\u5305\u62ec\u201cSimSun\uff08\u5b8b\u4f53\uff09\u201d\u3001\u201cSimHei\uff08\u9ed1\u4f53\uff09\u201d\u3001\u201cMicrosoft YaHei\uff08\u5fae\u8f6f\u96c5\u9ed1\uff09\u201d\u7b49\u3002\u5728macOS\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528\u201cPingFang SC\u201d\u6216\u201cHeiti SC\u201d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728Matplotlib\u4e2d\u8bbe\u7f6e\u5b57\u4f53<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u4f7f\u7528Matplotlib\u8fdb\u884c\u7ed8\u56fe\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;]  # \u7528\u9ed1\u4f53\u663e\u793a\u4e2d\u6587<\/p>\n<p>plt.rcParams[&#39;axes.unicode_minus&#39;] = False  # \u89e3\u51b3\u4fdd\u5b58\u56fe\u50cf\u662f\u8d1f\u53f7 &#39;-&#39; \u663e\u793a\u4e3a\u65b9\u5757\u7684\u95ee\u9898<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u7ed8\u56fe\u65f6\u663e\u793a\u4e2d\u6587\u6807\u9898\u4e0d\u4f1a\u51fa\u73b0\u4e71\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528\u9002\u5f53\u7684\u5e93\u548c\u65b9\u6cd5\u6765\u652f\u6301\u4e2d\u6587\u663e\u793a<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u4e2a\u5e93\u652f\u6301\u56fe\u5f62\u7528\u6237\u754c\u9762\u548c\u7ed8\u56fe\uff0c\u8fd9\u4e9b\u5e93\u4e2d\u6709\u4e00\u4e9b\u63d0\u4f9b\u4e86\u5bf9\u4e2d\u6587\u5b57\u7b26\u7684\u826f\u597d\u652f\u6301\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528Matplotlib\u7ed8\u56fe<\/strong><\/p>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u3002\u4e3a\u4e86\u5728\u56fe\u8868\u4e2d\u663e\u793a\u4e2d\u6587\u6807\u9898\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u8bbe\u7f6e\u4e86\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\uff08\u5982\u524d\u9762\u6240\u8ff0\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>plt.title(&#39;\u4e2d\u6587\u6807\u9898&#39;)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Tkinter\u5f00\u53d1GUI\u5e94\u7528<\/strong><\/p>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5de5\u5177\u5305\uff0c\u652f\u6301\u5728\u754c\u9762\u4e2d\u663e\u793a\u4e2d\u6587\u6587\u672c\u3002\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53\u53ef\u4ee5\u901a\u8fc7<code>font<\/code>\u53c2\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>root = tk.Tk()<\/p>\n<p>label = tk.Label(root, text=&#39;\u4e2d\u6587\u6807\u9898&#39;, font=(&#39;SimHei&#39;, 12))<\/p>\n<p>label.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<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u7684\u8f93\u5165\u8f93\u51fa<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u7684\u8f93\u5165\u548c\u8f93\u51fa\u65f6\uff0c\u786e\u4fdd\u7cfb\u7edf\u7684\u7f16\u7801\u8bbe\u7f6e\u4e0e\u7a0b\u5e8f\u7684\u7f16\u7801\u4e00\u81f4\uff0c\u4ee5\u907f\u514d\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u548c\u5199\u5165\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u7684\u6587\u4ef6\u65f6\uff0c\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;\u6587\u4ef6.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u547d\u4ee4\u884c\u8f93\u5165\u8f93\u51fa<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u8f93\u5165\u8f93\u51fa\u4e2d\uff0cPython 3\u9ed8\u8ba4\u652f\u6301UTF-8\u7f16\u7801\uff0c\u4f46\u5728\u67d0\u4e9b\u73af\u5883\u4e2d\u53ef\u80fd\u9700\u8981\u624b\u52a8\u8bbe\u7f6e\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>sys.stdout.reconfigure(encoding=&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5176\u4ed6\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u73af\u5883\u914d\u7f6e<\/strong><\/p>\n<\/p>\n<p><p>\u786e\u4fdd\u64cd\u4f5c\u7cfb\u7edf\u548c\u5f00\u53d1\u73af\u5883\u7684\u8bed\u8a00\u8bbe\u7f6e\u652f\u6301\u4e2d\u6587\u663e\u793a\u3002\u5982\u679c\u7cfb\u7edf\u73af\u5883\u4e0d\u652f\u6301\u4e2d\u6587\uff0c\u5373\u4f7f\u4ee3\u7801\u8bbe\u7f6e\u6b63\u786e\uff0c\u4e5f\u53ef\u80fd\u65e0\u6cd5\u663e\u793a\u4e2d\u6587\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u8bd5\u548c\u6d4b\u8bd5<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u5b9a\u671f\u8fdb\u884c\u8c03\u8bd5\u548c\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u4e2d\u6587\u5b57\u7b26\u80fd\u591f\u6b63\u786e\u663e\u793a\u3002\u540c\u65f6\uff0c\u6ce8\u610f\u68c0\u67e5\u7b2c\u4e09\u65b9\u5e93\u7684\u6587\u6863\uff0c\u4e86\u89e3\u5176\u5bf9\u4e2d\u6587\u652f\u6301\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u6b63\u786e\u663e\u793a\u4e2d\u6587\u6807\u9898\uff0c\u65e0\u8bba\u662f\u5728\u63a7\u5236\u53f0\u3001GUI\u5e94\u7528\uff0c\u8fd8\u662f\u5728\u6570\u636e\u53ef\u89c6\u5316\u7684\u56fe\u8868\u4e2d\u3002\u786e\u4fdd\u7f16\u7801\u683c\u5f0f\u6b63\u786e\u3001\u9009\u62e9\u5408\u9002\u7684\u5b57\u4f53\uff0c\u5e76\u4f7f\u7528\u652f\u6301\u4e2d\u6587\u663e\u793a\u7684\u5e93\u548c\u65b9\u6cd5\uff0c\u662f\u6210\u529f\u663e\u793a\u4e2d\u6587\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u4e2d\u6587\u6807\u9898\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\u5e93\uff08\u5982Tkinter\u3001PyQt\u6216Matplotlib\uff09\u6765\u8bbe\u7f6e\u4e2d\u6587\u6807\u9898\u3002\u786e\u4fdd\u60a8\u7684\u4ee3\u7801\u6587\u4ef6\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u5e76\u5728\u8bbe\u7f6e\u6807\u9898\u65f6\u5c06\u4e2d\u6587\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9\u76f8\u5e94\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5728Tkinter\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>title()<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u7a97\u53e3\u6807\u9898\u4e3a\u4e2d\u6587\u3002<\/p>\n<p><strong>\u5728Jupyter Notebook\u4e2d\u5982\u4f55\u663e\u793a\u4e2d\u6587\u6807\u9898\uff1f<\/strong><br \/>\u5728Jupyter Notebook\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eMatplotlib\u7684\u5b57\u4f53\u6765\u663e\u793a\u4e2d\u6587\u6807\u9898\u3002\u60a8\u9700\u8981\u5148\u5b89\u88c5\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\uff0c\u5e76\u5728\u7ed8\u56fe\u524d\u8bbe\u7f6e\u5b57\u4f53\u3002\u4f8b\u5982\uff0c\u901a\u8fc7<code>plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;]<\/code>\u6765\u6307\u5b9a\u4f7f\u7528\u9ed1\u4f53\u5b57\uff0c\u8fd9\u6837\u5728\u7ed8\u5236\u56fe\u5f62\u65f6\uff0c\u6807\u9898\u53ef\u4ee5\u6b63\u5e38\u663e\u793a\u4e2d\u6587\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u4e2d\u4e2d\u6587\u5b57\u7b26\u7684\u7f16\u7801\u95ee\u9898\uff1f<\/strong><br \/>\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u65f6\uff0c\u5e38\u5e38\u4f1a\u9047\u5230\u7f16\u7801\u95ee\u9898\u3002\u786e\u4fdd\u60a8\u7684Python\u73af\u5883\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u4ee3\u7801\u6587\u4ef6\u5f00\u5934\u6dfb\u52a0<code># -*- coding: utf-8 -*-<\/code>\u6765\u6307\u5b9a\u3002\u6b64\u5916\uff0c\u5728\u8bfb\u5199\u6587\u4ef6\u65f6\uff0c\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u65f6\u8bbe\u7f6e<code>encoding=&#39;utf-8&#39;<\/code>\u53c2\u6570\uff0c\u4ee5\u786e\u4fdd\u6b63\u786e\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u663e\u793a\u4e2d\u6587\u6807\u9898\u7684\u65b9\u6cd5\u6709\uff1a\u786e\u4fdd\u6e90\u4ee3\u7801\u6587\u4ef6\u4f7f\u7528UTF-8\u7f16\u7801\u3001\u5728\u4ee3\u7801\u4e2d\u663e\u5f0f\u58f0\u660e\u7f16\u7801\u683c\u5f0f\u3001\u4f7f\u7528\u5408\u9002\u7684\u5b57 [&hellip;]","protected":false},"author":3,"featured_media":1014236,"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\/1014222"}],"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=1014222"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1014222\/revisions"}],"predecessor-version":[{"id":1014237,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1014222\/revisions\/1014237"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1014236"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1014222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1014222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1014222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}