{"id":1178768,"date":"2025-01-15T18:14:52","date_gmt":"2025-01-15T10:14:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1178768.html"},"modified":"2025-01-15T18:14:55","modified_gmt":"2025-01-15T10:14:55","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%86%99%e8%a1%a8%e7%99%bd%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1178768.html","title":{"rendered":"\u5982\u4f55\u7528python\u5199\u8868\u767d\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25113340\/02c07c4f-d565-44dd-bcf6-4d5c5be44c20.webp\" alt=\"\u5982\u4f55\u7528python\u5199\u8868\u767d\u7a0b\u5e8f\" \/><\/p>\n<p><p> \u7528Python\u5199\u8868\u767d\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684GUI\u5e93\uff08\u5982Tkinter\uff09\u6216\u8005\u63a7\u5236\u53f0\u8f93\u51fa\u6765\u5b9e\u73b0\u3002<strong>\u5229\u7528Tkinter\u5e93\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u754c\u9762\u3001\u4f7f\u7528\u63a7\u5236\u53f0\u8f93\u51fa\u8868\u767d\u4fe1\u606f\u3001\u6dfb\u52a0\u4e92\u52a8\u5143\u7d20<\/strong>\uff0c\u8fd9\u4e9b\u90fd\u662f\u6709\u6548\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Tkinter\u5e93\u6765\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u8868\u767d\u7a0b\u5e8f\uff0c\u5e76\u4e14\u6dfb\u52a0\u4e00\u4e9b\u4e92\u52a8\u5143\u7d20\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u66f4\u52a0\u751f\u52a8\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165Tkinter\u5e93<\/h3>\n<\/p>\n<p><p>Tkinter\u662fPython\u81ea\u5e26\u7684\u6807\u51c6GUI\u5e93\uff0c\u56e0\u6b64\u65e0\u9700\u5b89\u88c5\u3002\u6211\u4eec\u53ea\u9700\u8981\u5728\u4ee3\u7801\u5f00\u59cb\u90e8\u5206\u5bfc\u5165\u5b83\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import messagebox<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u4e3b\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\u6765\u663e\u793a\u6211\u4eec\u7684\u8868\u767d\u5185\u5bb9\u3002\u53ef\u4ee5\u901a\u8fc7<code>tk.Tk()<\/code>\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">root = tk.Tk()<\/p>\n<p>root.title(&quot;\u8868\u767d\u7a0b\u5e8f&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6dfb\u52a0\u6807\u7b7e\u548c\u6309\u94ae<\/h3>\n<\/p>\n<p><p>\u5728\u4e3b\u7a97\u53e3\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u6807\u7b7e\u548c\u6309\u94ae\uff0c\u4ee5\u4fbf\u7528\u6237\u53ef\u4ee5\u4e0e\u7a0b\u5e8f\u4e92\u52a8\u3002\u53ef\u4ee5\u4f7f\u7528<code>tk.Label<\/code>\u548c<code>tk.Button<\/code>\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">label = tk.Label(root, text=&quot;\u4f60\u613f\u610f\u505a\u6211\u7684\u5973\u670b\u53cb\u5417\uff1f&quot;, font=(&quot;Arial&quot;, 14))<\/p>\n<p>label.pack(pady=20)<\/p>\n<p>def on_yes():<\/p>\n<p>    messagebox.showinfo(&quot;\u8868\u767d\u6210\u529f&quot;, &quot;\u592a\u597d\u4e86\uff01\u6211\u4e5f\u559c\u6b22\u4f60\uff01&quot;)<\/p>\n<p>def on_no():<\/p>\n<p>    messagebox.showwarning(&quot;\u8868\u767d\u5931\u8d25&quot;, &quot;\u518d\u7ed9\u6211\u4e00\u6b21\u673a\u4f1a\u5427\uff01&quot;)<\/p>\n<p>yes_button = tk.Button(root, text=&quot;\u613f\u610f&quot;, command=on_yes)<\/p>\n<p>yes_button.pack(side=&quot;left&quot;, padx=20)<\/p>\n<p>no_button = tk.Button(root, text=&quot;\u4e0d\u613f\u610f&quot;, command=on_no)<\/p>\n<p>no_button.pack(side=&quot;right&quot;, padx=20)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8fd0\u884c\u4e3b\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u8fd0\u884c\u4e3b\u5faa\u73af\u4f7f\u5f97\u7a97\u53e3\u4fdd\u6301\u663e\u793a\u72b6\u6001\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>\u5b8c\u6574\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import messagebox<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;\u8868\u767d\u7a0b\u5e8f&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<p>label = tk.Label(root, text=&quot;\u4f60\u613f\u610f\u505a\u6211\u7684\u5973\u670b\u53cb\u5417\uff1f&quot;, font=(&quot;Arial&quot;, 14))<\/p>\n<p>label.pack(pady=20)<\/p>\n<p>def on_yes():<\/p>\n<p>    messagebox.showinfo(&quot;\u8868\u767d\u6210\u529f&quot;, &quot;\u592a\u597d\u4e86\uff01\u6211\u4e5f\u559c\u6b22\u4f60\uff01&quot;)<\/p>\n<p>def on_no():<\/p>\n<p>    messagebox.showwarning(&quot;\u8868\u767d\u5931\u8d25&quot;, &quot;\u518d\u7ed9\u6211\u4e00\u6b21\u673a\u4f1a\u5427\uff01&quot;)<\/p>\n<p>yes_button = tk.Button(root, text=&quot;\u613f\u610f&quot;, command=on_yes)<\/p>\n<p>yes_button.pack(side=&quot;left&quot;, padx=20)<\/p>\n<p>no_button = tk.Button(root, text=&quot;\u4e0d\u613f\u610f&quot;, command=on_no)<\/p>\n<p>no_button.pack(side=&quot;right&quot;, padx=20)<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u589e\u5f3a\u529f\u80fd<\/h3>\n<\/p>\n<p><h4>\u6dfb\u52a0\u80cc\u666f\u97f3\u4e50<\/h4>\n<\/p>\n<p><p>\u4e3a\u7a0b\u5e8f\u6dfb\u52a0\u80cc\u666f\u97f3\u4e50\u53ef\u4ee5\u4f7f\u8868\u767d\u66f4\u52a0\u6d6a\u6f2b\u3002\u53ef\u4ee5\u4f7f\u7528<code>pygame<\/code>\u5e93\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pygame<\/p>\n<p>pygame.mixer.init()<\/p>\n<p>pygame.mixer.music.load(&quot;background_music.mp3&quot;)<\/p>\n<p>pygame.mixer.music.play(-1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06\u4e0a\u8ff0\u4ee3\u7801\u6dfb\u52a0\u5230\u4e3b\u7a97\u53e3\u521b\u5efa\u4e4b\u524d\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>\u6dfb\u52a0\u56fe\u7247<\/h4>\n<\/p>\n<p><p>\u5728\u8868\u767d\u7a97\u53e3\u4e2d\u6dfb\u52a0\u56fe\u7247\u53ef\u4ee5\u589e\u52a0\u89c6\u89c9\u6548\u679c\u3002\u53ef\u4ee5\u4f7f\u7528<code>tk.PhotoImage<\/code>\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">img = tk.PhotoImage(file=&quot;love_image.png&quot;)<\/p>\n<p>img_label = tk.Label(root, image=img)<\/p>\n<p>img_label.pack(pady=10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06\u4e0a\u8ff0\u4ee3\u7801\u6dfb\u52a0\u5230\u6807\u7b7e\u548c\u6309\u94ae\u521b\u5efa\u4e4b\u524d\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>\u6dfb\u52a0\u66f4\u591a\u4e92\u52a8\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u6dfb\u52a0\u66f4\u591a\u7684\u4e92\u52a8\u5143\u7d20\uff0c\u4f8b\u5982\u8f93\u5165\u6846\u3001\u590d\u9009\u6846\u7b49\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u66f4\u52a0\u751f\u52a8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">entry = tk.Entry(root, font=(&quot;Arial&quot;, 12))<\/p>\n<p>entry.pack(pady=10)<\/p>\n<p>def on_submit():<\/p>\n<p>    name = entry.get()<\/p>\n<p>    messagebox.showinfo(&quot;\u8868\u767d&quot;, f&quot;\u6211\u559c\u6b22\u4f60\uff0c{name}\uff01&quot;)<\/p>\n<p>submit_button = tk.Button(root, text=&quot;\u63d0\u4ea4&quot;, command=on_submit)<\/p>\n<p>submit_button.pack(pady=5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u4f46\u529f\u80fd\u4e30\u5bcc\u7684\u8868\u767d\u7a0b\u5e8f\u3002<strong>\u5229\u7528Tkinter\u5e93\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u754c\u9762\u3001\u6dfb\u52a0\u4e92\u52a8\u5143\u7d20\u3001\u4e3a\u7a0b\u5e8f\u6dfb\u52a0\u80cc\u666f\u97f3\u4e50\u548c\u56fe\u7247<\/strong>\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u66f4\u52a0\u751f\u52a8\u3002\u8fd9\u4e9b\u6280\u5de7\u4e0d\u4ec5\u9002\u7528\u4e8e\u8868\u767d\u7a0b\u5e8f\uff0c\u4e5f\u53ef\u4ee5\u5e94\u7528\u5230\u5176\u4ed6\u9700\u8981\u56fe\u5f62\u754c\u9762\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002\u5e0c\u671b\u4ee5\u4e0a\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u4f60\u8868\u767d\u6210\u529f\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u8868\u767d\u7a0b\u5e8f\uff1f<\/strong><br \/>\u8981\u5b9e\u73b0\u4e00\u4e2a\u8868\u767d\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u57fa\u672c\u8f93\u5165\u8f93\u51fa\u529f\u80fd\uff0c\u4ee5\u53ca\u4e00\u4e9b\u6709\u8da3\u7684\u6587\u672c\u6548\u679c\u3002\u6bd4\u5982\uff0c\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u8f93\u51fa\u6d6a\u6f2b\u7684\u8bdd\u8bed\uff0c\u6216\u8005\u8ba9\u7a0b\u5e8f\u63a5\u53d7\u7528\u6237\u7684\u8f93\u5165\uff0c\u751f\u6210\u4e2a\u6027\u5316\u7684\u8868\u767d\u5185\u5bb9\u3002\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u6765\u589e\u52a0\u7a0b\u5e8f\u7684\u4e92\u52a8\u6027\uff0c\u8ba9\u7528\u6237\u9009\u62e9\u4e0d\u540c\u7684\u8868\u767d\u65b9\u5f0f\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9bPython\u5e93\u53ef\u4ee5\u589e\u5f3a\u8868\u767d\u7a0b\u5e8f\u7684\u8da3\u5473\u6027\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>time<\/code>\u5e93\u6765\u6dfb\u52a0\u5ef6\u65f6\u6548\u679c\uff0c\u8ba9\u8868\u767d\u8fc7\u7a0b\u66f4\u52a0\u7f13\u6162\u548c\u6d6a\u6f2b\u3002<code>random<\/code>\u5e93\u5219\u53ef\u4ee5\u7528\u6765\u968f\u673a\u9009\u62e9\u8868\u767d\u7684\u53e5\u5b50\u6216\u98ce\u683c\uff0c\u589e\u52a0\u610f\u5916\u60ca\u559c\u3002\u82e5\u60f3\u8ba9\u7a0b\u5e8f\u7684\u8f93\u51fa\u66f4\u52a0\u7f8e\u89c2\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>colorama<\/code>\u5e93\u4e3a\u6587\u672c\u6dfb\u52a0\u989c\u8272\u3002<\/p>\n<p><strong>\u5982\u4f55\u8ba9\u8868\u767d\u7a0b\u5e8f\u652f\u6301\u591a\u79cd\u8bed\u8a00\u8f93\u51fa\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5b57\u5178\u7ed3\u6784\u6765\u5b58\u50a8\u4e0d\u540c\u8bed\u8a00\u7684\u8868\u767d\u53e5\u5b50\u3002\u6839\u636e\u7528\u6237\u7684\u8bed\u8a00\u9009\u62e9\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u8f93\u51fa\u76f8\u5e94\u7684\u8bed\u8a00\u5185\u5bb9\u3002\u4e3a\u4e86\u5b9e\u73b0\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\uff0c\u5efa\u8bae\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u8ba9\u7528\u6237\u9009\u62e9\u4ed6\u4eec\u7684\u8bed\u8a00\u504f\u597d\uff0c\u4ece\u800c\u63d0\u4f9b\u4e2a\u6027\u5316\u7684\u8868\u767d\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u5199\u8868\u767d\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684GUI\u5e93\uff08\u5982Tkinter\uff09\u6216\u8005\u63a7\u5236\u53f0\u8f93\u51fa\u6765\u5b9e\u73b0\u3002\u5229\u7528Tkinter\u5e93\u521b [&hellip;]","protected":false},"author":3,"featured_media":1178773,"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\/1178768"}],"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=1178768"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178768\/revisions"}],"predecessor-version":[{"id":1178774,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178768\/revisions\/1178774"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1178773"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1178768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1178768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1178768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}