{"id":1080234,"date":"2025-01-08T12:28:15","date_gmt":"2025-01-08T04:28:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1080234.html"},"modified":"2025-01-08T12:28:17","modified_gmt":"2025-01-08T04:28:17","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e6%98%be%e7%a4%ba%e4%b8%80%e4%b8%aa%e5%bc%b9%e7%aa%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1080234.html","title":{"rendered":"\u5982\u4f55\u7528python\u663e\u793a\u4e00\u4e2a\u5f39\u7a97"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182855\/9d28ace2-a809-4088-8d60-7f0bbc69a9d4.webp\" alt=\"\u5982\u4f55\u7528python\u663e\u793a\u4e00\u4e2a\u5f39\u7a97\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u663e\u793a\u4e00\u4e2a\u5f39\u7a97\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\uff0c\u4f8b\u5982Tkinter\u3001PyQt\u3001wxPython\u7b49\u3002\u6700\u5e38\u7528\u7684\u662fTkinter\uff0c\u56e0\u4e3a\u5b83\u662fPython\u81ea\u5e26\u7684\u6807\u51c6GUI\u5e93\u3002\u4f7f\u7528Tkinter\u3001\u5b89\u88c5\u76f8\u5173\u5e93\u3001\u521b\u5efa\u6839\u7a97\u53e3\u3001\u663e\u793a\u6d88\u606f\u5f39\u7a97<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165Tkinter\u5e93<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Tkinter<\/h4>\n<\/p>\n<p><p>Tkinter\u662fPython\u81ea\u5e26\u7684\u5e93\uff0c\u65e0\u9700\u5355\u72ec\u5b89\u88c5\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u6807\u51c6\u7684Python\u53d1\u884c\u7248\uff0c\u90a3\u4e48Tkinter\u5df2\u7ecf\u5305\u542b\u5728\u5185\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u4e00\u4e9b\u5b9a\u5236\u7684Python\u73af\u5883\uff0c\u53ef\u80fd\u9700\u8981\u624b\u52a8\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install tk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5bfc\u5165Tkinter<\/h4>\n<\/p>\n<p><p>\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\uff0c\u5bfc\u5165Tkinter\u5e93\uff1a<\/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><h4>1\u3001\u521b\u5efa\u6839\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Tkinter\u65f6\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\uff08root window\uff09\uff0c\u8fd9\u662f\u6240\u6709\u5176\u4ed6\u7a97\u53e3\u548c\u63a7\u4ef6\u7684\u5bb9\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">root = tk.Tk()<\/p>\n<p>root.withdraw()  # \u9690\u85cf\u4e3b\u7a97\u53e3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bbe\u7f6e\u7a97\u53e3\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u7a97\u53e3\u7684\u6807\u9898\u3001\u5927\u5c0f\u3001\u4f4d\u7f6e\u7b49\u5c5e\u6027\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u5f39\u7a97\uff0c\u53ef\u4ee5\u5ffd\u7565\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">root.title(&quot;\u5f39\u7a97\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u663e\u793a\u6d88\u606f\u5f39\u7a97<\/h3>\n<\/p>\n<p><h4>1\u3001\u663e\u793a\u7b80\u5355\u6d88\u606f\u5f39\u7a97<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Tkinter\u7684messagebox\u6a21\u5757\uff0c\u53ef\u4ee5\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u6d88\u606f\u5f39\u7a97\uff0c\u4f8b\u5982\u4fe1\u606f\u3001\u8b66\u544a\u3001\u9519\u8bef\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">messagebox.showinfo(&quot;\u4fe1\u606f&quot;, &quot;\u8fd9\u662f\u4e00\u4e2a\u4fe1\u606f\u5f39\u7a97&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u663e\u793a\u8b66\u544a\u5f39\u7a97<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528messagebox.showwarning\u53ef\u4ee5\u663e\u793a\u4e00\u4e2a\u8b66\u544a\u5f39\u7a97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">messagebox.showwarning(&quot;\u8b66\u544a&quot;, &quot;\u8fd9\u662f\u4e00\u4e2a\u8b66\u544a\u5f39\u7a97&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u663e\u793a\u9519\u8bef\u5f39\u7a97<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528messagebox.showerror\u53ef\u4ee5\u663e\u793a\u4e00\u4e2a\u9519\u8bef\u5f39\u7a97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">messagebox.showerror(&quot;\u9519\u8bef&quot;, &quot;\u8fd9\u662f\u4e00\u4e2a\u9519\u8bef\u5f39\u7a97&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u663e\u793a\u8be2\u95ee\u5f39\u7a97<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528messagebox.askquestion\u53ef\u4ee5\u663e\u793a\u4e00\u4e2a\u8be2\u95ee\u5f39\u7a97\uff0c\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u201c\u662f\u201d\u6216\u201c\u5426\u201d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">response = messagebox.askquestion(&quot;\u8be2\u95ee&quot;, &quot;\u4f60\u786e\u5b9a\u8981\u7ee7\u7eed\u5417\uff1f&quot;)<\/p>\n<p>if response == &#39;yes&#39;:<\/p>\n<p>    print(&quot;\u7528\u6237\u9009\u62e9\u4e86\u662f&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u7528\u6237\u9009\u62e9\u4e86\u5426&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Tkinter\u521b\u5efa\u548c\u663e\u793a\u5404\u79cd\u7c7b\u578b\u7684\u5f39\u7a97\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.withdraw()  # \u9690\u85cf\u4e3b\u7a97\u53e3<\/p>\n<h2><strong>\u663e\u793a\u4fe1\u606f\u5f39\u7a97<\/strong><\/h2>\n<p>messagebox.showinfo(&quot;\u4fe1\u606f&quot;, &quot;\u8fd9\u662f\u4e00\u4e2a\u4fe1\u606f\u5f39\u7a97&quot;)<\/p>\n<h2><strong>\u663e\u793a\u8b66\u544a\u5f39\u7a97<\/strong><\/h2>\n<p>messagebox.showwarning(&quot;\u8b66\u544a&quot;, &quot;\u8fd9\u662f\u4e00\u4e2a\u8b66\u544a\u5f39\u7a97&quot;)<\/p>\n<h2><strong>\u663e\u793a\u9519\u8bef\u5f39\u7a97<\/strong><\/h2>\n<p>messagebox.showerror(&quot;\u9519\u8bef&quot;, &quot;\u8fd9\u662f\u4e00\u4e2a\u9519\u8bef\u5f39\u7a97&quot;)<\/p>\n<h2><strong>\u663e\u793a\u8be2\u95ee\u5f39\u7a97<\/strong><\/h2>\n<p>response = messagebox.askquestion(&quot;\u8be2\u95ee&quot;, &quot;\u4f60\u786e\u5b9a\u8981\u7ee7\u7eed\u5417\uff1f&quot;)<\/p>\n<p>if response == &#39;yes&#39;:<\/p>\n<p>    print(&quot;\u7528\u6237\u9009\u62e9\u4e86\u662f&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u7528\u6237\u9009\u62e9\u4e86\u5426&quot;)<\/p>\n<h2><strong>\u5173\u95ed\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root.destroy()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5176\u4ed6\u5e93\u663e\u793a\u5f39\u7a97<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528PyQt\u663e\u793a\u5f39\u7a97<\/h4>\n<\/p>\n<p><p>PyQt\u662f\u53e6\u4e00\u4e2a\u5e38\u7528\u7684GUI\u5e93\uff0c\u529f\u80fd\u5f3a\u5927\u4f46\u76f8\u5bf9\u590d\u6742\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528PyQt\u521b\u5efa\u548c\u663e\u793a\u4e00\u4e2a\u5f39\u7a97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QMessageBox<\/p>\n<p>app = QApplication([])<\/p>\n<h2><strong>\u521b\u5efa\u6d88\u606f\u5f39\u7a97<\/strong><\/h2>\n<p>msg = QMessageBox()<\/p>\n<p>msg.setIcon(QMessageBox.Information)<\/p>\n<p>msg.setText(&quot;\u8fd9\u662f\u4e00\u4e2a\u4fe1\u606f\u5f39\u7a97&quot;)<\/p>\n<p>msg.setWindowTitle(&quot;\u4fe1\u606f&quot;)<\/p>\n<p>msg.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528wxPython\u663e\u793a\u5f39\u7a97<\/h4>\n<\/p>\n<p><p>wxPython\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684GUI\u5e93\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import wx<\/p>\n<p>app = wx.App(False)<\/p>\n<h2><strong>\u521b\u5efa\u6d88\u606f\u5f39\u7a97<\/strong><\/h2>\n<p>wx.MessageBox(&quot;\u8fd9\u662f\u4e00\u4e2a\u4fe1\u606f\u5f39\u7a97&quot;, &quot;\u4fe1\u606f&quot;, wx.OK | wx.ICON_INFORMATION)<\/p>\n<p>app.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>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u521b\u5efa\u548c\u663e\u793a\u5404\u79cd\u7c7b\u578b\u7684\u5f39\u7a97\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u9879\u76ee\u9700\u6c42\u548c\u4e2a\u4eba\u504f\u597d\u8fdb\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u7b80\u5355\u7684\u5f39\u7a97?<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528tkinter\u5e93\u521b\u5efa\u5f39\u7a97\u3002tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u63a5\u53e3\u6765\u6784\u5efa\u56fe\u5f62\u754c\u9762\u3002\u8981\u521b\u5efa\u4e00\u4e2a\u5f39\u7a97\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165tkinter\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>messagebox<\/code>\u6765\u663e\u793a\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import tkinter as tk\nfrom tkinter import messagebox\n\n# \u521b\u5efa\u4e3b\u7a97\u53e3\nroot = tk.Tk()\nroot.withdraw()  # \u9690\u85cf\u4e3b\u7a97\u53e3\n\n# \u663e\u793a\u5f39\u7a97\nmessagebox.showinfo(&quot;\u6807\u9898&quot;, &quot;\u8fd9\u662f\u4e00\u4e2a\u5f39\u7a97\u6d88\u606f&quot;)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u540e\uff0c\u4f1a\u5f39\u51fa\u4e00\u4e2a\u5305\u542b\u6807\u9898\u548c\u4fe1\u606f\u7684\u7a97\u53e3\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u5e93\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5f39\u7a97?<\/strong><br \/>\u9664\u4e86tkinter\uff0cPython\u8fd8\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5f39\u7a97\u3002PyQt\u548cwxPython\u662f\u4e24\u4e2a\u6d41\u884c\u7684\u5e93\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u66f4\u4e3a\u4e30\u5bcc\u548c\u590d\u6742\u7684GUI\u529f\u80fd\u3002PyQt\u5141\u8bb8\u5f00\u53d1\u8005\u4f7f\u7528Qt\u6846\u67b6\uff0c\u53ef\u4ee5\u521b\u5efa\u8de8\u5e73\u53f0\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u800cwxPython\u5219\u53ef\u4ee5\u521b\u5efa\u672c\u5730\u5916\u89c2\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u9009\u62e9\u54ea\u79cd\u5e93\u53d6\u51b3\u4e8e\u9879\u76ee\u7684\u9700\u6c42\u4ee5\u53ca\u5f00\u53d1\u8005\u7684\u719f\u6089\u7a0b\u5ea6\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u5f39\u7a97\u7684\u5185\u5bb9\u548c\u6837\u5f0f?<\/strong><br \/>\u5728tkinter\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539<code>messagebox<\/code>\u7684\u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u5f39\u7a97\u7684\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u5f39\u7a97\u7684\u6807\u9898\u3001\u6d88\u606f\u5185\u5bb9\u4ee5\u53ca\u6309\u94ae\u7c7b\u578b\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u81ea\u5b9a\u4e49\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u7a97\u53e3\u7c7b\uff0c\u8c03\u6574\u5176\u5927\u5c0f\u3001\u989c\u8272\u548c\u5b57\u4f53\u7b49\u5c5e\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f39\u7a97\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import tkinter as tk\nfrom tkinter import messagebox\n\ndef custom_popup():\n    popup = tk.Toplevel()\n    popup.title(&quot;\u81ea\u5b9a\u4e49\u5f39\u7a97&quot;)\n    label = tk.Label(popup, text=&quot;\u8fd9\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f39\u7a97&quot;, padx=20, pady=20)\n    label.pack()\n    button = tk.Button(popup, text=&quot;\u5173\u95ed&quot;, command=popup.destroy)\n    button.pack()\n\nroot = tk.Tk()\nroot.withdraw()  # \u9690\u85cf\u4e3b\u7a97\u53e3\ncustom_popup()  # \u8c03\u7528\u81ea\u5b9a\u4e49\u5f39\u7a97\nroot.mainloop()\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8bbe\u8ba1\u5f39\u7a97\u7684\u5404\u79cd\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u663e\u793a\u4e00\u4e2a\u5f39\u7a97\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\uff0c\u4f8b\u5982Tkinter\u3001PyQt\u3001wxPython\u7b49\u3002\u6700\u5e38\u7528\u7684\u662fTk [&hellip;]","protected":false},"author":3,"featured_media":1080241,"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\/1080234"}],"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=1080234"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080234\/revisions"}],"predecessor-version":[{"id":1080242,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080234\/revisions\/1080242"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1080241"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1080234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1080234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1080234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}