{"id":1113061,"date":"2025-01-08T17:44:16","date_gmt":"2025-01-08T09:44:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1113061.html"},"modified":"2025-01-08T17:44:18","modified_gmt":"2025-01-08T09:44:18","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%86%99%e4%b8%80%e6%ae%b5%e8%a1%a8%e7%99%bd%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1113061.html","title":{"rendered":"\u5982\u4f55\u7528python\u5199\u4e00\u6bb5\u8868\u767d\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074834\/9e9c5b73-8319-41bc-a54c-18c67fe4051a.webp\" alt=\"\u5982\u4f55\u7528python\u5199\u4e00\u6bb5\u8868\u767d\u4ee3\u7801\" \/><\/p>\n<p><p> <strong>\u7528Python\u5199\u8868\u767d\u4ee3\u7801\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528print\u51fd\u6570\u8f93\u51fa\u8868\u767d\u4fe1\u606f\u3001\u8bbe\u8ba1\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62\u754c\u9762\u3001\u4f7f\u7528\u52a8\u753b\u6548\u679c\u6765\u5c55\u793a\u8868\u767d\u3001\u521b\u5efa\u4e00\u4e2a\u4e92\u52a8\u5c0f\u6e38\u620f\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7Python\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\u4e2d\u7684\u4e00\u79cd\uff1a\u8bbe\u8ba1\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62\u754c\u9762\u6765\u5c55\u793a\u8868\u767d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528print\u51fd\u6570\u8f93\u51fa\u8868\u767d\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u4f7f\u7528Python\u7684print\u51fd\u6570\u8f93\u51fa\u8868\u767d\u4fe1\u606f\u3002\u8fd9\u79cd\u65b9\u5f0f\u867d\u7136\u7b80\u5355\uff0c\u4f46\u6548\u679c\u76f4\u63a5\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;\u4eb2\u7231\u7684\uff0c\u6211\u559c\u6b22\u4f60\uff01&quot;)<\/p>\n<p>print(&quot;\u613f\u610f\u505a\u6211\u7684\u5973\u670b\u53cb\u5417\uff1f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbe\u8ba1\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62\u754c\u9762<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u56fe\u5f62\u754c\u9762\u53ef\u4ee5\u8ba9\u8868\u767d\u66f4\u52a0\u751f\u52a8\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684Tkinter\u5e93\u6765\u8bbe\u8ba1\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62\u754c\u9762\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import messagebox<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;\u8868\u767d&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6807\u7b7e<\/strong><\/h2>\n<p>label = tk.Label(root, text=&quot;\u4eb2\u7231\u7684\uff0c\u6211\u559c\u6b22\u4f60\uff01&quot;, font=(&quot;Arial&quot;, 14))<\/p>\n<p>label.pack(pady=20)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6309\u94ae<\/strong><\/h2>\n<p>def confess():<\/p>\n<p>    messagebox.showinfo(&quot;\u8868\u767d&quot;, &quot;\u613f\u610f\u505a\u6211\u7684\u5973\u670b\u53cb\u5417\uff1f&quot;)<\/p>\n<p>button = tk.Button(root, text=&quot;\u8868\u767d&quot;, command=confess, font=(&quot;Arial&quot;, 12))<\/p>\n<p>button.pack(pady=20)<\/p>\n<h2><strong>\u8fdb\u5165\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\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Tkinter\u5e93\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u7a97\u53e3\uff0c\u5e76\u5728\u7a97\u53e3\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6807\u7b7e\u548c\u4e00\u4e2a\u6309\u94ae\u3002\u5f53\u7528\u6237\u70b9\u51fb\u6309\u94ae\u65f6\uff0c\u4f1a\u5f39\u51fa\u4e00\u4e2a\u6d88\u606f\u6846\u663e\u793a\u8868\u767d\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u52a8\u753b\u6548\u679c\u6765\u5c55\u793a\u8868\u767d<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528Pygame\u5e93\u6765\u5236\u4f5c\u4e00\u4e2a\u7b80\u5355\u7684\u52a8\u753b\u6548\u679c\u6765\u5c55\u793a\u8868\u767d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/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>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/strong><\/h2>\n<p>screen = pygame.display.set_mode((800, 600))<\/p>\n<p>pygame.display.set_caption(&#39;\u8868\u767d&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b57\u4f53\u548c\u989c\u8272<\/strong><\/h2>\n<p>font = pygame.font.Font(None, 74)<\/p>\n<p>color = (255, 0, 0)<\/p>\n<h2><strong>\u8bbe\u7f6e\u8868\u767d\u6587\u5b57<\/strong><\/h2>\n<p>text = &quot;\u4eb2\u7231\u7684\uff0c\u6211\u559c\u6b22\u4f60\uff01&quot;<\/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>    # \u6e05\u5c4f<\/p>\n<p>    screen.fill((255, 255, 255))<\/p>\n<p>    # \u6e32\u67d3\u6587\u5b57<\/p>\n<p>    text_surface = font.render(text, True, color)<\/p>\n<p>    screen.blit(text_surface, (100, 250))<\/p>\n<p>    # \u66f4\u65b0\u5c4f\u5e55<\/p>\n<p>    pygame.display.flip()<\/p>\n<h2><strong>\u9000\u51faPygame<\/strong><\/h2>\n<p>pygame.quit()<\/p>\n<p>sys.exit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pygame\u5e93\u521b\u5efa\u4e86\u4e00\u4e2a\u7a97\u53e3\uff0c\u5e76\u5728\u7a97\u53e3\u4e2d\u6e32\u67d3\u4e86\u8868\u767d\u6587\u5b57\u3002\u867d\u7136\u8fd9\u4e2a\u793a\u4f8b\u6ca1\u6709\u590d\u6742\u7684\u52a8\u753b\u6548\u679c\uff0c\u4f46\u5df2\u7ecf\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Pygame\u8fdb\u884c\u56fe\u5f62\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u521b\u5efa\u4e00\u4e2a\u4e92\u52a8\u5c0f\u6e38\u620f<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u4e92\u52a8\u5c0f\u6e38\u620f\u6765\u589e\u52a0\u8868\u767d\u7684\u8da3\u5473\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>import tkinter as tk<\/p>\n<p>from tkinter import messagebox<\/p>\n<p>class ConfessionGame:<\/p>\n<p>    def __init__(self, root):<\/p>\n<p>        self.root = root<\/p>\n<p>        self.root.title(&quot;\u8868\u767d\u5c0f\u6e38\u620f&quot;)<\/p>\n<p>        self.root.geometry(&quot;400x300&quot;)<\/p>\n<p>        self.label = tk.Label(root, text=&quot;\u4f60\u559c\u6b22\u6211\u5417\uff1f&quot;, font=(&quot;Arial&quot;, 16))<\/p>\n<p>        self.label.pack(pady=20)<\/p>\n<p>        self.yes_button = tk.Button(root, text=&quot;\u559c\u6b22&quot;, command=self.yes, font=(&quot;Arial&quot;, 12))<\/p>\n<p>        self.yes_button.pack(side=tk.LEFT, padx=50)<\/p>\n<p>        self.no_button = tk.Button(root, text=&quot;\u4e0d\u559c\u6b22&quot;, command=self.no, font=(&quot;Arial&quot;, 12))<\/p>\n<p>        self.no_button.pack(side=tk.RIGHT, padx=50)<\/p>\n<p>    def yes(self):<\/p>\n<p>        messagebox.showinfo(&quot;\u8868\u767d&quot;, &quot;\u6211\u4e5f\u559c\u6b22\u4f60\uff01&quot;)<\/p>\n<p>    def no(self):<\/p>\n<p>        x = random.randint(0, 300)<\/p>\n<p>        y = random.randint(0, 200)<\/p>\n<p>        self.no_button.place(x=x, y=y)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    game = ConfessionGame(root)<\/p>\n<p>    root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u4e92\u52a8\u5c0f\u6e38\u620f\u3002\u5f53\u7528\u6237\u70b9\u51fb\u201c\u4e0d\u559c\u6b22\u201d\u6309\u94ae\u65f6\uff0c\u8be5\u6309\u94ae\u4f1a\u968f\u673a\u79fb\u52a8\u4f4d\u7f6e\uff0c\u589e\u52a0\u4e86\u8868\u767d\u7684\u8da3\u5473\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u5b9e\u73b0\u4e0d\u540c\u5f62\u5f0f\u7684\u8868\u767d\u4ee3\u7801\u3002<strong>\u4f7f\u7528print\u51fd\u6570\u8f93\u51fa\u8868\u767d\u4fe1\u606f\u3001\u8bbe\u8ba1\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62\u754c\u9762\u3001\u4f7f\u7528\u52a8\u753b\u6548\u679c\u6765\u5c55\u793a\u8868\u767d\u3001\u521b\u5efa\u4e00\u4e2a\u4e92\u52a8\u5c0f\u6e38\u620f<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u603b\u7684\u6765\u8bf4\uff0c\u5229\u7528Python\u7f16\u5199\u8868\u767d\u4ee3\u7801\u662f\u4e00\u79cd\u6709\u8da3\u4e14\u5bcc\u6709\u521b\u610f\u7684\u65b9\u5f0f\uff0c\u5e0c\u671b\u8fd9\u4e9b\u793a\u4f8b\u4ee3\u7801\u80fd\u591f\u5e2e\u52a9\u4f60\u5b9e\u73b0\u5fc3\u4e2d\u7684\u8868\u767d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u8868\u767d\u7a0b\u5e8f\uff1f<\/strong><br \/>\u8981\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u8868\u767d\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u57fa\u672c\u7684Python\u8bed\u6cd5\u548c\u8f93\u5165\u8f93\u51fa\u529f\u80fd\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u7684\u540d\u5b57\u548c\u4e00\u4e9b\u751c\u871c\u7684\u6d88\u606f\uff0c\u7136\u540e\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u5c55\u793a\u8fd9\u4e9b\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">name = input(&quot;\u8bf7\u8f93\u5165\u4f60\u559c\u6b22\u7684\u4eba\u7684\u540d\u5b57\uff1a&quot;)\nmessage = &quot;\u6211\u559c\u6b22\u4f60\uff0c&quot; + name + &quot;\uff01\u5e0c\u671b\u4f60\u80fd\u548c\u6211\u5728\u4e00\u8d77\u3002&quot;\nprint(message)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u7a0b\u5e8f\u4f1a\u8ba9\u7528\u6237\u8f93\u5165\u4ed6\u4eec\u559c\u6b22\u7684\u4eba\u7684\u540d\u5b57\uff0c\u5e76\u8f93\u51fa\u4e00\u6bb5\u751c\u871c\u7684\u8868\u767d\u8bed\u3002<\/p>\n<p><strong>\u7528Python\u8868\u767d\u65f6\uff0c\u5982\u4f55\u7f8e\u5316\u8f93\u51fa\u6548\u679c\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528ANSI\u8f6c\u4e49\u7801\u6765\u6539\u53d8\u6587\u672c\u7684\u989c\u8272\u548c\u6837\u5f0f\uff0c\u589e\u52a0\u8868\u767d\u7684\u8da3\u5473\u6027\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u8ba9\u6587\u5b57\u53d8\u6210\u7ea2\u8272\u6216\u8005\u52a0\u7c97\uff0c\u5438\u5f15\u5bf9\u65b9\u7684\u6ce8\u610f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">print(&quot;\\033[91m&quot; + &quot;\u6211\u559c\u6b22\u4f60\uff0c&quot; + name + &quot;\uff01&quot; + &quot;\\033[0m&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u4f60\u7684\u8868\u767d\u66f4\u52a0\u751f\u52a8\u548c\u5f15\u4eba\u6ce8\u76ee\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u6dfb\u52a0\u968f\u673a\u5143\u7d20\u5230\u8868\u767d\u7a0b\u5e8f\u4e2d\uff1f<\/strong><br \/>\u53ef\u4ee5\u5229\u7528Python\u7684<code>random<\/code>\u6a21\u5757\uff0c\u6dfb\u52a0\u4e00\u4e9b\u968f\u673a\u7684\u8868\u767d\u8bed\u53e5\uff0c\u4f7f\u6bcf\u6b21\u8868\u767d\u90fd\u72ec\u4e00\u65e0\u4e8c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import random\n\nmessages = [\n    &quot;\u4f60\u662f\u6211\u5fc3\u4e2d\u7684\u552f\u4e00\u3002&quot;,\n    &quot;\u6bcf\u6b21\u770b\u5230\u4f60\uff0c\u6211\u7684\u5fc3\u90fd\u4f1a\u8df3\u52a8\u5f97\u66f4\u5feb\u3002&quot;,\n    &quot;\u4e0e\u4f60\u5728\u4e00\u8d77\u7684\u65f6\u5149\u662f\u6211\u6700\u5feb\u4e50\u7684\u65f6\u5149\u3002&quot;,\n]\n\nprint(random.choice(messages) + &quot; &quot; + name + &quot;\uff01&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u968f\u673a\u9009\u62e9\u4e00\u6761\u8868\u767d\u4fe1\u606f\uff0c\u8ba9\u4f60\u7684\u8868\u767d\u66f4\u5177\u521b\u610f\u548c\u60ca\u559c\u611f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u5199\u8868\u767d\u4ee3\u7801\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528print\u51fd\u6570\u8f93\u51fa\u8868\u767d\u4fe1\u606f\u3001\u8bbe\u8ba1\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62\u754c\u9762\u3001\u4f7f\u7528\u52a8\u753b\u6548\u679c\u6765\u5c55\u793a [&hellip;]","protected":false},"author":3,"featured_media":1113066,"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\/1113061"}],"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=1113061"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113061\/revisions"}],"predecessor-version":[{"id":1113067,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113061\/revisions\/1113067"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1113066"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1113061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1113061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1113061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}