{"id":204913,"date":"2025-05-29T09:40:48","date_gmt":"2025-05-29T01:40:48","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204913\/"},"modified":"2025-05-29T09:40:48","modified_gmt":"2025-05-29T01:40:48","slug":"tkinter%e7%95%8c%e9%9d%a2%e5%ae%9e%e6%97%b6%e7%bb%98%e5%88%b6%e5%87%bd%e6%95%b0%e5%9b%be%e5%83%8f%ef%bc%9a%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e6%8c%89%e9%92%ae%e6%8e%a7%e5%88%b6%e7%94%b5%e8%b7%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204913\/","title":{"rendered":"Tkinter\u754c\u9762\u5b9e\u65f6\u7ed8\u5236\u51fd\u6570\u56fe\u50cf\uff1a\u5982\u4f55\u5b9e\u73b0\u6309\u94ae\u63a7\u5236\u7535\u8def\u7684\u65ad\u5f00\u4e0e\u95ed\u5408\u5e76\u4ece\u70b9\u51fb\u65f6\u523b\u5f00\u59cb\u7ed8\u5236\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Tkinter\u754c\u9762\u5b9e\u65f6\u7ed8\u5236\u51fd\u6570\u56fe\u50cf\uff1a\u5982\u4f55\u5b9e\u73b0\u6309\u94ae\u63a7\u5236\u7535\u8def\u7684\u65ad\u5f00\u4e0e\u95ed\u5408\u5e76\u4ece\u70b9\u51fb\u65f6\u523b\u5f00\u59cb\u7ed8\u5236\uff1f <\/h1>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300aTkinter\u754c\u9762\u5b9e\u65f6\u7ed8\u5236\u51fd\u6570\u56fe\u50cf\uff1a\u5982\u4f55\u5b9e\u73b0\u6309\u94ae\u63a7\u5236\u7535\u8def\u7684\u65ad\u5f00\u4e0e\u95ed\u5408\u5e76\u4ece\u70b9\u51fb\u65f6\u523b\u5f00\u59cb\u7ed8\u5236\uff1f \u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1731989030673c0e263ca23.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u8bbe\u8ba1tkinter\u63a7\u5236\u6309\u94ae\uff0c\u5b9e\u65f6\u751f\u6210\u51fd\u6570\u56fe\u50cf<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong>\u4f7f\u7528tkinter\u8bbe\u8ba1\u4e86\u754c\u9762\uff0c\u4f46\u70b9\u51fb\u6309\u94ae\u540e\uff0c\u51fd\u6570\u56fe\u50cf\u4ece0\u5f00\u59cb\uff0c\u800c\u4e0d\u662f\u6309\u94ae\u70b9\u51fb\u65f6\u523b\u5f00\u59cb\uff0c\u65e0\u6cd5\u5b9e\u73b0\u7535\u8def\u7684\u65ad\u5f00\u548c\u95ed\u5408\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u4fee\u6539\u4ee3\u7801\u4e2d\u7684\u5173\u952e\u90e8\u5206\uff1a<\/p>\n<pre>def toggle_manual_switch(self):\n    # \u83b7\u53d6\u5f53\u524d\u65f6\u523b\u7684\u7d22\u5f15\n    current_index = int(self.current_time_index)\n\n    # \u8fdb\u884c\u72b6\u6001\u5207\u6362\n    self.simulator.switch_states[current_index] = not self.simulator.switch_states[current_index]\n\n    # \u66f4\u65b0\u6309\u94ae\u6587\u672c\u548c\u547d\u4ee4\n    if self.manual_switch_button[\"text\"] == \"Close Circuit\":\n        self.manual_switch_button[\"text\"] = \"Open Circuit\"\n    else:\n        self.manual_switch_button[\"text\"] = \"Close Circuit\"\n\n    # \u66f4\u65b0\u6574\u4e2a\u56fe\u8868\uff0c\u4f20\u9012\u5f53\u524d\u65f6\u95f4\u70b9\u7684\u7d22\u5f15\n    self.update_plot(current_index)\n    self.canvas.draw_idle()\n\n\ndef calculate_circuit_response(self, current_time_index):\n    # \u68c0\u67e5\u5f53\u524d\u65f6\u95f4\u70b9\u662f\u5426\u6709\u5f00\u5173\u5207\u6362\u53d1\u751f\n    if current_time_index &gt; self.previous_switch_time_index:\n        # \u68c0\u67e5\u5f53\u524d\u65f6\u95f4\u70b9\u7684\u5f00\u5173\u72b6\u6001\u662f\u5426\u4e0e\u524d\u4e00\u65f6\u523b\u4e0d\u540c\n        if self.switch_states[current_time_index] != self.switch_states[current_time_index - 1]:\n            self.previous_switch_state = not self.previous_switch_state\n            next_switch_index = current_time_index + np.argmax(\n                self.switch_states[current_time_index:] != self.switch_states[current_time_index])\n            if not self.previous_switch_state:\n                # \u5f00\u5173\u65ad\u5f00\n                self.VoltageOverTime[current_time_index:] = 0\n                self.CurrentOverTime[current_time_index:] = 0\n            else:\n                # \u5f00\u5173\u95ed\u5408\n                self.VoltageOverTime[current_time_index:] = V_battery * np.ones_like(self.VoltageOverTime[current_time_index:])\n                self.CurrentOverTime[current_time_index:] = V_battery \/ R_load * np.ones_like(self.CurrentOverTime[current_time_index:])\n            # \u66f4\u65b0\u4e0a\u4e00\u6b21\u5f00\u5173\u5207\u6362\u7684\u65f6\u95f4\u7d22\u5f15\n            self.previous_switch_time_index = next_switch_index\n\ndef update_plot(self, frame):\n    self.simulator.calculate_circuit_response(frame)\n    time = t[frame]\n    # \u66f4\u65b0\u65f6\u95f4\u7d22\u5f15\n    self.current_time_index = frame\n\n    V_circuit = self.simulator.VoltageOverTime[:frame + 1]\n    I_circuit = self.simulator.CurrentOverTime[:frame + 1]\n\n    self.V_line.set_data(t[:len(V_circuit)], V_circuit)\n    self.I_line.set_data(t[:len(I_circuit)], I_circuit)\n    self.axs[0].set_xlim(0, t_max)\n    self.axs[1].set_xlim(0, t_max)\n    self.axs[0].set_ylim(0, 20)\n    self.axs[1].set_ylim(0, 2)\n    print(\"Plot updated\")  # \u6dfb\u52a0\u8fd9\u884c\u4ee3\u7801\u6765\u786e\u8ba4\u56fe\u8868\u662f\u5426\u88ab\u66f4\u65b0\n    print(\"Plot Voltage:\", V_circuit[-1], \"V\")\n    return self.V_line, self.I_line<\/pre>\n<p><strong>\u7279\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u4fee\u6539\u4e86update_plot\u51fd\u6570\uff0c\u66f4\u65b0\u4e86\u65f6\u95f4\u7d22\u5f15current_time_index\u3002<\/li>\n<li>\u4fee\u6539\u4e86calculate_circuit_response\u51fd\u6570\uff0c\u5373\u4f7f\u4ec5\u53d1\u751f\u4e00\u6b21\u72b6\u6001\u5207\u6362\uff0c\u4e5f\u80fd\u66f4\u65b0\u7535\u538b\u548c\u7535\u6d41\u503c\u3002<\/li>\n<li>\u4fee\u6539\u4e86toggle_manual_switch\u51fd\u6570\uff0c\u70b9\u51fb\u6309\u94ae\u540e\u76f4\u63a5\u8fdb\u884c\u72b6\u6001\u5207\u6362\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u4fee\u6539\u540e\uff0c\u7535\u8def\u53ef\u4ee5\u4ece\u6309\u94ae\u70b9\u51fb\u65f6\u523b\u5f00\u59cb\u7ed8\u5236\uff0c\u5e76\u4e14\u6309\u94ae\u64cd\u4f5c\u80fd\u591f\u5b9e\u73b0\u7535\u8def\u7684\u65ad\u5f00\u548c\u95ed\u5408\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tkinter\u754c\u9762\u5b9e\u65f6\u7ed8\u5236\u51fd\u6570\u56fe&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204913","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204913","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204913"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204913\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}