{"id":1156966,"date":"2025-01-13T18:22:40","date_gmt":"2025-01-13T10:22:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1156966.html"},"modified":"2025-01-13T18:22:44","modified_gmt":"2025-01-13T10:22:44","slug":"python%e5%a6%82%e4%bd%95%e5%bb%b6%e6%97%b6%e5%85%b3%e9%97%admsgbox","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1156966.html","title":{"rendered":"Python\u5982\u4f55\u5ef6\u65f6\u5173\u95edMSGBOX"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25195313\/dd6059d2-81fa-4eeb-8feb-7f1c3990808a.webp\" alt=\"Python\u5982\u4f55\u5ef6\u65f6\u5173\u95edMSGBOX\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5ef6\u65f6\u5173\u95ed\u6d88\u606f\u6846\uff08MSGBOX\uff09\u7684\u5b9e\u73b0\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528tkinter\u5e93\u3001PyQt\u5e93\u6216\u5176\u4ed6GUI\u5de5\u5177\u5305\u3002\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528tkinter\u5e93\u3001\u4f7f\u7528\u7ebf\u7a0b\u3001\u4f7f\u7528\u5b9a\u65f6\u5668\u3001\u4f7f\u7528\u5176\u4ed6GUI\u5e93\u3002<\/strong>\u5176\u4e2d\uff0ctkinter\u5e93\u662fPython\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u8f83\u4e3a\u7b80\u5355\u6613\u7528\uff0c\u56e0\u6b64\u672c\u6587\u5c06\u4e3b\u8981\u4ecb\u7ecd\u4f7f\u7528tkinter\u5e93\u7ed3\u5408\u7ebf\u7a0b\u548c\u5b9a\u65f6\u5668\u6765\u5b9e\u73b0\u5ef6\u65f6\u5173\u95ed\u6d88\u606f\u6846\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528tkinter\u5e93<\/h3>\n<\/p>\n<p><p>tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u56fe\u5f62\u754c\u9762\u3002\u5728tkinter\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>tkinter.messagebox<\/code>\u6a21\u5757\u6765\u521b\u5efa\u6d88\u606f\u6846\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\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>import threading<\/p>\n<p>def show_message():<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    root.withdraw()  # \u9690\u85cf\u4e3b\u7a97\u53e3<\/p>\n<p>    messagebox.showinfo(&quot;\u63d0\u793a&quot;, &quot;\u8fd9\u662f\u4e00\u4e2a\u6d88\u606f\u6846&quot;)<\/p>\n<p>threading.Thread(target=show_message).start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u6d88\u606f\u6846\u5e76\u663e\u793a\u51fa\u6765\uff0c\u4f46\u6ca1\u6709\u5b9e\u73b0\u81ea\u52a8\u5173\u95ed\u7684\u529f\u80fd\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6dfb\u52a0\u5ef6\u65f6\u5173\u95ed\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u7ebf\u7a0b<\/h3>\n<\/p>\n<p><p>Python\u7684<code>threading<\/code>\u6a21\u5757\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5e76\u7ba1\u7406\u7ebf\u7a0b\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u7ebf\u7a0b\u6765\u5b9e\u73b0\u5ef6\u65f6\u5173\u95ed\u6d88\u606f\u6846\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u5b9e\u73b0\u6b65\u9aa4\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>import threading<\/p>\n<p>import time<\/p>\n<p>def show_message():<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    root.withdraw()  # \u9690\u85cf\u4e3b\u7a97\u53e3<\/p>\n<p>    msg_box = messagebox.showinfo(&quot;\u63d0\u793a&quot;, &quot;\u8fd9\u662f\u4e00\u4e2a\u6d88\u606f\u6846&quot;)<\/p>\n<p>    time.sleep(5)  # \u5ef6\u65f65\u79d2<\/p>\n<p>    root.destroy()  # \u5173\u95ed\u6d88\u606f\u6846<\/p>\n<p>threading.Thread(target=show_message).start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7ebf\u7a0b\u6765\u663e\u793a\u6d88\u606f\u6846\uff0c\u5e76\u57285\u79d2\u540e\u5173\u95ed\u5b83\u3002\u4f46\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\uff0c\u56e0\u4e3a<code>messagebox.showinfo<\/code>\u662f\u4e00\u4e2a\u963b\u585e\u51fd\u6570\uff0c\u5b83\u4f1a\u963b\u585e\u7ebf\u7a0b\u76f4\u5230\u7528\u6237\u5173\u95ed\u6d88\u606f\u6846\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5b9a\u65f6\u5668<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d\u963b\u585e\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>tkinter<\/code>\u4e2d\u7684\u5b9a\u65f6\u5668\u529f\u80fd\u6765\u5b9e\u73b0\u5ef6\u65f6\u5173\u95ed\u6d88\u606f\u6846\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u5b9e\u73b0\u6b65\u9aa4\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>def close_message(root):<\/p>\n<p>    root.destroy()  # \u5173\u95ed\u6d88\u606f\u6846<\/p>\n<p>def show_message():<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    root.withdraw()  # \u9690\u85cf\u4e3b\u7a97\u53e3<\/p>\n<p>    msg_box = tk.Toplevel(root)  # \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9876\u5c42\u7a97\u53e3\u4f5c\u4e3a\u6d88\u606f\u6846<\/p>\n<p>    msg_box.title(&quot;\u63d0\u793a&quot;)<\/p>\n<p>    tk.Label(msg_box, text=&quot;\u8fd9\u662f\u4e00\u4e2a\u6d88\u606f\u6846&quot;).pack()<\/p>\n<p>    msg_box.after(5000, close_message, msg_box)  # 5\u79d2\u540e\u5173\u95ed\u6d88\u606f\u6846<\/p>\n<p>    root.m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nloop()<\/p>\n<p>show_message()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>tk.Toplevel<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u9876\u5c42\u7a97\u53e3\u4f5c\u4e3a\u6d88\u606f\u6846\uff0c\u5e76\u4f7f\u7528<code>msg_box.after<\/code>\u65b9\u6cd5\u57285\u79d2\u540e\u5173\u95ed\u6d88\u606f\u6846\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u963b\u585e\u4e3b\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5176\u4ed6GUI\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86<code>tkinter<\/code>\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6GUI\u5e93\u6765\u5b9e\u73b0\u5ef6\u65f6\u5173\u95ed\u6d88\u606f\u6846\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528PyQt\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5 import QtWidgets<\/p>\n<p>import sys<\/p>\n<p>import threading<\/p>\n<p>def show_message():<\/p>\n<p>    app = QtWidgets.QApplication(sys.argv)<\/p>\n<p>    msg_box = QtWidgets.QMessageBox()<\/p>\n<p>    msg_box.setWindowTitle(&quot;\u63d0\u793a&quot;)<\/p>\n<p>    msg_box.setText(&quot;\u8fd9\u662f\u4e00\u4e2a\u6d88\u606f\u6846&quot;)<\/p>\n<p>    msg_box.show()<\/p>\n<p>    threading.Timer(5, msg_box.close).start()  # 5\u79d2\u540e\u5173\u95ed\u6d88\u606f\u6846<\/p>\n<p>    sys.exit(app.exec_())<\/p>\n<p>show_message()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86PyQt\u5e93\u6765\u521b\u5efa\u5e76\u663e\u793a\u6d88\u606f\u6846\uff0c\u5e76\u4f7f\u7528<code>threading.Timer<\/code>\u57285\u79d2\u540e\u5173\u95ed\u6d88\u606f\u6846\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u5ef6\u65f6\u5173\u95ed\u6d88\u606f\u6846\u7684\u529f\u80fd\u3002\u65e0\u8bba\u662f\u4f7f\u7528<code>tkinter<\/code>\u5e93\u3001\u7ebf\u7a0b\u3001\u5b9a\u65f6\u5668\u8fd8\u662f\u5176\u4ed6GUI\u5e93\uff0c\u90fd\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u5177\u4f53\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5ef6\u65f6\u5173\u95ed\u6d88\u606f\u6846\u7684\u529f\u80fd\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\u5ef6\u65f6\u5173\u95ed\u7684\u6d88\u606f\u6846\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528tkinter\u5e93\u6765\u521b\u5efa\u6d88\u606f\u6846\uff0c\u5e76\u7ed3\u5408time.sleep()\u51fd\u6570\u5b9e\u73b0\u5ef6\u65f6\u5173\u95ed\u7684\u6548\u679c\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6d88\u606f\u6846\uff0c\u7136\u540e\u4f7f\u7528\u4e00\u4e2a\u7ebf\u7a0b\u6765\u63a7\u5236\u5ef6\u65f6\u5173\u95ed\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import tkinter as tk\nfrom tkinter import messagebox\nimport time\nimport threading\n\ndef show_message():\n    root = tk.Tk()\n    root.withdraw()  # \u9690\u85cf\u4e3b\u7a97\u53e3\n    messagebox.showinfo(&quot;\u63d0\u793a&quot;, &quot;\u8fd9\u662f\u4e00\u4e2a\u6d88\u606f\u6846\uff01&quot;)\n    time.sleep(3)  # \u8bbe\u7f6e\u5ef6\u65f6\u5173\u95ed\u7684\u65f6\u95f4\n    root.quit()  # \u5173\u95ed\u7a97\u53e3\n\nthreading.Thread(target=show_message).start()\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5728\u6d88\u606f\u6846\u663e\u793a\u4e00\u6bb5\u65f6\u95f4\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6d88\u606f\u6846\u7684\u663e\u793a\u65f6\u95f4\uff1f<\/strong><br \/>\u5b8c\u5168\u53ef\u4ee5\u3002\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u8c03\u6574time.sleep(3)\u4e2d\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u8f7b\u677e\u81ea\u5b9a\u4e49\u6d88\u606f\u6846\u7684\u663e\u793a\u65f6\u95f4\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u53ea\u9700\u5c06\u6570\u5b57\u66ff\u6362\u4e3a\u4f60\u60f3\u8981\u7684\u79d2\u6570\u5373\u53ef\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u6d88\u606f\u6846\u4e2d\u6dfb\u52a0\u6309\u94ae\u6765\u624b\u52a8\u5173\u95ed\uff1f<\/strong><br \/>\u5728tkinter\u7684messagebox\u4e2d\uff0c\u9ed8\u8ba4\u63d0\u4f9b\u4e86\u786e\u5b9a\u548c\u53d6\u6d88\u7b49\u6309\u94ae\u3002\u4f60\u4e5f\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u7a97\u53e3\uff0c\u4f7f\u7528tkinter\u7684Button\u7ec4\u4ef6\u6765\u6dfb\u52a0\u624b\u52a8\u5173\u95ed\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">def create_custom_messagebox():\n    root = tk.Tk()\n    root.title(&quot;\u81ea\u5b9a\u4e49\u6d88\u606f\u6846&quot;)\n    \n    label = tk.Label(root, text=&quot;\u8fd9\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6d88\u606f\u6846\uff01&quot;)\n    label.pack(pady=10)\n    \n    button = tk.Button(root, text=&quot;\u5173\u95ed&quot;, command=root.quit)\n    button.pack(pady=5)\n    \n    root.after(3000, root.quit)  # 3\u79d2\u540e\u81ea\u52a8\u5173\u95ed\n    root.mainloop()\n\ncreate_custom_messagebox()\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u624b\u52a8\u5173\u95ed\u6d88\u606f\u6846\u6216\u7b49\u5f85\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5ef6\u65f6\u5173\u95ed\u6d88\u606f\u6846\uff08MSGBOX\uff09\u7684\u5b9e\u73b0\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528tkinter\u5e93\u3001PyQt\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1156977,"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\/1156966"}],"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=1156966"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156966\/revisions"}],"predecessor-version":[{"id":1156980,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156966\/revisions\/1156980"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1156977"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1156966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1156966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1156966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}