{"id":1093434,"date":"2025-01-08T14:31:04","date_gmt":"2025-01-08T06:31:04","guid":{"rendered":""},"modified":"2025-01-08T14:31:06","modified_gmt":"2025-01-08T06:31:06","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e5%87%ba%e8%88%9e%e5%8f%b0%e5%8c%ba-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1093434.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u663e\u793a\u51fa\u821e\u53f0\u533a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210027\/efd6be5b-4d06-45d9-a7ce-4a074e64dbe5.webp\" alt=\"python\u4e2d\u5982\u4f55\u663e\u793a\u51fa\u821e\u53f0\u533a\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u663e\u793a\u821e\u53f0\u533a\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u56fe\u5f62\u5316\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\u6765\u5b9e\u73b0\uff0c\u5982Tkinter\u3001Pygame\u6216PyQt\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c<strong>Tkinter<\/strong>\u662fPython\u6807\u51c6\u5e93\u4e2d\u81ea\u5e26\u7684\uff0c\u6bd4\u8f83\u7b80\u5355\u6613\u7528\uff1b<strong>Pygame<\/strong>\u9002\u7528\u4e8e\u9700\u8981\u66f4\u591a\u56fe\u5f62\u548c\u5a92\u4f53\u5904\u7406\u80fd\u529b\u7684\u5e94\u7528\uff1b<strong>PyQt<\/strong>\u5219\u9002\u7528\u4e8e\u9700\u8981\u66f4\u590d\u6742\u548c\u7f8e\u89c2\u754c\u9762\u7684\u5e94\u7528\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u5b9e\u73b0\u663e\u793a\u821e\u53f0\u533a\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Tkinter\u521b\u5efa\u821e\u53f0\u533a<\/h3>\n<\/p>\n<p><p><strong>Tkinter<\/strong>\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684GUI\u5de5\u5177\u5305\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u63a5\u53e3\u6765\u521b\u5efa\u7a97\u53e3\u548c\u63a7\u4ef6\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Tkinter\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u821e\u53f0\u533a\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u548c\u5bfc\u5165Tkinter<\/h4>\n<\/p>\n<p><p>Tkinter\u662fPython\u81ea\u5e26\u7684\u5e93\uff0c\u56e0\u6b64\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002\u53ef\u4ee5\u76f4\u63a5\u5bfc\u5165\u5e76\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import Canvas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u4e3b\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\uff0c\u7528\u4e8e\u653e\u7f6e\u821e\u53f0\u533a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">root = tk.Tk()<\/p>\n<p>root.title(&quot;\u821e\u53f0\u533a&quot;)<\/p>\n<p>root.geometry(&quot;800x600&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u521b\u5efa\u548c\u914d\u7f6eCanvas<\/h4>\n<\/p>\n<p><p>Canvas\u662fTkinter\u4e2d\u7684\u4e00\u4e2a\u63a7\u4ef6\uff0c\u7528\u4e8e\u7ed8\u5236\u56fe\u5f62\u548c\u663e\u793a\u56fe\u50cf\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Canvas\u6765\u521b\u5efa\u821e\u53f0\u533a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">canvas = Canvas(root, width=800, height=600, bg=&quot;white&quot;)<\/p>\n<p>canvas.pack()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u7ed8\u5236\u56fe\u5f62<\/h4>\n<\/p>\n<p><p>\u5728Canvas\u4e0a\u7ed8\u5236\u4e00\u4e9b\u57fa\u672c\u56fe\u5f62\uff0c\u8868\u793a\u821e\u53f0\u533a\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ed8\u5236\u4e00\u4e2a\u77e9\u5f62\uff0c\u8868\u793a\u821e\u53f0<\/p>\n<p>canvas.create_rectangle(50, 50, 750, 550, outline=&quot;black&quot;, fill=&quot;lightblue&quot;)<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u4e9b\u5706\u5f62\uff0c\u8868\u793a\u821e\u53f0\u4e0a\u7684\u706f\u5149<\/strong><\/h2>\n<p>canvas.create_oval(100, 100, 150, 150, outline=&quot;red&quot;, fill=&quot;red&quot;)<\/p>\n<p>canvas.create_oval(650, 100, 700, 150, outline=&quot;red&quot;, fill=&quot;red&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5\u3001\u8fd0\u884c\u4e3b\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u8fd0\u884cTkinter\u7684\u4e3b\u5faa\u73af\uff0c\u663e\u793a\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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><h3>\u4e8c\u3001\u4f7f\u7528Pygame\u521b\u5efa\u821e\u53f0\u533a<\/h3>\n<\/p>\n<p><p><strong>Pygame<\/strong>\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684Python\u6a21\u5757\uff0c\u7528\u4e8e\u7f16\u5199\u89c6\u9891\u6e38\u620f\u3002\u5b83\u5305\u542b\u8ba1\u7b97\u673a\u56fe\u5f62\u548c\u58f0\u97f3\u5e93\uff0c\u9002\u5408\u521b\u5efa\u590d\u6742\u7684\u821e\u53f0\u533a\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u548c\u5bfc\u5165Pygame<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Pygame\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install pygame<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165Pygame\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pygame<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521d\u59cb\u5316Pygame<\/h4>\n<\/p>\n<p><p>\u521d\u59cb\u5316Pygame\u5e93\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pygame.init()<\/p>\n<p>screen = pygame.display.set_mode((800, 600))<\/p>\n<p>pygame.display.set_caption(&quot;\u821e\u53f0\u533a&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7ed8\u5236\u56fe\u5f62<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pygame\u7684\u7ed8\u56fe\u51fd\u6570\u6765\u521b\u5efa\u821e\u53f0\u533a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u989c\u8272<\/p>\n<p>white = (255, 255, 255)<\/p>\n<p>black = (0, 0, 0)<\/p>\n<p>blue = (0, 0, 255)<\/p>\n<p>red = (255, 0, 0)<\/p>\n<h2><strong>\u586b\u5145\u80cc\u666f<\/strong><\/h2>\n<p>screen.fill(white)<\/p>\n<h2><strong>\u7ed8\u5236\u821e\u53f0<\/strong><\/h2>\n<p>pygame.draw.rect(screen, blue, [50, 50, 700, 500], 0)<\/p>\n<h2><strong>\u7ed8\u5236\u706f\u5149<\/strong><\/h2>\n<p>pygame.draw.circle(screen, red, (125, 125), 25)<\/p>\n<p>pygame.draw.circle(screen, red, (675, 125), 25)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u66f4\u65b0\u5c4f\u5e55\u548c\u4e8b\u4ef6\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u66f4\u65b0\u5c4f\u5e55\u5185\u5bb9\uff0c\u5e76\u5904\u7406\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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>    pygame.display.flip()<\/p>\n<p>pygame.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528PyQt\u521b\u5efa\u821e\u53f0\u533a<\/h3>\n<\/p>\n<p><p><strong>PyQt<\/strong>\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684Python GUI\u5e93\uff0c\u9002\u5408\u9700\u8981\u66f4\u590d\u6742\u548c\u7f8e\u89c2\u754c\u9762\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u548c\u5bfc\u5165PyQt<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5PyQt\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install PyQt5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165PyQt\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget<\/p>\n<p>from PyQt5.QtGui import QPainter, QColor<\/p>\n<p>from PyQt5.QtCore import Qt<\/p>\n<p>import sys<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u4e3b\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\u7c7b\uff0c\u5e76\u5728\u5176\u4e2d\u7ed8\u5236\u821e\u53f0\u533a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class StageWindow(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&quot;\u821e\u53f0\u533a&quot;)<\/p>\n<p>        self.setGeometry(100, 100, 800, 600)<\/p>\n<p>        self.stage = Stage(self)<\/p>\n<p>        self.setCentralWidget(self.stage)<\/p>\n<p>class Stage(QWidget):<\/p>\n<p>    def __init__(self, parent):<\/p>\n<p>        super().__init__(parent)<\/p>\n<p>    def paintEvent(self, event):<\/p>\n<p>        painter = QPainter(self)<\/p>\n<p>        self.drawStage(painter)<\/p>\n<p>    def drawStage(self, painter):<\/p>\n<p>        # \u8bbe\u7f6e\u989c\u8272<\/p>\n<p>        painter.setBrush(QColor(173, 216, 230))  # lightblue<\/p>\n<p>        painter.drawRect(50, 50, 700, 500)<\/p>\n<p>        # \u7ed8\u5236\u706f\u5149<\/p>\n<p>        painter.setBrush(QColor(255, 0, 0))  # red<\/p>\n<p>        painter.drawEllipse(100, 100, 50, 50)<\/p>\n<p>        painter.drawEllipse(650, 100, 50, 50)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8fd0\u884c\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u5e94\u7528\u5b9e\u4f8b\uff0c\u5e76\u663e\u793a\u4e3b\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if __name__ == &quot;__main__&quot;:<\/p>\n<p>    app = QApplication(sys.argv)<\/p>\n<p>    window = StageWindow()<\/p>\n<p>    window.show()<\/p>\n<p>    sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684Python GUI\u5e93\u6765\u521b\u5efa\u548c\u663e\u793a\u821e\u53f0\u533a\u3002<strong>Tkinter<\/strong>\u9002\u5408\u7b80\u5355\u7684\u5e94\u7528\uff0c<strong>Pygame<\/strong>\u9002\u5408\u9700\u8981\u66f4\u591a\u56fe\u5f62\u548c\u5a92\u4f53\u5904\u7406\u7684\u5e94\u7528\uff0c<strong>PyQt<\/strong>\u9002\u5408\u9700\u8981\u66f4\u590d\u6742\u548c\u7f8e\u89c2\u754c\u9762\u7684\u5e94\u7528\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u6839\u636e\u9700\u6c42\u5b9e\u73b0\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u4f60\u7684\u5e94\u7528\u66f4\u52a0\u4e30\u5bcc\u548c\u751f\u52a8\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\u821e\u53f0\u533a\uff1f<\/strong><br \/>\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u821e\u53f0\u533a\u901a\u5e38\u6d89\u53ca\u4f7f\u7528\u56fe\u5f62\u5e93\uff0c\u5982Pygame\u6216Tkinter\u3002\u4f7f\u7528Pygame\u65f6\uff0c\u60a8\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u7a97\u53e3\u5e76\u5728\u5176\u4e2d\u7ed8\u5236\u821e\u53f0\u5143\u7d20\u3002\u901a\u8fc7\u5b9a\u4e49\u821e\u53f0\u7684\u5927\u5c0f\u548c\u80cc\u666f\u989c\u8272\uff0c\u5e76\u4f7f\u7528\u51fd\u6570\u6765\u7ed8\u5236\u5f62\u72b6\u6216\u89d2\u8272\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u6a21\u62df\u4e00\u4e2a\u821e\u53f0\u533a\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u4e8e\u821e\u53f0\u533a\u7684\u53ef\u89c6\u5316\uff1f<\/strong><br \/>\u6709\u8bb8\u591a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5728Python\u4e2d\u5b9e\u73b0\u821e\u53f0\u533a\u7684\u53ef\u89c6\u5316\u3002Pygame\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u9009\u62e9\uff0c\u9002\u5408\u6e38\u620f\u5f00\u53d1\u3002Tkinter\u662fPython\u81ea\u5e26\u7684\u56fe\u5f62\u754c\u9762\u5e93\uff0c\u9002\u5408\u7b80\u5355\u7684GUI\u5e94\u7528\u3002\u8fd8\u6709\u5176\u4ed6\u5e93\uff0c\u5982Matplotlib\u548cPyglet\uff0c\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u56fe\u5f62\u5904\u7406\u548c\u52a8\u753b\u6548\u679c\u3002<\/p>\n<p><strong>\u5728Python\u7684\u821e\u53f0\u533a\u4e2d\u5982\u4f55\u6dfb\u52a0\u52a8\u6001\u5143\u7d20\uff1f<\/strong><br \/>\u4e3a\u4e86\u5728\u821e\u53f0\u533a\u4e2d\u6dfb\u52a0\u52a8\u6001\u5143\u7d20\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u548c\u4e8b\u4ef6\u5904\u7406\u673a\u5236\u6765\u66f4\u65b0\u5c4f\u5e55\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u5728Pygame\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>pygame.event.get()<\/code>\u6765\u68c0\u6d4b\u7528\u6237\u8f93\u5165\uff0c\u5e76\u901a\u8fc7\u66f4\u65b0\u5bf9\u8c61\u7684\u5750\u6807\u6765\u5b9e\u73b0\u52a8\u753b\u6548\u679c\u3002\u4f7f\u7528\u5b9a\u65f6\u5668\u548c\u5e27\u7387\u63a7\u5236\uff0c\u53ef\u4ee5\u8ba9\u8fd9\u4e9b\u52a8\u6001\u5143\u7d20\u5e73\u6ed1\u5730\u79fb\u52a8\u6216\u53d8\u5316\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u663e\u793a\u821e\u53f0\u533a\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u56fe\u5f62\u5316\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\u6765\u5b9e\u73b0\uff0c\u5982Tkinter\u3001Pygame\u6216P [&hellip;]","protected":false},"author":3,"featured_media":1093445,"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\/1093434"}],"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=1093434"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1093434\/revisions"}],"predecessor-version":[{"id":1093447,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1093434\/revisions\/1093447"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1093445"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1093434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1093434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1093434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}