{"id":1063959,"date":"2024-12-31T16:04:58","date_gmt":"2024-12-31T08:04:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1063959.html"},"modified":"2024-12-31T16:05:00","modified_gmt":"2024-12-31T08:05:00","slug":"python%e5%a6%82%e4%bd%95%e5%9b%9e%e8%b0%83%e4%b8%8a%e4%b8%80%e5%b1%82%e7%95%8c%e9%9d%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1063959.html","title":{"rendered":"python\u5982\u4f55\u56de\u8c03\u4e0a\u4e00\u5c42\u754c\u9762"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/163e9e6c-ad69-484c-8ef9-d9c5d29542ed.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u56de\u8c03\u4e0a\u4e00\u5c42\u754c\u9762\" \/><\/p>\n<p><p> <strong>Python\u56de\u8c03\u4e0a\u4e00\u5c42\u754c\u9762\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u51fd\u6570\u56de\u8c03\u3001\u4f7f\u7528\u4e8b\u4ef6\u9a71\u52a8\u3001\u4f7f\u7528\u72b6\u6001\u7ba1\u7406<\/strong>\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u8be6\u7ec6\u8bb2\u89e3\u5176\u4e2d\u4e00\u4e2a\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528\u51fd\u6570\u56de\u8c03<\/strong>\u3002\u51fd\u6570\u56de\u8c03\u662f\u4e00\u79cd\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u6280\u672f\uff0c\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u4e00\u4e2a\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u4f20\u9012\u7ed9\u6309\u94ae\u7684\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\uff0c\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u56de\u8c03\u4e0a\u4e00\u5c42\u754c\u9762\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u51fd\u6570\u56de\u8c03<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u56de\u8c03\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7f16\u7a0b\u6280\u672f\uff0c\u7279\u522b\u662f\u5728\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u4ece\u800c\u5728\u7279\u5b9a\u4e8b\u4ef6\u53d1\u751f\u540e\u8c03\u7528\u8be5\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u4f20\u9012\u7ed9\u6309\u94ae\u7684\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\uff0c\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u56de\u8c03\u4e0a\u4e00\u5c42\u754c\u9762\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def show_frame(frame):<\/p>\n<p>    frame.tkr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se()<\/p>\n<p>def create_main_frame(root):<\/p>\n<p>    frame = tk.Frame(root)<\/p>\n<p>    label = tk.Label(frame, text=&quot;Main Frame&quot;)<\/p>\n<p>    label.pack(pady=20)<\/p>\n<p>    btn = tk.Button(frame, text=&quot;Go to Second Frame&quot;, command=lambda: show_frame(second_frame))<\/p>\n<p>    btn.pack(pady=10)<\/p>\n<p>    frame.grid(row=0, column=0, sticky=&#39;news&#39;)<\/p>\n<p>    return frame<\/p>\n<p>def create_second_frame(root):<\/p>\n<p>    frame = tk.Frame(root)<\/p>\n<p>    label = tk.Label(frame, text=&quot;Second Frame&quot;)<\/p>\n<p>    label.pack(pady=20)<\/p>\n<p>    btn = tk.Button(frame, text=&quot;Back to Main Frame&quot;, command=lambda: show_frame(main_frame))<\/p>\n<p>    btn.pack(pady=10)<\/p>\n<p>    frame.grid(row=0, column=0, sticky=&#39;news&#39;)<\/p>\n<p>    return frame<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<p>main_frame = create_main_frame(root)<\/p>\n<p>second_frame = create_second_frame(root)<\/p>\n<p>show_frame(main_frame)<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<ol>\n<li>\u4f7f\u7528 <code>tk.Frame<\/code> \u521b\u5efa\u4e24\u4e2a\u72ec\u7acb\u7684\u754c\u9762 <code>main_frame<\/code> \u548c <code>second_frame<\/code>\u3002<\/li>\n<li><code>show_frame<\/code> \u51fd\u6570\u7528\u4e8e\u5207\u6362\u663e\u793a\u7684\u754c\u9762\u3002<\/li>\n<li>\u901a\u8fc7\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u8c03\u7528 <code>show_frame<\/code> \u51fd\u6570\uff0c\u5b9e\u73b0\u754c\u9762\u7684\u5207\u6362\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u4e8b\u4ef6\u9a71\u52a8<\/h3>\n<\/p>\n<p><p>\u4e8b\u4ef6\u9a71\u52a8\u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\uff0c\u5176\u4e2d\u7a0b\u5e8f\u7684\u6d41\u7a0b\u662f\u7531\u4e8b\u4ef6\u89e6\u53d1\u7684\u3002\u4e8b\u4ef6\u53ef\u4ee5\u662f\u7528\u6237\u8f93\u5165\uff08\u5982\u9f20\u6807\u70b9\u51fb\u3001\u952e\u76d8\u6309\u952e\uff09\u6216\u7cfb\u7edf\u4e8b\u4ef6\uff08\u5982\u6587\u4ef6\u8bfb\u53d6\u5b8c\u6210\uff09\u3002\u5728\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u7b49\u5f85\u4e8b\u4ef6\u7684\u53d1\u751f\uff0c\u5e76\u5728\u4e8b\u4ef6\u53d1\u751f\u65f6\u8c03\u7528\u76f8\u5e94\u7684\u5904\u7406\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def on_button_click(event):<\/p>\n<p>    show_frame(second_frame)<\/p>\n<p>def on_back_button_click(event):<\/p>\n<p>    show_frame(main_frame)<\/p>\n<p>def show_frame(frame):<\/p>\n<p>    frame.tkraise()<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<p>main_frame = tk.Frame(root)<\/p>\n<p>main_frame.grid(row=0, column=0, sticky=&#39;news&#39;)<\/p>\n<p>second_frame = tk.Frame(root)<\/p>\n<p>second_frame.grid(row=0, column=0, sticky=&#39;news&#39;)<\/p>\n<p>label = tk.Label(main_frame, text=&quot;Main Frame&quot;)<\/p>\n<p>label.pack(pady=20)<\/p>\n<p>btn = tk.Button(main_frame, text=&quot;Go to Second Frame&quot;)<\/p>\n<p>btn.pack(pady=10)<\/p>\n<p>btn.bind(&quot;&lt;Button-1&gt;&quot;, on_button_click)<\/p>\n<p>label = tk.Label(second_frame, text=&quot;Second Frame&quot;)<\/p>\n<p>label.pack(pady=20)<\/p>\n<p>btn = tk.Button(second_frame, text=&quot;Back to Main Frame&quot;)<\/p>\n<p>btn.pack(pady=10)<\/p>\n<p>btn.bind(&quot;&lt;Button-1&gt;&quot;, on_back_button_click)<\/p>\n<p>show_frame(main_frame)<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<ol>\n<li>\u4f7f\u7528 <code>bind<\/code> \u65b9\u6cd5\u5c06\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u4e0e\u5904\u7406\u51fd\u6570\u5173\u8054\u3002<\/li>\n<li><code>on_button_click<\/code> \u548c <code>on_back_button_click<\/code> \u51fd\u6570\u5206\u522b\u7528\u4e8e\u5904\u7406\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\uff0c\u8c03\u7528 <code>show_frame<\/code> \u51fd\u6570\u5207\u6362\u754c\u9762\u3002<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u72b6\u6001\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u72b6\u6001\u7ba1\u7406\u662f\u4e00\u79cd\u7f16\u7a0b\u6280\u672f\uff0c\u7528\u4e8e\u7ba1\u7406\u5e94\u7528\u7a0b\u5e8f\u7684\u72b6\u6001\u3002\u72b6\u6001\u662f\u5e94\u7528\u7a0b\u5e8f\u5728\u4efb\u4f55\u65f6\u523b\u7684\u5f53\u524d\u4fe1\u606f\u3002\u5728\u72b6\u6001\u7ba1\u7406\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u53d8\u91cf\u6765\u5b58\u50a8\u5e94\u7528\u7a0b\u5e8f\u7684\u5f53\u524d\u72b6\u6001\uff0c\u5e76\u6839\u636e\u72b6\u6001\u7684\u53d8\u5316\u6765\u66f4\u65b0\u754c\u9762\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>class App:<\/p>\n<p>    def __init__(self, root):<\/p>\n<p>        self.root = root<\/p>\n<p>        self.state = &quot;main&quot;<\/p>\n<p>        self.create_main_frame()<\/p>\n<p>        self.create_second_frame()<\/p>\n<p>        self.show_frame()<\/p>\n<p>    def create_main_frame(self):<\/p>\n<p>        self.main_frame = tk.Frame(self.root)<\/p>\n<p>        label = tk.Label(self.main_frame, text=&quot;Main Frame&quot;)<\/p>\n<p>        label.pack(pady=20)<\/p>\n<p>        btn = tk.Button(self.main_frame, text=&quot;Go to Second Frame&quot;, command=self.go_to_second_frame)<\/p>\n<p>        btn.pack(pady=10)<\/p>\n<p>        self.main_frame.grid(row=0, column=0, sticky=&#39;news&#39;)<\/p>\n<p>    def create_second_frame(self):<\/p>\n<p>        self.second_frame = tk.Frame(self.root)<\/p>\n<p>        label = tk.Label(self.second_frame, text=&quot;Second Frame&quot;)<\/p>\n<p>        label.pack(pady=20)<\/p>\n<p>        btn = tk.Button(self.second_frame, text=&quot;Back to Main Frame&quot;, command=self.go_to_main_frame)<\/p>\n<p>        btn.pack(pady=10)<\/p>\n<p>        self.second_frame.grid(row=0, column=0, sticky=&#39;news&#39;)<\/p>\n<p>    def show_frame(self):<\/p>\n<p>        if self.state == &quot;main&quot;:<\/p>\n<p>            self.main_frame.tkraise()<\/p>\n<p>        elif self.state == &quot;second&quot;:<\/p>\n<p>            self.second_frame.tkraise()<\/p>\n<p>    def go_to_main_frame(self):<\/p>\n<p>        self.state = &quot;main&quot;<\/p>\n<p>        self.show_frame()<\/p>\n<p>    def go_to_second_frame(self):<\/p>\n<p>        self.state = &quot;second&quot;<\/p>\n<p>        self.show_frame()<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<p>app = App(root)<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<ol>\n<li>\u4f7f\u7528 <code>self.state<\/code> \u53d8\u91cf\u5b58\u50a8\u5e94\u7528\u7a0b\u5e8f\u7684\u5f53\u524d\u72b6\u6001\u3002<\/li>\n<li>\u6839\u636e <code>self.state<\/code> \u7684\u503c\u8c03\u7528 <code>show_frame<\/code> \u51fd\u6570\u5207\u6362\u754c\u9762\u3002<\/li>\n<li><code>go_to_main_frame<\/code> \u548c <code>go_to_second_frame<\/code> \u65b9\u6cd5\u7528\u4e8e\u66f4\u65b0\u72b6\u6001\u5e76\u8c03\u7528 <code>show_frame<\/code> \u51fd\u6570\u3002<\/li>\n<\/ol>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u4ecb\u7ecd\u4e86\u4e09\u79cd\u5728 Python \u4e2d\u56de\u8c03\u4e0a\u4e00\u5c42\u754c\u9762\u7684\u65b9\u6cd5\uff1a<strong>\u51fd\u6570\u56de\u8c03<\/strong>\u3001<strong>\u4e8b\u4ef6\u9a71\u52a8<\/strong> \u548c <strong>\u72b6\u6001\u7ba1\u7406<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u51fd\u6570\u56de\u8c03\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u5c0f\u578b\u9879\u76ee\uff1b\u4e8b\u4ef6\u9a71\u52a8\u65b9\u6cd5\u7075\u6d3b\u9002\u7528\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u7528\u6237\u4ea4\u4e92\uff1b\u72b6\u6001\u7ba1\u7406\u65b9\u6cd5\u7ed3\u6784\u6e05\u6670\uff0c\u9002\u5408\u5927\u578b\u9879\u76ee\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u754c\u9762\u4e4b\u95f4\u7684\u56de\u8c03\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5b9e\u73b0\u754c\u9762\u4e4b\u95f4\u7684\u56de\u8c03\u901a\u5e38\u4f9d\u8d56\u4e8e\u4f7f\u7528\u6846\u67b6\uff0c\u5982Tkinter\u6216PyQt\u3002\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\u5e76\u5728\u754c\u9762\u7ec4\u4ef6\u7684\u4e8b\u4ef6\u4e2d\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u754c\u9762\u4e4b\u95f4\u7684\u4ea4\u4e92\u3002\u5f53\u7528\u6237\u70b9\u51fb\u6309\u94ae\u6216\u9009\u62e9\u67d0\u4e2a\u9009\u9879\u65f6\uff0c\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u66f4\u65b0\u5f53\u524d\u754c\u9762\u6216\u8c03\u7528\u4e0a\u4e00\u4e2a\u754c\u9762\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u4f7f\u7528Tkinter\uff0c\u5982\u4f55\u7ba1\u7406\u4e0d\u540c\u754c\u9762\u4e4b\u95f4\u7684\u5207\u6362\uff1f<\/strong><br \/>\u5728Tkinter\u4e2d\uff0c\u53ef\u4ee5\u521b\u5efa\u591a\u4e2aFrame\u6765\u4ee3\u8868\u4e0d\u540c\u7684\u754c\u9762\u3002\u901a\u8fc7\u8c03\u7528<code>pack_forget()<\/code>\u548c<code>pack()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9690\u85cf\u5f53\u524dFrame\u5e76\u663e\u793a\u4e0a\u4e00\u4e2aFrame\u3002\u901a\u8fc7\u8bbe\u8ba1\u4e00\u4e2a\u63a7\u5236\u5668\u7c7b\u6765\u7ba1\u7406\u8fd9\u4e9bFrame\uff0c\u53ef\u4ee5\u4f7f\u5207\u6362\u66f4\u52a0\u9ad8\u6548\u548c\u6e05\u6670\u3002<\/p>\n<p><strong>\u5728\u56de\u8c03\u4e4b\u524d\uff0c\u6211\u9700\u8981\u4fdd\u5b58\u5f53\u524d\u754c\u9762\u7684\u72b6\u6001\u5417\uff1f<\/strong><br \/>\u5728\u56de\u8c03\u524d\u4fdd\u5b58\u5f53\u524d\u754c\u9762\u7684\u72b6\u6001\u662f\u4e00\u4e2a\u826f\u597d\u7684\u5b9e\u8df5\uff0c\u7279\u522b\u662f\u5f53\u7528\u6237\u5728\u5f53\u524d\u754c\u9762\u8f93\u5165\u4e86\u4fe1\u606f\u65f6\u3002\u53ef\u4ee5\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3001\u7c7b\u5c5e\u6027\u6216\u6570\u636e\u7ed3\u6784\u6765\u5b58\u50a8\u8fd9\u4e9b\u72b6\u6001\uff0c\u4ee5\u4fbf\u5728\u8fd4\u56de\u65f6\u80fd\u591f\u6062\u590d\u4e4b\u524d\u7684\u8bbe\u7f6e\uff0c\u786e\u4fdd\u7528\u6237\u4f53\u9a8c\u7684\u4e00\u81f4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u56de\u8c03\u4e0a\u4e00\u5c42\u754c\u9762\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u51fd\u6570\u56de\u8c03\u3001\u4f7f\u7528\u4e8b\u4ef6\u9a71\u52a8\u3001\u4f7f\u7528\u72b6\u6001\u7ba1\u7406\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u8be6\u7ec6\u8bb2\u89e3\u5176\u4e2d\u4e00\u4e2a\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1063968,"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\/1063959"}],"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=1063959"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1063959\/revisions"}],"predecessor-version":[{"id":1063970,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1063959\/revisions\/1063970"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1063968"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1063959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1063959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1063959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}