{"id":1052612,"date":"2024-12-31T14:26:36","date_gmt":"2024-12-31T06:26:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1052612.html"},"modified":"2024-12-31T14:26:40","modified_gmt":"2024-12-31T06:26:40","slug":"python%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e5%85%a8%e9%83%a8%e7%9a%84%e5%ad%97%e4%bd%93%e5%a4%a7%e5%b0%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1052612.html","title":{"rendered":"python\u5982\u4f55\u8bbe\u7f6e\u5168\u90e8\u7684\u5b57\u4f53\u5927\u5c0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/046f1613-c419-42b1-a0f4-a163feac877b.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u8bbe\u7f6e\u5168\u90e8\u7684\u5b57\u4f53\u5927\u5c0f\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8bbe\u7f6e\u5168\u90e8\u5b57\u4f53\u5927\u5c0f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u53d6\u51b3\u4e8e\u5177\u4f53\u4f7f\u7528\u7684\u5e93\u548c\u5e94\u7528\u573a\u666f\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Tkinter\u3001Matplotlib\u548cPygame\u5e93\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5177\u4f53\u6765\u8bf4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u901a\u8fc7Tkinter\u5e93\u8bbe\u7f6eGUI\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5b57\u4f53\u5927\u5c0f\u3001\u901a\u8fc7Matplotlib\u5e93\u8bbe\u7f6e\u7ed8\u56fe\u4e2d\u7684\u5b57\u4f53\u5927\u5c0f\u3001\u901a\u8fc7Pygame\u5e93\u8bbe\u7f6e\u6e38\u620f\u6216\u591a\u5a92\u4f53\u5e94\u7528\u4e2d\u7684\u5b57\u4f53\u5927\u5c0f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728\u8fd9\u4e9b\u5e93\u4e2d\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001TKINTER\u5e93\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f<\/p>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u901a\u8fc7Tkinter\uff0c\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u3002\u5728Tkinter\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5b57\u4f53\u5c5e\u6027\u6765\u6539\u53d8\u5b57\u4f53\u5927\u5c0f\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import font<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u793a\u4f8b&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f<\/strong><\/h2>\n<p>default_font = font.nametofont(&quot;TkDefaultFont&quot;)<\/p>\n<p>default_font.configure(size=20)<\/p>\n<h2><strong>\u521b\u5efa\u6807\u7b7e\u5e76\u8bbe\u7f6e\u5b57\u4f53<\/strong><\/h2>\n<p>label = tk.Label(root, text=&quot;Hello, World!&quot;, font=default_font)<\/p>\n<p>label.pack()<\/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><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u5bfc\u5165tkinter\u548c\u5b57\u4f53\u6a21\u5757\uff0c\u7136\u540e\u521b\u5efa\u4e3b\u7a97\u53e3\u3002\u901a\u8fc7<code>font.nametofont(&quot;TkDefaultFont&quot;)<\/code>\u83b7\u53d6\u9ed8\u8ba4\u5b57\u4f53\uff0c\u5e76\u4f7f\u7528<code>default_font.configure(size=20)<\/code>\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u4e3a20\u3002\u6700\u540e\uff0c\u5c06\u5b57\u4f53\u5e94\u7528\u5230\u6807\u7b7e\u4e0a\u5e76\u8fd0\u884c\u4e3b\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001MATPLOTLIB\u5e93\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f<\/p>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u3002\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5b57\u4f53\u5c5e\u6027\u6765\u6539\u53d8\u56fe\u8868\u4e2d\u7684\u5b57\u4f53\u5927\u5c0f\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f<\/strong><\/h2>\n<p>plt.rcParams.update({&#39;font.size&#39;: 12})<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [10, 20, 25, 30, 35]<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u8868<\/strong><\/h2>\n<p>plt.plot(x, y, label=&#39;\u6570\u636e&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&#39;\u793a\u4f8b\u56fe\u8868&#39;)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u4f8b<\/strong><\/h2>\n<p>plt.legend()<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u5bfc\u5165Matplotlib\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>plt.rcParams.update({&#39;font.size&#39;: 12})<\/code>\u8bbe\u7f6e\u5168\u5c40\u5b57\u4f53\u5927\u5c0f\u4e3a12\u3002\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u793a\u4f8b\u6570\u636e\u5e76\u7ed8\u5236\u56fe\u8868\uff0c\u8bbe\u7f6e\u6807\u9898\u548c\u6807\u7b7e\u7684\u5b57\u4f53\u5927\u5c0f\uff0c\u5e76\u663e\u793a\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001PYGAME\u5e93\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f<\/p>\n<\/p>\n<p><p>Pygame\u662f\u4e00\u4e2a\u7528\u4e8e\u5f00\u53d1\u6e38\u620f\u548c\u591a\u5a92\u4f53\u5e94\u7528\u7684\u5e93\uff0c\u901a\u8fc7Pygame\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u4ee5\u4fbf\u5728\u6e38\u620f\u6216\u591a\u5a92\u4f53\u5e94\u7528\u4e2d\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pygame<\/p>\n<h2><strong>\u521d\u59cb\u5316Pygame<\/strong><\/h2>\n<p>pygame.init()<\/p>\n<h2><strong>\u8bbe\u7f6e\u5c4f\u5e55\u5c3a\u5bf8<\/strong><\/h2>\n<p>screen = pygame.display.set_mode((640, 480))<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b57\u4f53\u548c\u5927\u5c0f<\/strong><\/h2>\n<p>font = pygame.font.Font(None, 36)<\/p>\n<h2><strong>\u521b\u5efa\u6587\u672c\u8868\u9762<\/strong><\/h2>\n<p>text_surface = font.render(&#39;Hello, World!&#39;, True, (255, 255, 255))<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u672c\u77e9\u5f62\u533a\u57df<\/strong><\/h2>\n<p>text_rect = text_surface.get_rect(center=(320, 240))<\/p>\n<h2><strong>\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>running = True<\/p>\n<p>while running:<\/p>\n<p>    for event in pygame.event.get():<\/p>\n<p>        if event.type == pygame.QUIT:<\/p>\n<p>            running = False<\/p>\n<p>    # \u586b\u5145\u80cc\u666f\u989c\u8272<\/p>\n<p>    screen.fill((0, 0, 0))<\/p>\n<p>    # \u7ed8\u5236\u6587\u672c<\/p>\n<p>    screen.blit(text_surface, text_rect)<\/p>\n<p>    # \u66f4\u65b0\u663e\u793a<\/p>\n<p>    pygame.display.flip()<\/p>\n<h2><strong>\u9000\u51faPygame<\/strong><\/h2>\n<p>pygame.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u521d\u59cb\u5316Pygame\u5e76\u8bbe\u7f6e\u5c4f\u5e55\u5c3a\u5bf8\u3002\u7136\u540e\u4f7f\u7528<code>pygame.font.Font(None, 36)<\/code>\u8bbe\u7f6e\u5b57\u4f53\u548c\u5927\u5c0f\uff0836\uff09\u3002\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u6587\u672c\u8868\u9762\u5e76\u83b7\u53d6\u6587\u672c\u77e9\u5f62\u533a\u57df\u3002\u5728\u4e3b\u5faa\u73af\u4e2d\uff0c\u5904\u7406\u4e8b\u4ef6\u3001\u586b\u5145\u80cc\u666f\u989c\u8272\u3001\u7ed8\u5236\u6587\u672c\u5e76\u66f4\u65b0\u663e\u793a\uff0c\u6700\u540e\u9000\u51faPygame\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u53ef\u4ee5\u770b\u51fa\u5728Python\u4e2d\u8bbe\u7f6e\u5168\u90e8\u5b57\u4f53\u5927\u5c0f\u7684\u65b9\u6cd5\u4e3b\u8981\u53d6\u51b3\u4e8e\u5177\u4f53\u4f7f\u7528\u7684\u5e93\u548c\u5e94\u7528\u573a\u666f\u3002\u65e0\u8bba\u662f\u4f7f\u7528Tkinter\u3001Matplotlib\u8fd8\u662fPygame\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5b57\u4f53\u5c5e\u6027\u6765\u6539\u53d8\u5b57\u4f53\u5927\u5c0f\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5bf9\u60a8\u5728Python\u7f16\u7a0b\u4e2d\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5168\u5c40\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u4f53\u5927\u5c0f\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\u5e93\uff08\u5982Tkinter\u3001PyQt\u6216Matplotlib\uff09\u6765\u8bbe\u7f6e\u3002\u4ee5Tkinter\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u6837\u5f0f\u8868\u6765\u5b9a\u4e49\u6240\u6709\u63a7\u4ef6\u7684\u5b57\u4f53\u5927\u5c0f\u3002\u5177\u4f53\u64cd\u4f5c\u5305\u62ec\uff1a\u4f7f\u7528<code>font<\/code>\u6a21\u5757\u6765\u8bbe\u7f6e\u5b57\u4f53\u5c5e\u6027\uff0c\u6216\u901a\u8fc7<code>tkFont.Font<\/code>\u7c7b\u6307\u5b9a\u5168\u5c40\u5b57\u4f53\u3002\u8fd9\u6837\uff0c\u6240\u6709\u521b\u5efa\u7684\u63a7\u4ef6\u90fd\u5c06\u5e94\u7528\u76f8\u540c\u7684\u5b57\u4f53\u8bbe\u7f6e\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Matplotlib\u65f6\uff0c\u5982\u4f55\u7edf\u4e00\u8c03\u6574\u6240\u6709\u56fe\u8868\u7684\u5b57\u4f53\u5927\u5c0f\uff1f<\/strong><br \/>Matplotlib\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5168\u5c40\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u3002\u53ef\u4ee5\u4f7f\u7528<code>matplotlib.rcParams<\/code>\u5b57\u5178\u6765\u8c03\u6574\u5b57\u4f53\u5927\u5c0f\u3002\u4f8b\u5982\uff0c<code>matplotlib.rcParams[&#39;font.size&#39;] = 12<\/code>\u5c06\u8bbe\u7f6e\u6240\u6709\u6587\u672c\u7684\u5b57\u4f53\u5927\u5c0f\u4e3a12\u3002\u6b64\u8bbe\u7f6e\u9002\u7528\u4e8e\u56fe\u8868\u7684\u6807\u9898\u3001\u5750\u6807\u8f74\u6807\u7b7e\u548c\u56fe\u4f8b\u7b49\u3002<\/p>\n<p><strong>\u5728PyQt\u4e2d\u5982\u4f55\u5b9e\u73b0\u5168\u5c40\u5b57\u4f53\u5927\u5c0f\u8bbe\u7f6e\uff1f<\/strong><br \/>\u5728PyQt\u4e2d\uff0c\u8bbe\u7f6e\u5168\u5c40\u5b57\u4f53\u5927\u5c0f\u53ef\u4ee5\u901a\u8fc7<code>QApplication<\/code>\u7c7b\u6765\u5b9e\u73b0\u3002\u53ef\u4ee5\u4f7f\u7528<code>QFont<\/code>\u7c7b\u5b9a\u4e49\u4e00\u4e2a\u5b57\u4f53\u5bf9\u8c61\uff0c\u7136\u540e\u901a\u8fc7<code>QApplication.setFont()<\/code>\u65b9\u6cd5\u5e94\u7528\u4e8e\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u3002\u8fd9\u6837\uff0c\u6240\u6709\u7a97\u53e3\u548c\u63a7\u4ef6\u90fd\u5c06\u7ee7\u627f\u8fd9\u4e2a\u5b57\u4f53\u8bbe\u7f6e\uff0c\u786e\u4fdd\u4e00\u81f4\u7684\u5916\u89c2\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Web\u5e94\u7528\u4e2d\u4f7f\u7528Python\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5728\u4f7f\u7528Flask\u6216Django\u7b49Web\u6846\u67b6\u6784\u5efa\u5e94\u7528\uff0c\u53ef\u4ee5\u901a\u8fc7CSS\u6587\u4ef6\u6765\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u3002\u60a8\u53ef\u4ee5\u5728HTML\u6a21\u677f\u4e2d\u94fe\u63a5CSS\uff0c\u5e76\u4f7f\u7528\u6837\u5f0f\u9009\u62e9\u5668\u5b9a\u4e49\u5168\u5c40\u5b57\u4f53\u5927\u5c0f\u3002\u4f8b\u5982\uff0c<code>body { font-size: 16px; }<\/code>\u5c06\u5f71\u54cd\u6240\u6709\u6587\u672c\u5143\u7d20\u3002\u7ed3\u5408Python\u540e\u7aef\u903b\u8f91\uff0c\u60a8\u53ef\u4ee5\u52a8\u6001\u751f\u6210\u6837\u5f0f\u4ee5\u6ee1\u8db3\u7279\u5b9a\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8bbe\u7f6e\u5168\u90e8\u5b57\u4f53\u5927\u5c0f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u53d6\u51b3\u4e8e\u5177\u4f53\u4f7f\u7528\u7684\u5e93\u548c\u5e94\u7528\u573a\u666f\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Tkinte [&hellip;]","protected":false},"author":3,"featured_media":1052621,"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\/1052612"}],"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=1052612"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1052612\/revisions"}],"predecessor-version":[{"id":1052624,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1052612\/revisions\/1052624"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1052621"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1052612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1052612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1052612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}