{"id":945832,"date":"2024-12-26T23:29:41","date_gmt":"2024-12-26T15:29:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/945832.html"},"modified":"2024-12-26T23:29:43","modified_gmt":"2024-12-26T15:29:43","slug":"python-%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e9%92%9f%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/945832.html","title":{"rendered":"python \u5982\u4f55\u5b9e\u73b0\u949f\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082243\/0939df96-8e41-4c79-b99b-6a9d5e7a7087.webp\" alt=\"python \u5982\u4f55\u5b9e\u73b0\u949f\u8868\" \/><\/p>\n<p><p> <strong>\u5b9e\u73b0Python\u949f\u8868\u7684\u6838\u5fc3\u5728\u4e8e\uff1a\u4f7f\u7528\u56fe\u5f62\u5316\u5e93\u3001\u7ed8\u5236\u8868\u76d8\u548c\u6307\u9488\u3001\u66f4\u65b0\u6307\u9488\u7684\u4f4d\u7f6e<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u56fe\u5f62\u5316\u5e93\u662f\u5b9e\u73b0\u949f\u8868\u754c\u9762\u7684\u57fa\u7840\uff0c\u7ed8\u5236\u8868\u76d8\u548c\u6307\u9488\u662f\u949f\u8868\u7684\u89c6\u89c9\u8868\u73b0\uff0c\u800c\u66f4\u65b0\u6307\u9488\u7684\u4f4d\u7f6e\u5219\u662f\u5b9e\u73b0\u949f\u8868\u8fd0\u4f5c\u7684\u5173\u952e\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e9b\u6838\u5fc3\u8981\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u56fe\u5f62\u5316\u5e93<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u56fe\u5f62\u5316\u5e93\u53ef\u4f9b\u4f7f\u7528\u3002\u5176\u4e2d\uff0c<code>Tkinter<\/code> \u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u4e14\u5185\u7f6e\u7684\u5e93\uff0c\u7528\u4e8e\u521b\u5efa\u7b80\u5355\u7684GUI\u5e94\u7528\u7a0b\u5e8f\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u5e93\u5982<code>Pygame<\/code>\u548c<code>matplotlib<\/code>\u7b49\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u9700\u6c42\u7684\u56fe\u5f62\u5316\u5e94\u7528\u3002\u672c\u6587\u5c06\u4e3b\u8981\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>Tkinter<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u949f\u8868\u3002<\/p>\n<\/p>\n<p><p><code>Tkinter<\/code>\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a7\u4ef6\u548c\u5e03\u5c40\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7528\u4e8e\u7ed8\u5236\u949f\u8868\u3002\u4f7f\u7528<code>Canvas<\/code>\u5c0f\u90e8\u4ef6\u53ef\u4ee5\u7ed8\u5236\u57fa\u672c\u7684\u5f62\u72b6\uff0c\u5982\u5706\u5f62\u548c\u7ebf\u6bb5\uff0c\u975e\u5e38\u9002\u5408\u7528\u6765\u7ed8\u5236\u949f\u8868\u7684\u8868\u76d8\u548c\u6307\u9488\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u7ed8\u5236\u8868\u76d8\u548c\u6307\u9488<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7ed8\u5236\u8868\u76d8<\/strong><\/p>\n<\/p>\n<p><p>\u8868\u76d8\u662f\u949f\u8868\u7684\u80cc\u666f\uff0c\u9700\u8981\u7ed8\u5236\u4e00\u4e2a\u5706\u5f62\u6765\u8868\u793a\u3002\u4f7f\u7528<code>Tkinter<\/code>\u4e2d\u7684<code>Canvas<\/code>\u5c0f\u90e8\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>create_oval<\/code>\u65b9\u6cd5\u7ed8\u5236\u4e00\u4e2a\u5706\u3002\u8868\u76d8\u4e0a\u901a\u5e38\u8fd8\u6709\u523b\u5ea6\u548c\u6570\u5b57\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u548c<code>create_line<\/code>\u65b9\u6cd5\u5728\u5706\u5468\u4e0a\u753b\u51fa\u65f6\u523b\u7ebf\uff0c\u5e76\u901a\u8fc7<code>create_text<\/code>\u65b9\u6cd5\u6807\u6ce8\u65f6\u95f4\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u53ef\u4ee5\u7528\u4ee5\u4e0b\u4ee3\u7801\u7ed8\u5236\u57fa\u672c\u7684\u8868\u76d8\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def draw_clock_face(canvas):<\/p>\n<p>    # \u7ed8\u5236\u8868\u76d8\u5706<\/p>\n<p>    canvas.create_oval(50, 50, 350, 350, outline=&#39;black&#39;, width=2)<\/p>\n<p>    # \u7ed8\u5236\u65f6\u523b<\/p>\n<p>    for i in range(12):<\/p>\n<p>        angle = i * 30<\/p>\n<p>        x = 200 + 140 * math.cos(math.radians(angle - 90))<\/p>\n<p>        y = 200 + 140 * math.sin(math.radians(angle - 90))<\/p>\n<p>        canvas.create_text(x, y, text=str(i+1), font=(&quot;Arial&quot;, 12))<\/p>\n<p>root = tk.Tk()<\/p>\n<p>canvas = tk.Canvas(root, width=400, height=400)<\/p>\n<p>canvas.pack()<\/p>\n<p>draw_clock_face(canvas)<\/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<\/li>\n<li>\n<p><strong>\u7ed8\u5236\u6307\u9488<\/strong><\/p>\n<\/p>\n<p><p>\u949f\u8868\u7684\u6307\u9488\u5305\u62ec\u65f6\u9488\u3001\u5206\u9488\u548c\u79d2\u9488\u3002\u6bcf\u4e00\u4e2a\u6307\u9488\u90fd\u662f\u4ece\u8868\u76d8\u7684\u4e2d\u5fc3\u6307\u5411\u4e0d\u540c\u7684\u89d2\u5ea6\u3002\u4f7f\u7528<code>Canvas<\/code>\u7684<code>create_line<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7ed8\u5236\u6307\u9488\u3002<\/p>\n<\/p>\n<p><p>\u6307\u9488\u7684\u957f\u5ea6\u548c\u7c97\u7ec6\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u3002\u65f6\u9488\u901a\u5e38\u8f83\u77ed\u4e14\u8f83\u7c97\uff0c\u5206\u9488\u7a0d\u957f\uff0c\u79d2\u9488\u6700\u957f\u4e14\u6700\u7ec6\u3002\u6839\u636e\u5f53\u524d\u65f6\u95f4\u8ba1\u7b97\u6bcf\u4e2a\u6307\u9488\u7684\u89d2\u5ea6\uff0c\u7136\u540e\u4f7f\u7528\u4e09\u89d2\u51fd\u6570\u786e\u5b9a\u6307\u9488\u7684\u7ec8\u70b9\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import math<\/p>\n<p>def draw_hands(canvas, hour, minute, second):<\/p>\n<p>    # \u8ba1\u7b97\u89d2\u5ea6<\/p>\n<p>    second_angle = second * 6<\/p>\n<p>    minute_angle = minute * 6 + second \/ 10<\/p>\n<p>    hour_angle = (hour % 12) * 30 + minute \/ 2<\/p>\n<p>    # \u7ed8\u5236\u65f6\u9488<\/p>\n<p>    hour_x = 200 + 60 * math.cos(math.radians(hour_angle - 90))<\/p>\n<p>    hour_y = 200 + 60 * math.sin(math.radians(hour_angle - 90))<\/p>\n<p>    canvas.create_line(200, 200, hour_x, hour_y, fill=&quot;black&quot;, width=6)<\/p>\n<p>    # \u7ed8\u5236\u5206\u9488<\/p>\n<p>    minute_x = 200 + 80 * math.cos(math.radians(minute_angle - 90))<\/p>\n<p>    minute_y = 200 + 80 * math.sin(math.radians(minute_angle - 90))<\/p>\n<p>    canvas.create_line(200, 200, minute_x, minute_y, fill=&quot;black&quot;, width=4)<\/p>\n<p>    # \u7ed8\u5236\u79d2\u9488<\/p>\n<p>    second_x = 200 + 90 * math.cos(math.radians(second_angle - 90))<\/p>\n<p>    second_y = 200 + 90 * math.sin(math.radians(second_angle - 90))<\/p>\n<p>    canvas.create_line(200, 200, second_x, second_y, fill=&quot;red&quot;, width=2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u66f4\u65b0\u6307\u9488\u7684\u4f4d\u7f6e<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u949f\u8868\u80fd\u591f\u8fd0\u4f5c\uff0c\u6307\u9488\u9700\u8981\u6839\u636e\u65f6\u95f4\u4e0d\u65ad\u66f4\u65b0\u3002\u53ef\u4ee5\u4f7f\u7528<code>Tkinter<\/code>\u7684<code>after<\/code>\u65b9\u6cd5\uff0c\u5b9a\u671f\u66f4\u65b0\u6307\u9488\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7684<code>time<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u7684\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u66f4\u65b0\u6307\u9488<\/strong><\/p>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u6e05\u9664\u65e7\u7684\u6307\u9488\u5e76\u7ed8\u5236\u65b0\u7684\u6307\u9488\u3002\u4f7f\u7528<code>after<\/code>\u65b9\u6cd5\u6bcf\u9694\u4e00\u6bb5\u65f6\u95f4\u8c03\u7528\u6b64\u51fd\u6570\uff0c\u4ee5\u5b9e\u73b0\u6307\u9488\u7684\u4e0d\u65ad\u66f4\u65b0\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def update_clock():<\/p>\n<p>    # \u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/p>\n<p>    current_time = time.localtime()<\/p>\n<p>    hour = current_time.tm_hour<\/p>\n<p>    minute = current_time.tm_min<\/p>\n<p>    second = current_time.tm_sec<\/p>\n<p>    # \u6e05\u9664\u65e7\u6307\u9488<\/p>\n<p>    canvas.delete(&quot;hands&quot;)<\/p>\n<p>    # \u7ed8\u5236\u65b0\u6307\u9488<\/p>\n<p>    draw_hands(canvas, hour, minute, second)<\/p>\n<p>    # \u6bcf1000\u6beb\u79d2\u66f4\u65b0\u4e00\u6b21<\/p>\n<p>    canvas.after(1000, update_clock)<\/p>\n<h2><strong>\u521d\u59cb\u5316\u5e76\u8fd0\u884c\u66f4\u65b0\u51fd\u6570<\/strong><\/h2>\n<p>update_clock()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528Python\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u949f\u8868\u3002\u867d\u7136\u8fd9\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u5b9e\u73b0\uff0c\u4f46\u5b83\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Python\u7684\u56fe\u5f62\u5316\u5e93\u6765\u521b\u5efa\u52a8\u6001\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u6839\u636e\u9700\u6c42\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u6269\u5c55\u529f\u80fd\uff0c\u6bd4\u5982\u6dfb\u52a0\u65e5\u671f\u663e\u793a\u3001\u8c03\u6574\u5916\u89c2\u6837\u5f0f\u6216\u589e\u52a0\u5176\u4ed6\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u949f\u8868\u7a0b\u5e8f\uff1f<\/strong><br \/>\u8981\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u949f\u8868\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684Tkinter\u5e93\u6765\u6784\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u3002\u60a8\u53ef\u4ee5\u5229\u7528datetime\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff0c\u5e76\u4f7f\u7528Tkinter\u7684Label\u7ec4\u4ef6\u6765\u663e\u793a\u8be5\u65f6\u95f4\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5faa\u73af\u5b9a\u65f6\u66f4\u65b0Label\u6765\u5b9e\u73b0\u949f\u8868\u7684\u52a8\u6001\u663e\u793a\u3002<\/p>\n<p><strong>Python\u4e2d\u5b9e\u73b0\u949f\u8868\u9700\u8981\u54ea\u4e9b\u57fa\u672c\u77e5\u8bc6\uff1f<\/strong><br \/>\u5728\u5b9e\u73b0\u949f\u8868\u529f\u80fd\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u638c\u63e1\u4e00\u4e9b\u57fa\u7840\u77e5\u8bc6\uff0c\u5305\u62ecPython\u7684\u57fa\u672c\u8bed\u6cd5\u3001Tkinter\u5e93\u7684\u4f7f\u7528\u4ee5\u53ca\u65f6\u95f4\u6a21\u5757datetime\u7684\u57fa\u672c\u64cd\u4f5c\u3002\u8fd9\u4e9b\u77e5\u8bc6\u80fd\u591f\u5e2e\u52a9\u60a8\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u66f4\u8f7b\u677e\u5730\u5904\u7406\u65f6\u95f4\u548c\u56fe\u5f62\u754c\u9762\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u4e2d\u5236\u4f5c\u6570\u5b57\u65f6\u949f\u548c\u6a21\u62df\u65f6\u949f\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528Tkinter\u5236\u4f5c\u6570\u5b57\u65f6\u949f\uff0c\u901a\u8fc7Label\u7ec4\u4ef6\u663e\u793a\u5f53\u524d\u65f6\u95f4\u7684\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u3002\u800c\u5bf9\u4e8e\u6a21\u62df\u65f6\u949f\uff0c\u53ef\u4ee5\u5229\u7528Canvas\u7ec4\u4ef6\u7ed8\u5236\u65f6\u949f\u7684\u8868\u76d8\u548c\u6307\u9488\uff0c\u5e76\u901a\u8fc7\u6570\u5b66\u8ba1\u7b97\u548c\u65f6\u95f4\u66f4\u65b0\u6765\u52a8\u6001\u663e\u793a\u65f6\u9488\u3001\u5206\u9488\u548c\u79d2\u9488\u7684\u79fb\u52a8\u3002\u8fd9\u6837\u7684\u5b9e\u73b0\u53ef\u4ee5\u8ba9\u60a8\u66f4\u52a0\u6df1\u5165\u7406\u89e3\u56fe\u5f62\u7f16\u7a0b\u548c\u65f6\u95f4\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5b9e\u73b0Python\u949f\u8868\u7684\u6838\u5fc3\u5728\u4e8e\uff1a\u4f7f\u7528\u56fe\u5f62\u5316\u5e93\u3001\u7ed8\u5236\u8868\u76d8\u548c\u6307\u9488\u3001\u66f4\u65b0\u6307\u9488\u7684\u4f4d\u7f6e\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u56fe\u5f62\u5316\u5e93\u662f\u5b9e\u73b0\u949f\u8868\u754c\u9762 [&hellip;]","protected":false},"author":3,"featured_media":945840,"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\/945832"}],"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=945832"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/945832\/revisions"}],"predecessor-version":[{"id":945842,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/945832\/revisions\/945842"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/945840"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=945832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=945832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=945832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}