{"id":1113685,"date":"2025-01-08T17:50:16","date_gmt":"2025-01-08T09:50:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1113685.html"},"modified":"2025-01-08T17:50:18","modified_gmt":"2025-01-08T09:50:18","slug":"python%e4%b8%ad%e7%9a%84%e5%a6%82%e4%bd%95%e5%9c%a8%e7%94%bb%e5%b8%83%e4%b8%8a%e8%bf%9b%e8%a1%8c%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1113685.html","title":{"rendered":"python\u4e2d\u7684\u5982\u4f55\u5728\u753b\u5e03\u4e0a\u8fdb\u884c\u64cd\u4f5c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075156\/650b20a5-c488-431e-8336-851379374869.webp\" alt=\"python\u4e2d\u7684\u5982\u4f55\u5728\u753b\u5e03\u4e0a\u8fdb\u884c\u64cd\u4f5c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u64cd\u4f5c\u753b\u5e03\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Tkinter\u5e93\u3001\u4f7f\u7528Pygame\u5e93\u3001\u4f7f\u7528Matplotlib\u5e93\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>Tkinter<\/strong>\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u7b80\u5355\u7684\u56fe\u5f62\u754c\u9762\u548c\u753b\u5e03\u64cd\u4f5c\uff1b<strong>Pygame<\/strong>\u66f4\u9002\u5408\u5f00\u53d1\u6e38\u620f\u548c\u591a\u5a92\u4f53\u5e94\u7528\uff1b<strong>Matplotlib<\/strong>\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u548c\u7ed8\u56fe\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u8fd9\u4e09\u79cd\u5e93\u8fdb\u884c\u753b\u5e03\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528Tkinter\u5e93\u8fdb\u884c\u753b\u5e03\u64cd\u4f5c<\/h2>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u56fe\u5f62\u754c\u9762\u548c\u5728\u753b\u5e03\u4e0a\u7ed8\u5236\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Tkinter\u5e93\u5728\u753b\u5e03\u4e0a\u8fdb\u884c\u57fa\u672c\u64cd\u4f5c\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>1. \u521b\u5efa\u753b\u5e03<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2aTkinter\u7a97\u53e3\u548c\u4e00\u4e2a\u753b\u5e03\u5bf9\u8c61\u3002\u753b\u5e03\u5bf9\u8c61\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\uff0c\u5982\u7ebf\u6761\u3001\u77e9\u5f62\u3001\u692d\u5706\u7b49\u3002<\/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;Tkinter \u753b\u5e03\u793a\u4f8b&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u753b\u5e03\u5bf9\u8c61<\/strong><\/h2>\n<p>canvas = tk.Canvas(root, width=500, height=500, bg=&quot;white&quot;)<\/p>\n<p>canvas.pack()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u7ed8\u5236\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u753b\u5e03\u5bf9\u8c61\u7684\u5404\u79cd\u65b9\u6cd5\u6765\u7ed8\u5236\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u7ed8\u56fe\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u753b\u7ebf<\/strong>\uff1a<code>create_line(x1, y1, x2, y2, options)<\/code><\/li>\n<li><strong>\u753b\u77e9\u5f62<\/strong>\uff1a<code>create_rectangle(x1, y1, x2, y2, options)<\/code><\/li>\n<li><strong>\u753b\u692d\u5706<\/strong>\uff1a<code>create_oval(x1, y1, x2, y2, options)<\/code><\/li>\n<li><strong>\u753b\u591a\u8fb9\u5f62<\/strong>\uff1a<code>create_polygon(points, options)<\/code><\/li>\n<li><strong>\u753b\u6587\u672c<\/strong>\uff1a<code>create_text(x, y, options)<\/code><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u753b\u7ebf<\/p>\n<p>canvas.create_line(50, 50, 200, 200, fill=&quot;blue&quot;, width=2)<\/p>\n<h2><strong>\u753b\u77e9\u5f62<\/strong><\/h2>\n<p>canvas.create_rectangle(50, 50, 200, 200, outline=&quot;red&quot;, width=2)<\/p>\n<h2><strong>\u753b\u692d\u5706<\/strong><\/h2>\n<p>canvas.create_oval(250, 50, 400, 200, outline=&quot;green&quot;, width=2)<\/p>\n<h2><strong>\u753b\u591a\u8fb9\u5f62<\/strong><\/h2>\n<p>canvas.create_polygon([100, 300, 200, 250, 300, 300, 200, 350], outline=&quot;purple&quot;, fill=&quot;yellow&quot;, width=2)<\/p>\n<h2><strong>\u753b\u6587\u672c<\/strong><\/h2>\n<p>canvas.create_text(250, 400, text=&quot;Hello, Tkinter!&quot;, font=(&quot;Arial&quot;, 20), fill=&quot;black&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u8fd0\u884c\u4e3b\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u8fd0\u884c\u4e3b\u5faa\u73af\u4ee5\u663e\u793a\u7a97\u53e3\u548c\u7ed8\u5236\u7684\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8fd0\u884c\u4e3b\u5faa\u73af<\/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>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u57fa\u672c\u56fe\u5f62\u7684Tkinter\u753b\u5e03\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528Pygame\u5e93\u8fdb\u884c\u753b\u5e03\u64cd\u4f5c<\/h2>\n<\/p>\n<p><p>Pygame\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684\u591a\u5a92\u4f53\u5e93\uff0c\u9002\u7528\u4e8e\u6e38\u620f\u5f00\u53d1\u548c\u591a\u5a92\u4f53\u5e94\u7528\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Pygame\u5e93\u5728\u753b\u5e03\u4e0a\u8fdb\u884c\u57fa\u672c\u64cd\u4f5c\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>1. \u521d\u59cb\u5316Pygame<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u521d\u59cb\u5316Pygame\u5e93\u548c\u521b\u5efa\u4e00\u4e2a\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pygame<\/p>\n<p>import sys<\/p>\n<h2><strong>\u521d\u59cb\u5316Pygame<\/strong><\/h2>\n<p>pygame.init()<\/p>\n<h2><strong>\u521b\u5efa\u7a97\u53e3<\/strong><\/h2>\n<p>screen = pygame.display.set_mode((500, 500))<\/p>\n<p>pygame.display.set_caption(&quot;Pygame \u753b\u5e03\u793a\u4f8b&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u7ed8\u5236\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Pygame\u7684\u7ed8\u56fe\u51fd\u6570\u5728\u7a97\u53e3\u4e2d\u7ed8\u5236\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u7ed8\u56fe\u51fd\u6570\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u753b\u7ebf<\/strong>\uff1a<code>pygame.draw.line(surface, color, start_pos, end_pos, width)<\/code><\/li>\n<li><strong>\u753b\u77e9\u5f62<\/strong>\uff1a<code>pygame.draw.rect(surface, color, rect, width)<\/code><\/li>\n<li><strong>\u753b\u692d\u5706<\/strong>\uff1a<code>pygame.draw.ellipse(surface, color, rect, width)<\/code><\/li>\n<li><strong>\u753b\u591a\u8fb9\u5f62<\/strong>\uff1a<code>pygame.draw.polygon(surface, color, points, width)<\/code><\/li>\n<li><strong>\u753b\u6587\u672c<\/strong>\uff1a\u4f7f\u7528<code>pygame.font.Font<\/code>\u5bf9\u8c61\u7ed8\u5236\u6587\u672c<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u989c\u8272<\/p>\n<p>WHITE = (255, 255, 255)<\/p>\n<p>BLUE = (0, 0, 255)<\/p>\n<p>RED = (255, 0, 0)<\/p>\n<p>GREEN = (0, 255, 0)<\/p>\n<p>PURPLE = (128, 0, 128)<\/p>\n<p>YELLOW = (255, 255, 0)<\/p>\n<p>BLACK = (0, 0, 0)<\/p>\n<h2><strong>\u6e05\u5c4f<\/strong><\/h2>\n<p>screen.fill(WHITE)<\/p>\n<h2><strong>\u753b\u7ebf<\/strong><\/h2>\n<p>pygame.draw.line(screen, BLUE, (50, 50), (200, 200), 2)<\/p>\n<h2><strong>\u753b\u77e9\u5f62<\/strong><\/h2>\n<p>pygame.draw.rect(screen, RED, (50, 50, 150, 150), 2)<\/p>\n<h2><strong>\u753b\u692d\u5706<\/strong><\/h2>\n<p>pygame.draw.ellipse(screen, GREEN, (250, 50, 150, 150), 2)<\/p>\n<h2><strong>\u753b\u591a\u8fb9\u5f62<\/strong><\/h2>\n<p>pygame.draw.polygon(screen, PURPLE, [(100, 300), (200, 250), (300, 300), (200, 350)], 2)<\/p>\n<h2><strong>\u753b\u6587\u672c<\/strong><\/h2>\n<p>font = pygame.font.Font(None, 36)<\/p>\n<p>text = font.render(&quot;Hello, Pygame!&quot;, True, BLACK)<\/p>\n<p>screen.blit(text, (150, 400))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u4e8b\u4ef6\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4fdd\u6301\u7a97\u53e3\u6253\u5f00\u5e76\u54cd\u5e94\u7528\u6237\u4e8b\u4ef6\uff0c\u9700\u8981\u8fd0\u884c\u4e00\u4e2a\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e8b\u4ef6\u5faa\u73af<\/p>\n<p>while True:<\/p>\n<p>    for event in pygame.event.get():<\/p>\n<p>        if event.type == pygame.QUIT:<\/p>\n<p>            pygame.quit()<\/p>\n<p>            sys.exit()<\/p>\n<p>    # \u5237\u65b0\u5c4f\u5e55<\/p>\n<p>    pygame.display.flip()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u57fa\u672c\u56fe\u5f62\u7684Pygame\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528Matplotlib\u5e93\u8fdb\u884c\u753b\u5e03\u64cd\u4f5c<\/h2>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u7ed8\u5236\u5404\u79cd\u56fe\u8868\u548c\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Matplotlib\u5e93\u5728\u753b\u5e03\u4e0a\u8fdb\u884c\u57fa\u672c\u64cd\u4f5c\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>1. \u521b\u5efa\u753b\u5e03<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165Matplotlib\u5e93\u5e76\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61\u548c\u8f74\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u548c\u8f74\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u7ed8\u5236\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u8f74\u5bf9\u8c61\u7684\u5404\u79cd\u65b9\u6cd5\u6765\u7ed8\u5236\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u7ed8\u56fe\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u753b\u7ebf<\/strong>\uff1a<code>ax.plot(x, y, options)<\/code><\/li>\n<li><strong>\u753b\u77e9\u5f62<\/strong>\uff1a\u4f7f\u7528<code>patches.Rectangle<\/code><\/li>\n<li><strong>\u753b\u692d\u5706<\/strong>\uff1a\u4f7f\u7528<code>patches.Ellipse<\/code><\/li>\n<li><strong>\u753b\u591a\u8fb9\u5f62<\/strong>\uff1a\u4f7f\u7528<code>patches.Polygon<\/code><\/li>\n<li><strong>\u753b\u6587\u672c<\/strong>\uff1a<code>ax.text(x, y, s, options)<\/code><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import matplotlib.patches as patches<\/p>\n<h2><strong>\u753b\u7ebf<\/strong><\/h2>\n<p>ax.plot([50, 200], [50, 200], color=&quot;blue&quot;, linewidth=2)<\/p>\n<h2><strong>\u753b\u77e9\u5f62<\/strong><\/h2>\n<p>rect = patches.Rectangle((50, 50), 150, 150, linewidth=2, edgecolor=&quot;red&quot;, facecolor=&quot;none&quot;)<\/p>\n<p>ax.add_patch(rect)<\/p>\n<h2><strong>\u753b\u692d\u5706<\/strong><\/h2>\n<p>ellipse = patches.Ellipse((325, 125), 150, 150, linewidth=2, edgecolor=&quot;green&quot;, facecolor=&quot;none&quot;)<\/p>\n<p>ax.add_patch(ellipse)<\/p>\n<h2><strong>\u753b\u591a\u8fb9\u5f62<\/strong><\/h2>\n<p>polygon = patches.Polygon([[100, 300], [200, 250], [300, 300], [200, 350]], linewidth=2, edgecolor=&quot;purple&quot;, facecolor=&quot;yellow&quot;)<\/p>\n<p>ax.add_patch(polygon)<\/p>\n<h2><strong>\u753b\u6587\u672c<\/strong><\/h2>\n<p>ax.text(250, 400, &quot;Hello, Matplotlib!&quot;, fontsize=20, color=&quot;black&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u663e\u793a\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u663e\u793a\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u8f74\u8303\u56f4<\/p>\n<p>ax.set_xlim(0, 500)<\/p>\n<p>ax.set_ylim(0, 500)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.gca().set_aspect(&#39;equal&#39;, adjustable=&#39;box&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u57fa\u672c\u56fe\u5f62\u7684Matplotlib\u753b\u5e03\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u753b\u5e03\u4e0a\u8fdb\u884c\u64cd\u4f5c\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528Tkinter\u5e93\u3001Pygame\u5e93\u548cMatplotlib\u5e93\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u60a8\u7684\u5177\u4f53\u9700\u6c42\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>Tkinter<\/strong>\uff1a\u9002\u7528\u4e8e\u521b\u5efa\u7b80\u5355\u7684\u56fe\u5f62\u754c\u9762\u548c\u57fa\u672c\u7684\u753b\u5e03\u64cd\u4f5c\u3002<\/li>\n<li><strong>Pygame<\/strong>\uff1a\u9002\u7528\u4e8e\u6e38\u620f\u5f00\u53d1\u548c\u591a\u5a92\u4f53\u5e94\u7528\uff0c\u63d0\u4f9b\u66f4\u591a\u9ad8\u7ea7\u529f\u80fd\u3002<\/li>\n<li><strong>Matplotlib<\/strong>\uff1a\u9002\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u548c\u7ed8\u56fe\uff0c\u63d0\u4f9b\u5f3a\u5927\u7684\u7ed8\u56fe\u529f\u80fd\u3002<\/li>\n<\/ul>\n<p><p>\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u8fdb\u884c\u753b\u5e03\u64cd\u4f5c\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1\u5728Python\u4e2d\u8fdb\u884c\u753b\u5e03\u64cd\u4f5c\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u753b\u5e03\u8fdb\u884c\u7ed8\u56fe\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\u6765\u521b\u5efa\u4e00\u4e2a\u753b\u5e03\u3002\u9996\u5148\u9700\u8981\u5bfc\u5165Tkinter\u6a21\u5757\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\u3002\u5728\u4e3b\u7a97\u53e3\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Canvas<\/code>\u7c7b\u6765\u521b\u5efa\u753b\u5e03\u3002\u901a\u8fc7\u8bbe\u7f6e\u753b\u5e03\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff0c\u53ef\u4ee5\u5b9a\u4e49\u753b\u5e03\u7684\u5c3a\u5bf8\u3002\u6700\u540e\uff0c\u8c03\u7528<code>pack()<\/code>\u65b9\u6cd5\u5c06\u753b\u5e03\u6dfb\u52a0\u5230\u4e3b\u7a97\u53e3\u4e2d\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import tkinter as tk\n\nroot = tk.Tk()\ncanvas = tk.Canvas(root, width=400, height=300)\ncanvas.pack()\nroot.mainloop()\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728\u753b\u5e03\u4e0a\u7ed8\u5236\u5f62\u72b6\u548c\u6587\u672c\uff1f<\/strong><br \/>\u5728Tkinter\u7684\u753b\u5e03\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u65b9\u6cd5\u6765\u7ed8\u5236\u5f62\u72b6\u548c\u6587\u672c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>create_line()<\/code>\u3001<code>create_rectangle()<\/code>\u3001<code>create_oval()<\/code>\u7b49\u65b9\u6cd5\u53ef\u4ee5\u7ed8\u5236\u7ebf\u6761\u3001\u77e9\u5f62\u548c\u692d\u5706\u3002\u4f7f\u7528<code>create_text()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5728\u6307\u5b9a\u4f4d\u7f6e\u6dfb\u52a0\u6587\u672c\u3002\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u9700\u8981\u63d0\u4f9b\u5fc5\u8981\u7684\u53c2\u6570\uff0c\u4f8b\u5982\u5750\u6807\u548c\u989c\u8272\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">canvas.create_rectangle(50, 50, 150, 100, fill=&quot;blue&quot;)\ncanvas.create_oval(200, 50, 300, 150, fill=&quot;red&quot;)\ncanvas.create_text(100, 200, text=&quot;Hello, Canvas!&quot;, fill=&quot;black&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728\u753b\u5e03\u4e0a\u54cd\u5e94\u9f20\u6807\u4e8b\u4ef6\uff1f<\/strong><br \/>\u901a\u8fc7\u7ed1\u5b9a\u9f20\u6807\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u753b\u5e03\u5bf9\u7528\u6237\u7684\u64cd\u4f5c\u505a\u51fa\u53cd\u5e94\u3002\u5728Tkinter\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>bind()<\/code>\u65b9\u6cd5\u6765\u7ed1\u5b9a\u4e8b\u4ef6\u548c\u5904\u7406\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u7ed1\u5b9a\u9f20\u6807\u70b9\u51fb\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>&lt;Button-1&gt;<\/code>\u6765\u5b9e\u73b0\u3002\u5f53\u7528\u6237\u5728\u753b\u5e03\u4e0a\u70b9\u51fb\u65f6\uff0c\u6307\u5b9a\u7684\u5904\u7406\u51fd\u6570\u5c06\u4f1a\u88ab\u8c03\u7528\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">def on_click(event):\n    print(f&quot;Clicked at: {event.x}, {event.y}&quot;)\n\ncanvas.bind(&quot;&lt;Button-1&gt;&quot;, on_click)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u4f7f\u7528\u6237\u4e0e\u753b\u5e03\u8fdb\u884c\u4ea4\u4e92\uff0c\u589e\u5f3a\u5e94\u7528\u7a0b\u5e8f\u7684\u4e92\u52a8\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u64cd\u4f5c\u753b\u5e03\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Tkinter\u5e93\u3001\u4f7f\u7528Pygame\u5e93\u3001\u4f7f\u7528Matplotlib\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1113696,"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\/1113685"}],"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=1113685"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113685\/revisions"}],"predecessor-version":[{"id":1113698,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113685\/revisions\/1113698"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1113696"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1113685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1113685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1113685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}