{"id":975201,"date":"2024-12-27T06:11:21","date_gmt":"2024-12-26T22:11:21","guid":{"rendered":""},"modified":"2024-12-27T06:11:23","modified_gmt":"2024-12-26T22:11:23","slug":"python%e5%a6%82%e4%bd%95%e5%85%b3%e9%97%adtkinter%e7%aa%97%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/975201.html","title":{"rendered":"python\u5982\u4f55\u5173\u95edtkinter\u7a97\u53e3"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200544\/a7997c2a-1939-43c1-95dc-7809cccfda93.webp\" alt=\"python\u5982\u4f55\u5173\u95edtkinter\u7a97\u53e3\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5173\u95edTkinter\u7a97\u53e3\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528<code>destroy()<\/code>\u65b9\u6cd5\u3001<code>quit()<\/code>\u65b9\u6cd5\u3001\u7ed1\u5b9a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u6216\u4f7f\u7528\u7a97\u53e3\u5173\u95ed\u6309\u94ae\u7b49\u3002<\/strong> <code>destroy()<\/code>\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684\uff0c\u5b83\u5f7b\u5e95\u9500\u6bc1\u7a97\u53e3\u53ca\u5176\u6240\u6709\u5b50\u7ec4\u4ef6\uff0c\u800c<code>quit()<\/code>\u65b9\u6cd5\u5219\u505c\u6b62\u4e3b\u4e8b\u4ef6\u5faa\u73af\uff0c\u4f46\u7a97\u53e3\u672c\u8eab\u5e76\u4e0d\u88ab\u9500\u6bc1\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>destroy()<\/code>\u65b9\u6cd5\u5173\u95ed\u7a97\u53e3<\/p>\n<\/p>\n<ol>\n<li><strong><code>destroy()<\/code>\u65b9\u6cd5\u7684\u57fa\u672c\u4f7f\u7528<\/strong><\/li>\n<\/ol>\n<p><p><code>destroy()<\/code>\u65b9\u6cd5\u662fTkinter\u4e2d\u7528\u4e8e\u5f7b\u5e95\u5173\u95ed\u7a97\u53e3\u7684\u4e3b\u8981\u65b9\u6cd5\u3002\u5f53\u4f60\u8c03\u7528<code>destroy()<\/code>\u65b9\u6cd5\u65f6\uff0c\u7a97\u53e3\u53ca\u5176\u6240\u6709\u5173\u8054\u7684\u5b50\u7ec4\u4ef6\u90fd\u4f1a\u88ab\u9500\u6bc1\u3002\u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u5728\u7a0b\u5e8f\u9700\u8981\u5b8c\u5168\u9000\u51fa\u65f6\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u5f53\u7528\u6237\u5728\u4e00\u4e2a\u5bf9\u8bdd\u6846\u4e2d\u9009\u62e9&quot;\u9000\u51fa&quot;\u9009\u9879\u65f6\uff0c\u53ef\u4ee5\u8c03\u7528<code>destroy()<\/code>\u65b9\u6cd5\u6765\u5173\u95ed\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def close_window():<\/p>\n<p>    root.destroy()<\/p>\n<p>root = tk.Tk()<\/p>\n<p>button = tk.Button(root, text=&quot;Close&quot;, command=close_window)<\/p>\n<p>button.pack()<\/p>\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\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u6309\u94ae\uff0c\u5f53\u7528\u6237\u70b9\u51fb\u8fd9\u4e2a\u6309\u94ae\u65f6\uff0c\u5c06\u8c03\u7528<code>close_window<\/code>\u51fd\u6570\uff0c\u5e76\u5728\u8be5\u51fd\u6570\u4e2d\u8c03\u7528<code>root.destroy()<\/code>\u65b9\u6cd5\u6765\u5173\u95ed\u7a97\u53e3\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5728\u591a\u7a97\u53e3\u5e94\u7528\u4e2d\u7684\u4f7f\u7528<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u591a\u7a97\u53e3\u5e94\u7528\u4e2d\uff0c<code>destroy()<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5173\u95ed\u7279\u5b9a\u7684\u7a97\u53e3\u800c\u4e0d\u5f71\u54cd\u5176\u4ed6\u7a97\u53e3\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5b50\u7a97\u53e3\uff0c\u5e76\u5728\u9700\u8981\u65f6\u8c03\u7528\u8be5\u5b50\u7a97\u53e3\u7684<code>destroy()<\/code>\u65b9\u6cd5\u4ee5\u4fbf\u4ec5\u5173\u95ed\u8be5\u5b50\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def open_child_window():<\/p>\n<p>    child = tk.Toplevel(root)<\/p>\n<p>    button = tk.Button(child, text=&quot;Close Child&quot;, command=child.destroy)<\/p>\n<p>    button.pack()<\/p>\n<p>root = tk.Tk()<\/p>\n<p>button = tk.Button(root, text=&quot;Open Child Window&quot;, command=open_child_window)<\/p>\n<p>button.pack()<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u4e3b\u7a97\u53e3\u548c\u4e00\u4e2a\u6309\u94ae\u3002\u70b9\u51fb\u6309\u94ae\u65f6\uff0c\u4f1a\u6253\u5f00\u4e00\u4e2a\u5b50\u7a97\u53e3\u3002\u5b50\u7a97\u53e3\u4e2d\u4e5f\u6709\u4e00\u4e2a\u6309\u94ae\uff0c\u5f53\u70b9\u51fb\u6b64\u6309\u94ae\u65f6\uff0c\u4ec5\u5173\u95ed\u5b50\u7a97\u53e3\u800c\u4e0d\u5f71\u54cd\u4e3b\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>quit()<\/code>\u65b9\u6cd5\u505c\u6b62\u4e8b\u4ef6\u5faa\u73af<\/p>\n<\/p>\n<ol>\n<li><strong><code>quit()<\/code>\u65b9\u6cd5\u7684\u4f5c\u7528<\/strong><\/li>\n<\/ol>\n<p><p><code>quit()<\/code>\u65b9\u6cd5\u7528\u4e8e\u505c\u6b62Tkinter\u7684\u4e3b\u4e8b\u4ef6\u5faa\u73af\u3002\u8fd9\u610f\u5473\u7740\u5f53<code>quit()<\/code>\u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\uff0c<code>mainloop()<\/code>\u5c06\u4f1a\u9000\u51fa\u3002\u7136\u800c\uff0c\u7a97\u53e3\u672c\u8eab\u5e76\u4e0d\u4f1a\u88ab\u9500\u6bc1\uff0c\u53ea\u662f\u4e8b\u4ef6\u5faa\u73af\u505c\u6b62\u4e86\u3002\u56e0\u6b64\uff0c\u901a\u5e38\u9700\u8981\u7ed3\u5408<code>destroy()<\/code>\u65b9\u6cd5\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def quit_program():<\/p>\n<p>    root.quit()<\/p>\n<p>root = tk.Tk()<\/p>\n<p>button = tk.Button(root, text=&quot;Quit&quot;, command=quit_program)<\/p>\n<p>button.pack()<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u70b9\u51fb\u6309\u94ae\u5c06\u505c\u6b62\u4e3b\u4e8b\u4ef6\u5faa\u73af\uff0c\u4f46\u7a97\u53e3\u4ecd\u7136\u5b58\u5728\u4e8e\u5c4f\u5e55\u4e0a\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5178\u578b\u4f7f\u7528\u573a\u666f<\/strong><\/li>\n<\/ol>\n<p><p><code>quit()<\/code>\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u9700\u8981\u5728\u4e0d\u7acb\u5373\u9500\u6bc1\u7a97\u53e3\u7684\u60c5\u51b5\u4e0b\u505c\u6b62\u5e94\u7528\u7a0b\u5e8f\u7684\u573a\u666f\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u5e0c\u671b\u5728\u67d0\u4e2a\u6761\u4ef6\u4e0b\u6682\u505c\u4e8b\u4ef6\u5faa\u73af\u4ee5\u6267\u884c\u67d0\u4e9b\u5904\u7406\u65f6\uff0c\u53ef\u4ee5\u8c03\u7528<code>quit()<\/code>\uff0c\u7136\u540e\u5728\u5904\u7406\u5b8c\u6210\u540e\u91cd\u65b0\u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u901a\u8fc7\u7ed1\u5b9a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u5173\u95ed\u7a97\u53e3<\/p>\n<\/p>\n<ol>\n<li><strong>\u7ed1\u5b9a<code>&lt;Destroy&gt;<\/code>\u4e8b\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>Tkinter\u5141\u8bb8\u4f60\u4e3a\u7a97\u53e3\u7684\u4e8b\u4ef6\u7ed1\u5b9a\u5904\u7406\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u7ed1\u5b9a\u7a97\u53e3\u7684<code>&lt;Destroy&gt;<\/code>\u4e8b\u4ef6\uff0c\u4ee5\u4fbf\u5728\u7a97\u53e3\u88ab\u5173\u95ed\u65f6\u6267\u884c\u7279\u5b9a\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def on_close(event):<\/p>\n<p>    print(&quot;Window is being destroyed&quot;)<\/p>\n<p>    root.quit()<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.bind(&quot;&lt;Destroy&gt;&quot;, on_close)<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53\u7a97\u53e3\u88ab\u5173\u95ed\u65f6\uff0c\u5c06\u89e6\u53d1<code>&lt;Destroy&gt;<\/code>\u4e8b\u4ef6\uff0c\u6267\u884c<code>on_close<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7ed1\u5b9a\u7a97\u53e3\u5173\u95ed\u6309\u94ae<\/strong><\/li>\n<\/ol>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u7ed1\u5b9a\u7a97\u53e3\u7684\u5173\u95ed\u6309\u94ae\uff08\u901a\u5e38\u662f\u53f3\u4e0a\u89d2\u7684\u201cX\u201d\u6309\u94ae\uff09\u6765\u6267\u884c\u81ea\u5b9a\u4e49\u64cd\u4f5c\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>protocol<\/code>\u65b9\u6cd5\u7ed1\u5b9a<code>WM_DELETE_WINDOW<\/code>\u534f\u8bae\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def on_closing():<\/p>\n<p>    print(&quot;Closing the window&quot;)<\/p>\n<p>    root.destroy()<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.protocol(&quot;WM_DELETE_WINDOW&quot;, on_closing)<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53\u7528\u6237\u70b9\u51fb\u7a97\u53e3\u7684\u5173\u95ed\u6309\u94ae\u65f6\uff0c\u5c06\u8c03\u7528<code>on_closing<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u8ba1\u65f6\u5668\u81ea\u52a8\u5173\u95ed\u7a97\u53e3<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528<code>after<\/code>\u65b9\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u6709\u65f6\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u7a97\u53e3\u5728\u7ecf\u8fc7\u4e00\u6bb5\u65f6\u95f4\u540e\u81ea\u52a8\u5173\u95ed\u3002\u53ef\u4ee5\u4f7f\u7528Tkinter\u7684<code>after<\/code>\u65b9\u6cd5\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def close_after_delay():<\/p>\n<p>    root.destroy()<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.after(5000, close_after_delay)  # 5\u79d2\u540e\u81ea\u52a8\u5173\u95ed\u7a97\u53e3<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a97\u53e3\u5c06\u57285\u79d2\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5728\u81ea\u52a8\u5316\u6d4b\u8bd5\u4e2d\u7684\u5e94\u7528<\/strong><\/li>\n<\/ol>\n<p><p>\u8fd9\u79cd\u81ea\u52a8\u5173\u95ed\u7a97\u53e3\u7684\u6280\u672f\u5728\u81ea\u52a8\u5316\u6d4b\u8bd5\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u4f60\u5728\u6d4b\u8bd5\u6267\u884c\u671f\u95f4\u81ea\u52a8\u7ba1\u7406\u7a97\u53e3\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u5916\u90e8\u4fe1\u53f7\u5173\u95edTkinter\u7a97\u53e3<\/p>\n<\/p>\n<ol>\n<li><strong>\u7ed3\u5408\u591a\u7ebf\u7a0b\u548cTkinter<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u67d0\u4e9b\u9ad8\u7ea7\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4ece\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u53d1\u51fa\u4fe1\u53f7\u4ee5\u5173\u95edTkinter\u7a97\u53e3\u3002\u8fd9\u9700\u8981\u5c0f\u5fc3\u5904\u7406\uff0c\u56e0\u4e3aTkinter\u901a\u5e38\u4e0d\u652f\u6301\u4ece\u975e\u4e3b\u7ebf\u7a0b\u76f4\u63a5\u66f4\u65b0UI\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>import threading<\/p>\n<p>import time<\/p>\n<p>def close_window_after_delay():<\/p>\n<p>    time.sleep(5)<\/p>\n<p>    root.quit()<\/p>\n<p>root = tk.Tk()<\/p>\n<p>threading.Thread(target=close_window_after_delay).start()<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5355\u72ec\u7684\u7ebf\u7a0b\u7b49\u5f855\u79d2\uff0c\u7136\u540e\u8c03\u7528<code>root.quit()<\/code>\u4ee5\u505c\u6b62\u4e3b\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4e0e\u5916\u90e8\u4e8b\u4ef6\u96c6\u6210<\/strong><\/li>\n<\/ol>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7528\u4e8e\u96c6\u6210\u5916\u90e8\u4e8b\u4ef6\u6216\u4fe1\u53f7\uff0c\u4f8b\u5982\u54cd\u5e94\u6765\u81ea\u7f51\u7edc\u3001\u786c\u4ef6\u8bbe\u5907\u6216\u5176\u4ed6\u8fdb\u7a0b\u7684\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cTkinter\u63d0\u4f9b\u4e86\u591a\u79cd\u5173\u95ed\u7a97\u53e3\u7684\u65b9\u6cd5\uff0c\u5305\u62ec<code>destroy()<\/code>\u3001<code>quit()<\/code>\u3001\u4e8b\u4ef6\u7ed1\u5b9a\u3001\u8ba1\u65f6\u5668\u548c\u591a\u7ebf\u7a0b\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u6280\u672f\uff0c\u4f60\u53ef\u4ee5\u5728\u5f00\u53d1Tkinter\u5e94\u7528\u7a0b\u5e8f\u65f6\u7075\u6d3b\u5730\u7ba1\u7406\u7a97\u53e3\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u5168\u5173\u95edTkinter\u7a97\u53e3\uff1f<\/strong><br \/>\u8981\u5b89\u5168\u5173\u95edTkinter\u7a97\u53e3\uff0c\u53ef\u4ee5\u4f7f\u7528\u7a97\u53e3\u7684<code>destroy()<\/code>\u65b9\u6cd5\u3002\u8fd9\u5c06\u7ec8\u6b62\u7a97\u53e3\u7684\u4e3b\u5faa\u73af\u5e76\u91ca\u653e\u6240\u6709\u8d44\u6e90\u3002\u53ef\u4ee5\u5728\u6309\u94ae\u7684\u547d\u4ee4\u4e2d\u8c03\u7528\u6b64\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import tkinter as tk\n\ndef close_window():\n    root.destroy()\n\nroot = tk.Tk()\nclose_button = tk.Button(root, text=&quot;\u5173\u95ed&quot;, command=close_window)\nclose_button.pack()\nroot.mainloop()\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5f53\u7528\u6237\u70b9\u51fb\u6309\u94ae\u65f6\uff0c\u7a97\u53e3\u5c31\u4f1a\u88ab\u5173\u95ed\u3002<\/p>\n<p><strong>Tkinter\u7a97\u53e3\u5173\u95ed\u540e\uff0c\u5982\u4f55\u786e\u4fdd\u7a0b\u5e8f\u5b8c\u5168\u9000\u51fa\uff1f<\/strong><br \/>\u5728\u5173\u95edTkinter\u7a97\u53e3\u540e\uff0c\u786e\u4fdd\u7a0b\u5e8f\u5b8c\u5168\u9000\u51fa\uff0c\u53ef\u4ee5\u5728\u8c03\u7528<code>destroy()<\/code>\u65b9\u6cd5\u4e4b\u540e\u4f7f\u7528<code>sys.exit()<\/code>\u3002\u8fd9\u5c06\u4f1a\u5173\u95ed\u6240\u6709\u7684Python\u8fdb\u7a0b\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import tkinter as tk\nimport sys\n\ndef close_window():\n    root.destroy()\n    sys.exit()\n\nroot = tk.Tk()\nclose_button = tk.Button(root, text=&quot;\u5173\u95ed&quot;, command=close_window)\nclose_button.pack()\nroot.mainloop()\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728\u5173\u95edTkinter\u7a97\u53e3\u65f6\u63d0\u793a\u7528\u6237\u786e\u8ba4\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u91cd\u5199\u7a97\u53e3\u7684\u5173\u95ed\u4e8b\u4ef6\u6765\u63d0\u793a\u7528\u6237\u8fdb\u884c\u786e\u8ba4\u3002\u5728Tkinter\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>protocol<\/code>\u65b9\u6cd5\u6765\u7ed1\u5b9a\u7a97\u53e3\u5173\u95ed\u4e8b\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import tkinter as tk\nfrom tkinter import messagebox\n\ndef on_closing():\n    if messagebox.askokcancel(&quot;\u9000\u51fa&quot;, &quot;\u4f60\u786e\u5b9a\u8981\u5173\u95ed\u7a97\u53e3\u5417\uff1f&quot;):\n        root.destroy()\n\nroot = tk.Tk()\nroot.protocol(&quot;WM_DELETE_WINDOW&quot;, on_closing)\nroot.mainloop()\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5f53\u7528\u6237\u5c1d\u8bd5\u5173\u95ed\u7a97\u53e3\u65f6\uff0c\u4f1a\u5f39\u51fa\u4e00\u4e2a\u786e\u8ba4\u5bf9\u8bdd\u6846\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5173\u95edTkinter\u7a97\u53e3\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528destroy()\u65b9\u6cd5\u3001quit()\u65b9\u6cd5\u3001\u7ed1\u5b9a\u4e8b\u4ef6 [&hellip;]","protected":false},"author":3,"featured_media":975207,"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\/975201"}],"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=975201"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/975201\/revisions"}],"predecessor-version":[{"id":975212,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/975201\/revisions\/975212"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/975207"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=975201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=975201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=975201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}