{"id":1181800,"date":"2025-01-15T18:55:59","date_gmt":"2025-01-15T10:55:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1181800.html"},"modified":"2025-01-15T18:56:02","modified_gmt":"2025-01-15T10:56:02","slug":"python-%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1181800.html","title":{"rendered":"python \u5982\u4f55\u751f\u6210\u5e94\u7528\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25130019\/5e42fd8d-9acc-41a4-9417-08778031dc06.webp\" alt=\"python \u5982\u4f55\u751f\u6210\u5e94\u7528\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u751f\u6210\u5e94\u7528\u7a0b\u5e8f\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528GUI\u5e93\u521b\u5efa\u684c\u9762\u5e94\u7528\u3001\u4f7f\u7528Web\u6846\u67b6\u521b\u5efaWeb\u5e94\u7528\u3001\u4f7f\u7528\u79fb\u52a8\u6846\u67b6\u521b\u5efa\u79fb\u52a8\u5e94\u7528\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u751f\u6210CLI\u5e94\u7528\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528GUI\u5e93\u521b\u5efa\u684c\u9762\u5e94\u7528\u662f\u6700\u5e38\u89c1\u548c\u57fa\u7840\u7684\u65b9\u5f0f\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u7684Tkinter\u5e93\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528GUI\u5e93\u521b\u5efa\u684c\u9762\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001Tkinter\u5e93\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u51e0\u4e4e\u6240\u6709\u7684Python\u5b89\u88c5\u5305\u90fd\u81ea\u5e26\u4e86\u8fd9\u4e2a\u5e93\u3002\u4f7f\u7528Tkinter\u53ef\u4ee5\u521b\u5efa\u8de8\u5e73\u53f0\u7684\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\uff0c\u5b83\u63d0\u4f9b\u4e86\u5404\u79cd\u63a7\u4ef6\uff0c\u5982\u6309\u94ae\u3001\u6807\u7b7e\u3001\u6587\u672c\u6846\u7b49\uff0c\u53ef\u4ee5\u6ee1\u8db3\u5927\u591a\u6570\u7b80\u5355\u7684\u5e94\u7528\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5b89\u88c5Tkinter<\/h4>\n<\/p>\n<p><p>Tkinter\u901a\u5e38\u968fPython\u4e00\u8d77\u5b89\u88c5\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u6807\u51c6\u7684Python\u5b89\u88c5\u5305\uff0c\u90a3\u4e48Tkinter\u5df2\u7ecf\u5305\u542b\u5728\u5176\u4e2d\uff0c\u4f60\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u67d0\u4e9b\u7cbe\u7b80\u7248\u7684Python\uff0c\u6216\u8005\u5728\u7279\u5b9a\u73af\u5883\u4e2d\u6ca1\u6709Tkinter\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install tk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u521b\u5efa\u7b2c\u4e00\u4e2aTkinter\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Tkinter\u5e94\u7528\u7a0b\u5e8f\u793a\u4f8b\uff0c\u5b83\u521b\u5efa\u4e86\u4e00\u4e2a\u7a97\u53e3\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6309\u94ae\uff0c\u70b9\u51fb\u6309\u94ae\u540e\u4f1a\u5728\u63a7\u5236\u53f0\u6253\u5370\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import messagebox<\/p>\n<p>def on_button_click():<\/p>\n<p>    print(&quot;Button clicked!&quot;)<\/p>\n<p>    messagebox.showinfo(&quot;Info&quot;, &quot;Button clicked!&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Simple Tkinter App&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6309\u94ae<\/strong><\/h2>\n<p>button = tk.Button(root, text=&quot;Click me!&quot;, command=on_button_click)<\/p>\n<p>button.pack(pady=20)<\/p>\n<h2><strong>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f<\/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>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86Tkinter\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a\u4e3b\u7a97\u53e3\uff08root\uff09\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6309\u94ae\u3002\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\uff0c\u4f1a\u8c03\u7528<code>on_button_click<\/code>\u51fd\u6570\uff0c\u5728\u63a7\u5236\u53f0\u6253\u5370\u4fe1\u606f\u5e76\u663e\u793a\u4e00\u4e2a\u6d88\u606f\u6846\u3002\u6700\u540e\uff0c\u901a\u8fc7\u8c03\u7528<code>root.mainloop()<\/code>\u8fdb\u5165\u5e94\u7528\u7684\u4e3b\u5faa\u73af\uff0c\u7b49\u5f85\u7528\u6237\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u6269\u5c55Tkinter\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u66f4\u591a\u7684\u63a7\u4ef6\u548c\u529f\u80fd\u6765\u6269\u5c55\u4f60\u7684Tkinter\u5e94\u7528\uff0c\u4f8b\u5982\u6dfb\u52a0\u6587\u672c\u6846\u3001\u5217\u8868\u6846\u3001\u83dc\u5355\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u793a\u4f8b\uff0c\u5b83\u5305\u542b\u4e86\u66f4\u591a\u7684\u63a7\u4ef6\u548c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import messagebox<\/p>\n<p>def on_button_click():<\/p>\n<p>    user_input = entry.get()<\/p>\n<p>    if user_input:<\/p>\n<p>        messagebox.showinfo(&quot;Info&quot;, f&quot;You entered: {user_input}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        messagebox.showwarning(&quot;Warning&quot;, &quot;Please enter something!&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Enhanced Tkinter App&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6807\u7b7e\u548c\u6587\u672c\u6846<\/strong><\/h2>\n<p>label = tk.Label(root, text=&quot;Enter something:&quot;)<\/p>\n<p>label.pack(pady=5)<\/p>\n<p>entry = tk.Entry(root)<\/p>\n<p>entry.pack(pady=5)<\/p>\n<h2><strong>\u521b\u5efa\u6309\u94ae<\/strong><\/h2>\n<p>button = tk.Button(root, text=&quot;Submit&quot;, command=on_button_click)<\/p>\n<p>button.pack(pady=20)<\/p>\n<h2><strong>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6807\u7b7e\u548c\u4e00\u4e2a\u6587\u672c\u6846\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u6587\u672c\u6846\u4e2d\u8f93\u5165\u5185\u5bb9\uff0c\u7136\u540e\u70b9\u51fb\u6309\u94ae\u540e\u4f1a\u663e\u793a\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u3002\u5982\u679c\u6587\u672c\u6846\u4e3a\u7a7a\uff0c\u5219\u663e\u793a\u8b66\u544a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Web\u6846\u67b6\u521b\u5efaWeb\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001Flask\u6846\u67b6\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Web\u6846\u67b6\uff0c\u4f7f\u7528Python\u7f16\u5199\uff0c\u9002\u5408\u6784\u5efa\u5c0f\u578b\u548c\u4e2d\u578bWeb\u5e94\u7528\u3002\u5b83\u7b80\u5355\u6613\u7528\uff0c\u4f46\u529f\u80fd\u5f3a\u5927\uff0c\u5177\u6709\u5f88\u9ad8\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5b89\u88c5Flask<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Flask\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install Flask<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u521b\u5efa\u7b2c\u4e00\u4e2aFlask\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528\u7a0b\u5e8f\u793a\u4f8b\uff0c\u5b83\u521b\u5efa\u4e86\u4e00\u4e2aWeb\u670d\u52a1\u5668\uff0c\u5e76\u5728\u6839\u8def\u5f84\u663e\u793a\u201cHello, World!\u201d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def hello_world():<\/p>\n<p>    return &#39;Hello, World!&#39;<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86Flask\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2aFlask\u5e94\u7528\u5bf9\u8c61\uff08app\uff09\uff0c\u5e76\u5b9a\u4e49\u4e86\u4e00\u4e2a\u8def\u7531\uff0c\u5f53\u7528\u6237\u8bbf\u95ee\u6839\u8def\u5f84\uff08<code>\/<\/code>\uff09\u65f6\uff0c\u8fd4\u56de\u201cHello, World!\u201d\u3002\u6700\u540e\uff0c\u901a\u8fc7\u8c03\u7528<code>app.run()<\/code>\u542f\u52a8Web\u670d\u52a1\u5668\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u6269\u5c55Flask\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u66f4\u591a\u7684\u8def\u7531\u548c\u529f\u80fd\u6765\u6269\u5c55\u4f60\u7684Flask\u5e94\u7528\uff0c\u4f8b\u5982\u6dfb\u52a0\u8868\u5355\u5904\u7406\u3001\u6570\u636e\u5e93\u4ea4\u4e92\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u793a\u4f8b\uff0c\u5b83\u5305\u542b\u4e86\u4e00\u4e2a\u8868\u5355\uff0c\u7528\u6237\u53ef\u4ee5\u63d0\u4ea4\u6570\u636e\uff0c\u670d\u52a1\u5668\u4f1a\u5904\u7406\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request, render_template<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def index():<\/p>\n<p>    return render_template(&#39;index.html&#39;)<\/p>\n<p>@app.route(&#39;\/submit&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def submit():<\/p>\n<p>    user_input = request.form[&#39;user_input&#39;]<\/p>\n<p>    return f&#39;You entered: {user_input}&#39;<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>render_template<\/code>\u51fd\u6570\u6765\u6e32\u67d3HTML\u6a21\u677f\uff0c\u5e76\u5904\u7406\u8868\u5355\u63d0\u4ea4\u3002\u4ee5\u4e0b\u662f<code>index.html<\/code>\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Flask Form&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Enter something&lt;\/h1&gt;<\/p>\n<p>    &lt;form action=&quot;\/submit&quot; method=&quot;post&quot;&gt;<\/p>\n<p>        &lt;input type=&quot;text&quot; name=&quot;user_input&quot;&gt;<\/p>\n<p>        &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;<\/p>\n<p>    &lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u79fb\u52a8\u6846\u67b6\u521b\u5efa\u79fb\u52a8\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001Kivy\u6846\u67b6\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>Kivy\u662f\u4e00\u4e2a\u5f00\u6e90\u7684Python\u5e93\uff0c\u7528\u4e8e\u5f00\u53d1\u8de8\u5e73\u53f0\u7684\u79fb\u52a8\u5e94\u7528\u7a0b\u5e8f\u3002\u5b83\u652f\u6301\u591a\u70b9\u89e6\u63a7\uff0c\u5177\u6709\u4e30\u5bcc\u7684\u63a7\u4ef6\u548c\u529f\u80fd\uff0c\u53ef\u4ee5\u5728iOS\u3001Android\u3001Windows\u3001macOS\u548cLinux\u4e0a\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5b89\u88c5Kivy<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Kivy\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install kivy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u521b\u5efa\u7b2c\u4e00\u4e2aKivy\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Kivy\u5e94\u7528\u7a0b\u5e8f\u793a\u4f8b\uff0c\u5b83\u521b\u5efa\u4e86\u4e00\u4e2a\u7a97\u53e3\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6309\u94ae\uff0c\u70b9\u51fb\u6309\u94ae\u540e\u4f1a\u5728\u63a7\u5236\u53f0\u6253\u5370\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.button import Button<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        return Button(text=&#39;Hello, World!&#39;, on_press=self.on_button_click)<\/p>\n<p>    def on_button_click(self, instance):<\/p>\n<p>        print(&#39;Button clicked!&#39;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86Kivy\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a\u7ee7\u627f\u81ea<code>App<\/code>\u7684\u7c7b\uff0c\u5e76\u5b9e\u73b0\u4e86<code>build<\/code>\u65b9\u6cd5\uff0c\u8fd4\u56de\u4e00\u4e2a\u6309\u94ae\u3002\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\uff0c\u4f1a\u8c03\u7528<code>on_button_click<\/code>\u65b9\u6cd5\uff0c\u5728\u63a7\u5236\u53f0\u6253\u5370\u4fe1\u606f\u3002\u6700\u540e\uff0c\u901a\u8fc7\u8c03\u7528<code>MyApp().run()<\/code>\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u6269\u5c55Kivy\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u66f4\u591a\u7684\u63a7\u4ef6\u548c\u529f\u80fd\u6765\u6269\u5c55\u4f60\u7684Kivy\u5e94\u7528\uff0c\u4f8b\u5982\u6dfb\u52a0\u6587\u672c\u6846\u3001\u5217\u8868\u6846\u3001\u5e03\u5c40\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u793a\u4f8b\uff0c\u5b83\u5305\u542b\u4e86\u66f4\u591a\u7684\u63a7\u4ef6\u548c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.boxlayout import BoxLayout<\/p>\n<p>from kivy.uix.label import Label<\/p>\n<p>from kivy.uix.textinput import TextInput<\/p>\n<p>from kivy.uix.button import Button<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        self.layout = BoxLayout(orientation=&#39;vertical&#39;)<\/p>\n<p>        self.label = Label(text=&#39;Enter something:&#39;)<\/p>\n<p>        self.layout.add_widget(self.label)<\/p>\n<p>        self.text_input = TextInput()<\/p>\n<p>        self.layout.add_widget(self.text_input)<\/p>\n<p>        self.button = Button(text=&#39;Submit&#39;, on_press=self.on_button_click)<\/p>\n<p>        self.layout.add_widget(self.button)<\/p>\n<p>        return self.layout<\/p>\n<p>    def on_button_click(self, instance):<\/p>\n<p>        user_input = self.text_input.text<\/p>\n<p>        self.label.text = f&#39;You entered: {user_input}&#39;<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>BoxLayout<\/code>\u5e03\u5c40\uff0c\u5e76\u6dfb\u52a0\u4e86\u6807\u7b7e\u3001\u6587\u672c\u6846\u548c\u6309\u94ae\u3002\u7528\u6237\u53ef\u4ee5\u5728\u6587\u672c\u6846\u4e2d\u8f93\u5165\u5185\u5bb9\uff0c\u7136\u540e\u70b9\u51fb\u6309\u94ae\u540e\u4f1a\u663e\u793a\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u751f\u6210CLI\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001Click\u5e93\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>Click\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u547d\u4ee4\u884c\u63a5\u53e3\uff08CLI\uff09\u7684Python\u5e93\uff0c\u7b80\u5355\u6613\u7528\uff0c\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u6784\u5efa\u5404\u79cd\u7c7b\u578b\u7684CLI\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5b89\u88c5Click<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Click\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install click<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u521b\u5efa\u7b2c\u4e00\u4e2aClick\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Click\u5e94\u7528\u7a0b\u5e8f\u793a\u4f8b\uff0c\u5b83\u521b\u5efa\u4e86\u4e00\u4e2a\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u5e76\u5b9a\u4e49\u4e86\u4e00\u4e2a\u547d\u4ee4\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import click<\/p>\n<p>@click.command()<\/p>\n<p>@click.option(&#39;--name&#39;, prompt=&#39;Your name&#39;, help=&#39;The person to greet.&#39;)<\/p>\n<p>def hello(name):<\/p>\n<p>    click.echo(f&#39;Hello, {name}!&#39;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86Click\u5e93\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u547d\u4ee4<code>hello<\/code>\uff0c\u5b83\u6709\u4e00\u4e2a\u9009\u9879<code>--name<\/code>\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u4f20\u9012\u8be5\u9009\u9879\u7684\u503c\uff0c\u6216\u8005\u5728\u8fd0\u884c\u65f6\u8f93\u5165\u3002\u6700\u540e\uff0c\u901a\u8fc7\u8c03\u7528<code>hello()<\/code>\u542f\u52a8\u547d\u4ee4\u884c\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u6269\u5c55Click\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u66f4\u591a\u7684\u547d\u4ee4\u548c\u9009\u9879\u6765\u6269\u5c55\u4f60\u7684Click\u5e94\u7528\uff0c\u4f8b\u5982\u6dfb\u52a0\u591a\u7ea7\u547d\u4ee4\u3001\u53c2\u6570\u9a8c\u8bc1\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u793a\u4f8b\uff0c\u5b83\u5305\u542b\u4e86\u66f4\u591a\u7684\u547d\u4ee4\u548c\u9009\u9879\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import click<\/p>\n<p>@click.group()<\/p>\n<p>def cli():<\/p>\n<p>    pass<\/p>\n<p>@cli.command()<\/p>\n<p>@click.option(&#39;--name&#39;, prompt=&#39;Your name&#39;, help=&#39;The person to greet.&#39;)<\/p>\n<p>def hello(name):<\/p>\n<p>    click.echo(f&#39;Hello, {name}!&#39;)<\/p>\n<p>@cli.command()<\/p>\n<p>@click.option(&#39;--age&#39;, prompt=&#39;Your age&#39;, help=&#39;Your age.&#39;)<\/p>\n<p>def age(age):<\/p>\n<p>    click.echo(f&#39;You are {age} years old!&#39;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    cli()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>group<\/code>\u88c5\u9970\u5668\u521b\u5efa\u4e86\u4e00\u4e2a\u547d\u4ee4\u7ec4\uff0c\u5e76\u5b9a\u4e49\u4e86\u4e24\u4e2a\u547d\u4ee4<code>hello<\/code>\u548c<code>age<\/code>\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u5206\u522b\u8c03\u7528\u8fd9\u4e24\u4e2a\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u4f7f\u7528Python\u751f\u6210\u5e94\u7528\u7a0b\u5e8f\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528GUI\u5e93\u521b\u5efa\u684c\u9762\u5e94\u7528\u3001\u4f7f\u7528Web\u6846\u67b6\u521b\u5efaWeb\u5e94\u7528\u3001\u4f7f\u7528\u79fb\u52a8\u6846\u67b6\u521b\u5efa\u79fb\u52a8\u5e94\u7528\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u751f\u6210CLI\u5e94\u7528\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u9002\u5408\u81ea\u5df1\u7684\u65b9\u6cd5\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5b8c\u6210\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u3002\u65e0\u8bba\u662f\u521b\u5efa\u7b80\u5355\u7684\u684c\u9762\u5e94\u7528\u3001\u590d\u6742\u7684Web\u5e94\u7528\uff0c\u8fd8\u662f\u529f\u80fd\u5f3a\u5927\u7684\u79fb\u52a8\u5e94\u7528\u548c\u547d\u4ee4\u884c\u5de5\u5177\uff0cPython\u90fd\u80fd\u63d0\u4f9b\u4e30\u5bcc\u7684\u5e93\u548c\u6846\u67b6\u6765\u5e2e\u52a9\u4f60\u5b9e\u73b0\u76ee\u6807\u3002\u5e0c\u671b\u8fd9\u4e9b\u793a\u4f8b\u548c\u4ecb\u7ecd\u80fd\u4e3a\u4f60\u63d0\u4f9b\u6709\u7528\u7684\u53c2\u8003\uff0c\u52a9\u4f60\u66f4\u597d\u5730\u4f7f\u7528Python\u8fdb\u884c\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\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\u5e94\u7528\u7a0b\u5e8f\uff1f<\/strong><br \/>\u8981\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u9996\u5148\u9700\u8981\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u6846\u67b6\uff0c\u6bd4\u5982Flask\u6216Django\u3002\u5982\u679c\u662f\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Tkinter\u6216PyQt\u3002\u5b89\u88c5\u6240\u9700\u7684\u5e93\u540e\uff0c\u8bbe\u8ba1\u5e94\u7528\u7a0b\u5e8f\u7684\u754c\u9762\u548c\u529f\u80fd\uff0c\u7f16\u5199\u4ee3\u7801\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\uff0c\u6700\u540e\u8fdb\u884c\u6d4b\u8bd5\u548c\u8c03\u8bd5\u3002<\/p>\n<p><strong>Python\u751f\u6210\u7684\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u8fd0\u884c\u5728\u54ea\u4e9b\u5e73\u53f0\u4e0a\uff1f<\/strong><br \/>Python\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u8fd0\u884c\u5728\u591a\u4e2a\u5e73\u53f0\u4e0a\uff0c\u5305\u62ecWindows\u3001macOS\u548cLinux\u3002\u4f7f\u7528PyInstaller\u6216cx_Freeze\u7b49\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4f7f\u5176\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\u800c\u65e0\u9700\u5b89\u88c5Python\u73af\u5883\u3002<\/p>\n<p><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u5982\u4f55\u7ba1\u7406\u4f9d\u8d56\u5e93\u548c\u73af\u5883\uff1f<\/strong><br \/>\u7ba1\u7406\u4f9d\u8d56\u5e93\u53ef\u4ee5\u4f7f\u7528\u865a\u62df\u73af\u5883\u5de5\u5177\uff0c\u5982venv\u6216conda\uff0c\u521b\u5efa\u72ec\u7acb\u7684\u9879\u76ee\u73af\u5883\uff0c\u4ee5\u907f\u514d\u5e93\u51b2\u7a81\u3002\u5728\u9879\u76ee\u5f00\u59cb\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7requirements.txt\u6587\u4ef6\u8bb0\u5f55\u6240\u9700\u7684\u5e93\uff0c\u4f7f\u7528pip install\u547d\u4ee4\u5feb\u901f\u5b89\u88c5\uff0c\u786e\u4fdd\u5f00\u53d1\u548c\u751f\u4ea7\u73af\u5883\u4e00\u81f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u751f\u6210\u5e94\u7528\u7a0b\u5e8f\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528GUI\u5e93\u521b\u5efa\u684c\u9762\u5e94\u7528\u3001\u4f7f\u7528Web\u6846\u67b6\u521b\u5efaWeb\u5e94\u7528\u3001\u4f7f\u7528\u79fb\u52a8\u6846\u67b6\u521b [&hellip;]","protected":false},"author":3,"featured_media":1181807,"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\/1181800"}],"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=1181800"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1181800\/revisions"}],"predecessor-version":[{"id":1181808,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1181800\/revisions\/1181808"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1181807"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1181800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1181800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1181800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}