{"id":1057478,"date":"2024-12-31T15:08:27","date_gmt":"2024-12-31T07:08:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1057478.html"},"modified":"2024-12-31T15:08:30","modified_gmt":"2024-12-31T07:08:30","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e8%be%93%e5%87%ba%e6%97%b6%e9%97%b4%e6%94%be%e5%88%b0%e7%95%8c%e9%9d%a2%e4%b8%8a","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1057478.html","title":{"rendered":"python\u5982\u4f55\u628a\u8f93\u51fa\u65f6\u95f4\u653e\u5230\u754c\u9762\u4e0a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/baed34db-9112-42e1-a0fb-8a4a49436a62.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u628a\u8f93\u51fa\u65f6\u95f4\u653e\u5230\u754c\u9762\u4e0a\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u7f16\u5199\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e76\u5c06\u8f93\u51fa\u65f6\u95f4\u663e\u793a\u5728\u754c\u9762\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528tkinter\u5e93\u3001PyQt\u5e93\u3001Kivy\u5e93\u7b49\u3002\u672c\u6587\u5c06\u4ee5tkinter\u5e93\u4e3a\u4f8b\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5c06\u8f93\u51fa\u65f6\u95f4\u653e\u5230\u754c\u9762\u4e0a\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u901a\u8fc7tkinter\u5e93\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684GUI\u3001\u4f7f\u7528Label\u5c0f\u90e8\u4ef6\u663e\u793a\u65f6\u95f4\u3001\u4f7f\u7528after\u65b9\u6cd5\u5b9a\u671f\u66f4\u65b0\u65f6\u95f4\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528tkinter\u5e93\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684GUI<\/p>\n<p>\u8981\u5728Python\u4e2d\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\uff0ctkinter\u662f\u4e00\u4e2a\u5e38\u7528\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u5e93\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165tkinter\u5e93\u5e76\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u7a97\u53e3\u3002\u4ee5\u4e0b\u662f\u521b\u5efa\u4e00\u4e2a\u57fa\u672cGUI\u7a97\u53e3\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;\u65f6\u95f4\u663e\u793a\u5668&quot;)<\/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>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684tkinter\u7a97\u53e3\uff0c\u5e76\u8bbe\u7f6e\u7a97\u53e3\u7684\u6807\u9898\u4e3a\u201c\u65f6\u95f4\u663e\u793a\u5668\u201d\u3002\u8c03\u7528<code>root.mainloop()<\/code>\u65b9\u6cd5\u5c06\u542f\u52a8tkinter\u7684\u4e3b\u4e8b\u4ef6\u5faa\u73af\uff0c\u4f7f\u7a97\u53e3\u4fdd\u6301\u6253\u5f00\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528Label\u5c0f\u90e8\u4ef6\u663e\u793a\u65f6\u95f4<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5728\u7a97\u53e3\u4e2d\u6dfb\u52a0\u4e00\u4e2aLabel\u5c0f\u90e8\u4ef6\u6765\u663e\u793a\u5f53\u524d\u65f6\u95f4\u3002Label\u5c0f\u90e8\u4ef6\u7528\u4e8e\u663e\u793a\u6587\u672c\u6216\u56fe\u50cf\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u5b83\u6765\u663e\u793a\u5f53\u524d\u65f6\u95f4\u3002\u4ee5\u4e0b\u662f\u6dfb\u52a0Label\u5c0f\u90e8\u4ef6\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>import time<\/p>\n<p>def update_time():<\/p>\n<p>    current_time = time.strftime(&quot;%H:%M:%S&quot;)<\/p>\n<p>    label.config(text=current_time)<\/p>\n<p>    root.after(1000, update_time)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;\u65f6\u95f4\u663e\u793a\u5668&quot;)<\/p>\n<h2><strong>\u521b\u5efaLabel\u5c0f\u90e8\u4ef6<\/strong><\/h2>\n<p>label = tk.Label(root, font=(&quot;Helvetica&quot;, 48))<\/p>\n<p>label.pack()<\/p>\n<h2><strong>\u8c03\u7528update_time\u51fd\u6570\u6765\u66f4\u65b0\u65f6\u95f4<\/strong><\/h2>\n<p>update_time()<\/p>\n<h2><strong>\u8fdb\u5165\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>update_time<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5e76\u66f4\u65b0Label\u5c0f\u90e8\u4ef6\u7684\u6587\u672c\u3002<code>time.strftime(&quot;%H:%M:%S&quot;)<\/code>\u7528\u4e8e\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002\u6211\u4eec\u4f7f\u7528<code>label.config(text=current_time)<\/code>\u6765\u66f4\u65b0Label\u5c0f\u90e8\u4ef6\u7684\u6587\u672c\u5185\u5bb9\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>root.after(1000, update_time)<\/code>\u65b9\u6cd5\u6bcf\u96941000\u6beb\u79d2\uff08\u53731\u79d2\uff09\u8c03\u7528\u4e00\u6b21<code>update_time<\/code>\u51fd\u6570\uff0c\u4ee5\u5b9e\u73b0\u65f6\u95f4\u7684\u81ea\u52a8\u66f4\u65b0\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528after\u65b9\u6cd5\u5b9a\u671f\u66f4\u65b0\u65f6\u95f4<\/p>\n<p><code>root.after(ms, func)<\/code>\u65b9\u6cd5\u662ftkinter\u5e93\u4e2d\u7528\u4e8e\u5ef6\u8fdf\u6267\u884c\u67d0\u4e2a\u51fd\u6570\u7684\u65b9\u6cd5\u3002\u5b83\u5c06\u5728\u6307\u5b9a\u7684\u6beb\u79d2\u6570\uff08ms\uff09\u540e\u8c03\u7528\u6307\u5b9a\u7684\u51fd\u6570\uff08func\uff09\u3002\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>root.after(1000, update_time)<\/code>\u65b9\u6cd5\u6bcf\u96941000\u6beb\u79d2\u8c03\u7528\u4e00\u6b21<code>update_time<\/code>\u51fd\u6570\uff0c\u4ee5\u5b9e\u73b0\u6bcf\u79d2\u66f4\u65b0\u4e00\u6b21\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5b8c\u5584\u754c\u9762\u548c\u529f\u80fd<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5b8c\u5584\u754c\u9762\u548c\u529f\u80fd\uff0c\u4f8b\u5982\u6dfb\u52a0\u6309\u94ae\u6765\u63a7\u5236\u65f6\u95f4\u7684\u663e\u793a\u3001\u8bbe\u7f6e\u4e0d\u540c\u7684\u65f6\u95f4\u683c\u5f0f\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u66f4\u4e3a\u5b8c\u5584\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>import time<\/p>\n<p>def update_time():<\/p>\n<p>    current_time = time.strftime(time_format)<\/p>\n<p>    label.config(text=current_time)<\/p>\n<p>    root.after(1000, update_time)<\/p>\n<p>def toggle_time_format():<\/p>\n<p>    global time_format<\/p>\n<p>    if time_format == &quot;%H:%M:%S&quot;:<\/p>\n<p>        time_format = &quot;%I:%M:%S %p&quot;<\/p>\n<p>    else:<\/p>\n<p>        time_format = &quot;%H:%M:%S&quot;<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;\u65f6\u95f4\u663e\u793a\u5668&quot;)<\/p>\n<h2><strong>\u521b\u5efaLabel\u5c0f\u90e8\u4ef6<\/strong><\/h2>\n<p>label = tk.Label(root, font=(&quot;Helvetica&quot;, 48))<\/p>\n<p>label.pack()<\/p>\n<h2><strong>\u521b\u5efa\u6309\u94ae\u6765\u5207\u6362\u65f6\u95f4\u683c\u5f0f<\/strong><\/h2>\n<p>button = tk.Button(root, text=&quot;\u5207\u6362\u65f6\u95f4\u683c\u5f0f&quot;, command=toggle_time_format)<\/p>\n<p>button.pack()<\/p>\n<h2><strong>\u8bbe\u7f6e\u521d\u59cb\u65f6\u95f4\u683c\u5f0f<\/strong><\/h2>\n<p>time_format = &quot;%H:%M:%S&quot;<\/p>\n<h2><strong>\u8c03\u7528update_time\u51fd\u6570\u6765\u66f4\u65b0\u65f6\u95f4<\/strong><\/h2>\n<p>update_time()<\/p>\n<h2><strong>\u8fdb\u5165\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6309\u94ae\u6765\u5207\u6362\u65f6\u95f4\u7684\u663e\u793a\u683c\u5f0f\u3002\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>toggle_time_format<\/code>\u51fd\u6570\u6765\u5207\u6362\u65f6\u95f4\u683c\u5f0f\uff0c\u5e76\u5c06\u5176\u7ed1\u5b9a\u5230\u6309\u94ae\u7684<code>command<\/code>\u5c5e\u6027\u4e0a\u3002\u521d\u59cb\u65f6\u95f4\u683c\u5f0f\u8bbe\u7f6e\u4e3a<code>&quot;%H:%M:%S&quot;<\/code>\uff0c\u537324\u5c0f\u65f6\u5236\u3002\u70b9\u51fb\u6309\u94ae\u540e\uff0c\u65f6\u95f4\u683c\u5f0f\u5c06\u572824\u5c0f\u65f6\u5236\u548c12\u5c0f\u65f6\u5236\u4e4b\u95f4\u5207\u6362\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<p>\u901a\u8fc7\u4f7f\u7528tkinter\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0c\u5e76\u5c06\u5f53\u524d\u65f6\u95f4\u663e\u793a\u5728\u754c\u9762\u4e0a\u3002\u6211\u4eec\u4f7f\u7528Label\u5c0f\u90e8\u4ef6\u6765\u663e\u793a\u65f6\u95f4\uff0c\u5e76\u4f7f\u7528after\u65b9\u6cd5\u5b9a\u671f\u66f4\u65b0\u65f6\u95f4\u3002\u901a\u8fc7\u8fdb\u4e00\u6b65\u5b8c\u5584\u754c\u9762\u548c\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u66f4\u52a0\u5b9e\u7528\u7684\u65f6\u95f4\u663e\u793a\u5668\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5e76\u663e\u793a\u5728\u754c\u9762\u4e0a\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3002\u7ed3\u5408\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\uff0c\u6bd4\u5982Tkinter\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u754c\u9762\u6765\u663e\u793a\u65f6\u95f4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import tkinter as tk\nfrom datetime import datetime\n\ndef update_time():\n    current_time = datetime.now().strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)\n    time_label.config(text=current_time)\n    time_label.after(1000, update_time)  # \u6bcf1000\u6beb\u79d2\u66f4\u65b0\u4e00\u6b21\n\nroot = tk.Tk()\nroot.title(&quot;\u65f6\u95f4\u663e\u793a&quot;)\n\ntime_label = tk.Label(root, font=(&#39;Helvetica&#39;, 48))\ntime_label.pack()\n\nupdate_time()  # \u521d\u59cb\u5316\u65f6\u95f4\u663e\u793a\nroot.mainloop()\n<\/code><\/pre>\n<p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5f39\u51fa\u4e00\u4e2a\u7a97\u53e3\uff0c\u5b9e\u65f6\u66f4\u65b0\u5f53\u524d\u65f6\u95f4\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python GUI\u4e2d\u683c\u5f0f\u5316\u65f6\u95f4\u7684\u663e\u793a\uff1f<\/strong><br \/>\u5728\u663e\u793a\u65f6\u95f4\u65f6\uff0c\u901a\u5e38\u9700\u8981\u6839\u636e\u9700\u6c42\u8fdb\u884c\u683c\u5f0f\u5316\u3002\u53ef\u4ee5\u4f7f\u7528<code>strftime<\/code>\u65b9\u6cd5\u6765\u5b9a\u4e49\u65f6\u95f4\u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff0c<code>strftime(&quot;%H:%M:%S&quot;)<\/code>\u5c06\u53ea\u663e\u793a\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u3002\u6839\u636e\u9700\u6c42\u53ef\u4ee5\u4fee\u6539\u683c\u5f0f\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982<code>&quot;%Y-%m-%d %I:%M:%S %p&quot;<\/code>\u5c06\u663e\u793a\u5b8c\u6574\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u5305\u62ecAM\/PM\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5728Python\u754c\u9762\u4e0a\u663e\u793a\u65f6\u95f4\uff1f<\/strong><br \/>\u9664\u4e86Tkinter\uff0cPython\u8fd8\u6709\u5176\u4ed6GUI\u5e93\uff0c\u4f8b\u5982PyQt\u548cKivy\u3002\u8fd9\u4e9b\u5e93\u4e5f\u80fd\u591f\u5b9e\u73b0\u65f6\u95f4\u7684\u663e\u793a\u529f\u80fd\u3002\u4ee5PyQt\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>QTimer<\/code>\u5b9a\u65f6\u5668\u6765\u66f4\u65b0\u663e\u793a\u7684\u65f6\u95f4\u3002\u8fd9\u6837\u53ef\u4ee5\u6839\u636e\u4e2a\u4eba\u7684\u7f16\u7a0b\u4e60\u60ef\u548c\u9879\u76ee\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u5e93\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u7f16\u5199\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e76\u5c06\u8f93\u51fa\u65f6\u95f4\u663e\u793a\u5728\u754c\u9762\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528tkinter\u5e93\u3001PyQt\u5e93\u3001Ki [&hellip;]","protected":false},"author":3,"featured_media":1057490,"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\/1057478"}],"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=1057478"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1057478\/revisions"}],"predecessor-version":[{"id":1057495,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1057478\/revisions\/1057495"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1057490"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1057478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1057478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1057478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}