{"id":1043965,"date":"2024-12-31T13:10:38","date_gmt":"2024-12-31T05:10:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1043965.html"},"modified":"2024-12-31T13:10:40","modified_gmt":"2024-12-31T05:10:40","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e8%be%93%e5%85%a5%e6%a1%86%e7%9a%84%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1043965.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6\u8f93\u5165\u6846\u7684\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/6214631f-3e5f-4ecd-b75b-15698e5477bd.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u83b7\u53d6\u8f93\u5165\u6846\u7684\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>Python\u83b7\u53d6\u8f93\u5165\u6846\u5185\u5bb9\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u8f93\u5165\u51fd\u6570\u3001\u4f7f\u7528GUI\u5e93\u3001\u4f7f\u7528Web\u6846\u67b6\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528GUI\u5e93\u662f\u8f83\u4e3a\u5e38\u89c1\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u521b\u5efa\u56fe\u5f62\u5316\u7528\u6237\u754c\u9762\uff0c\u4ece\u800c\u83b7\u53d6\u7528\u6237\u8f93\u5165\u5185\u5bb9\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u7684\u4e0d\u540c\u65b9\u6cd5\u83b7\u53d6\u8f93\u5165\u6846\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e00\u3001\u4f7f\u7528\u8f93\u5165\u51fd\u6570<\/strong><\/h2>\n<p><p>Python\u5185\u7f6e\u7684input()\u51fd\u6570\u662f\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u83b7\u53d6\u7528\u6237\u5728\u547d\u4ee4\u884c\u4e2d\u7684\u8f93\u5165\u3002\u5c3d\u7ba1\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u9002\u7528\u4e8e\u56fe\u5f62\u5316\u7528\u6237\u754c\u9762\uff0c\u4f46\u5b83\u5728\u5f88\u591a\u57fa\u7840\u7a0b\u5e8f\u4e2d\u4f9d\u7136\u975e\u5e38\u5b9e\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u5185\u5bb9\uff1a&quot;)<\/p>\n<p>print(&quot;\u4f60\u8f93\u5165\u7684\u5185\u5bb9\u662f\uff1a&quot;, user_input)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u6bb5\u4f1a\u5728\u7ec8\u7aef\u4e0a\u63d0\u793a\u7528\u6237\u8f93\u5165\u5185\u5bb9\uff0c\u5e76\u5c06\u8f93\u5165\u7684\u5185\u5bb9\u5b58\u50a8\u5728\u53d8\u91cf<code>user_input<\/code>\u4e2d\uff0c\u968f\u540e\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528GUI\u5e93<\/strong><\/h2>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u7528\u4e8e\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u7684\u5e93\uff0c\u4f8b\u5982Tkinter\u3001PyQt\u548cKivy\u7b49\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ec4\u4ef6\uff0c\u5305\u62ec\u8f93\u5165\u6846\u3001\u6309\u94ae\u3001\u6807\u7b7e\u7b49\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u4ea4\u4e92\u5f0f\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h2>2.1\u3001\u4f7f\u7528Tkinter\u5e93<\/h2>\n<\/p>\n<p><h3>Tkinter\u7b80\u4ecb\uff1a<\/h3>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u5b83\u7b80\u5355\u6613\u7528\u4e14\u529f\u80fd\u5f3a\u5927\u3002\u901a\u8fc7Tkinter\uff0c\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u7a97\u53e3\u3001\u6807\u7b7e\u3001\u6309\u94ae\u548c\u8f93\u5165\u6846\u7b49\u7ec4\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def get_input():<\/p>\n<p>    user_input = entry.get()<\/p>\n<p>    print(&quot;\u4f60\u8f93\u5165\u7684\u5185\u5bb9\u662f\uff1a&quot;, user_input)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;\u8f93\u5165\u6846\u793a\u4f8b&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6807\u7b7e\u548c\u8f93\u5165\u6846<\/strong><\/h2>\n<p>label = tk.Label(root, text=&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u5185\u5bb9\uff1a&quot;)<\/p>\n<p>label.pack()<\/p>\n<p>entry = tk.Entry(root)<\/p>\n<p>entry.pack()<\/p>\n<h2><strong>\u521b\u5efa\u6309\u94ae<\/strong><\/h2>\n<p>button = tk.Button(root, text=&quot;\u63d0\u4ea4&quot;, command=get_input)<\/p>\n<p>button.pack()<\/p>\n<h2><strong>\u8fd0\u884c\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><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165tkinter\u5e93<\/strong>\uff1a\u901a\u8fc7<code>import tkinter as tk<\/code>\u5bfc\u5165Tkinter\u5e93\u3002<\/li>\n<li><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong>\uff1a\u4f7f\u7528<code>tk.Tk()<\/code>\u521b\u5efa\u4e3b\u7a97\u53e3\uff0c\u5e76\u8bbe\u7f6e\u7a97\u53e3\u6807\u9898\u3002<\/li>\n<li><strong>\u521b\u5efa\u6807\u7b7e\u548c\u8f93\u5165\u6846<\/strong>\uff1a\u4f7f\u7528<code>tk.Label()<\/code>\u548c<code>tk.Entry()<\/code>\u5206\u522b\u521b\u5efa\u6807\u7b7e\u548c\u8f93\u5165\u6846\uff0c\u5e76\u901a\u8fc7<code>pack()<\/code>\u65b9\u6cd5\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230\u7a97\u53e3\u4e2d\u3002<\/li>\n<li><strong>\u521b\u5efa\u6309\u94ae<\/strong>\uff1a\u4f7f\u7528<code>tk.Button()<\/code>\u521b\u5efa\u6309\u94ae\uff0c\u5e76\u8bbe\u7f6e\u6309\u94ae\u7684\u6587\u672c\u548c\u70b9\u51fb\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570<code>get_input<\/code>\u3002<\/li>\n<li><strong>\u8fd0\u884c\u4e3b\u5faa\u73af<\/strong>\uff1a\u4f7f\u7528<code>root.mainloop()<\/code>\u8fd0\u884c\u4e3b\u5faa\u73af\uff0c\u4f7f\u7a97\u53e3\u4fdd\u6301\u663e\u793a\u72b6\u6001\u3002<\/li>\n<\/ol>\n<hr>\n<p><h2>2.2\u3001\u4f7f\u7528PyQt\u5e93<\/h2>\n<\/p>\n<p><h3>PyQt\u7b80\u4ecb\uff1a<\/h3>\n<\/p>\n<p><p>PyQt\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684Python GUI\u5e93\uff0c\u5b83\u57fa\u4e8eQt\u6846\u67b6\u3002PyQt\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ec4\u4ef6\u548c\u529f\u80fd\uff0c\u53ef\u4ee5\u521b\u5efa\u590d\u6742\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton<\/p>\n<p>def get_input():<\/p>\n<p>    user_input = entry.text()<\/p>\n<p>    print(&quot;\u4f60\u8f93\u5165\u7684\u5185\u5bb9\u662f\uff1a&quot;, user_input)<\/p>\n<h2><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u5b9e\u4f8b<\/strong><\/h2>\n<p>app = QApplication([])<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>window = QWidget()<\/p>\n<p>window.setWindowTitle(&quot;\u8f93\u5165\u6846\u793a\u4f8b&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5e03\u5c40\u3001\u6807\u7b7e\u548c\u8f93\u5165\u6846<\/strong><\/h2>\n<p>layout = QVBoxLayout()<\/p>\n<p>label = QLabel(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u5185\u5bb9\uff1a&quot;)<\/p>\n<p>layout.addWidget(label)<\/p>\n<p>entry = QLineEdit()<\/p>\n<p>layout.addWidget(entry)<\/p>\n<h2><strong>\u521b\u5efa\u6309\u94ae<\/strong><\/h2>\n<p>button = QPushButton(&quot;\u63d0\u4ea4&quot;)<\/p>\n<p>button.clicked.connect(get_input)<\/p>\n<p>layout.addWidget(button)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5e03\u5c40\u5e76\u663e\u793a\u7a97\u53e3<\/strong><\/h2>\n<p>window.setLayout(layout)<\/p>\n<p>window.show()<\/p>\n<h2><strong>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>app.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165PyQt\u5e93<\/strong>\uff1a\u901a\u8fc7<code>from PyQt5.QtWidgets import ...<\/code>\u5bfc\u5165\u5fc5\u8981\u7684\u7ec4\u4ef6\u3002<\/li>\n<li><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u5b9e\u4f8b<\/strong>\uff1a\u4f7f\u7528<code>QApplication<\/code>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u5b9e\u4f8b\u3002<\/li>\n<li><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong>\uff1a\u4f7f\u7528<code>QWidget<\/code>\u521b\u5efa\u4e3b\u7a97\u53e3\uff0c\u5e76\u8bbe\u7f6e\u7a97\u53e3\u6807\u9898\u3002<\/li>\n<li><strong>\u521b\u5efa\u5e03\u5c40\u3001\u6807\u7b7e\u548c\u8f93\u5165\u6846<\/strong>\uff1a\u4f7f\u7528<code>QVBoxLayout<\/code>\u521b\u5efa\u5782\u76f4\u5e03\u5c40\uff0c\u5e76\u4f9d\u6b21\u6dfb\u52a0\u6807\u7b7e\u548c\u8f93\u5165\u6846\u5230\u5e03\u5c40\u4e2d\u3002<\/li>\n<li><strong>\u521b\u5efa\u6309\u94ae<\/strong>\uff1a\u4f7f\u7528<code>QPushButton<\/code>\u521b\u5efa\u6309\u94ae\uff0c\u5e76\u8fde\u63a5\u6309\u94ae\u7684\u70b9\u51fb\u4e8b\u4ef6\u5230<code>get_input<\/code>\u51fd\u6570\u3002<\/li>\n<li><strong>\u8bbe\u7f6e\u5e03\u5c40\u5e76\u663e\u793a\u7a97\u53e3<\/strong>\uff1a\u5c06\u5e03\u5c40\u8bbe\u7f6e\u5230\u7a97\u53e3\u4e2d\uff0c\u5e76\u663e\u793a\u7a97\u53e3\u3002<\/li>\n<li><strong>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f<\/strong>\uff1a\u4f7f\u7528<code>app.exec_()<\/code>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f7f\u7a97\u53e3\u4fdd\u6301\u663e\u793a\u72b6\u6001\u3002<\/li>\n<\/ol>\n<hr>\n<p><h2>2.3\u3001\u4f7f\u7528Kivy\u5e93<\/h2>\n<\/p>\n<p><h3>Kivy\u7b80\u4ecb\uff1a<\/h3>\n<\/p>\n<p><p>Kivy\u662f\u4e00\u4e2a\u5f00\u6e90\u7684Python\u5e93\uff0c\u7528\u4e8e\u5feb\u901f\u5f00\u53d1\u5177\u6709\u521b\u65b0\u7528\u6237\u754c\u9762\u7684\u5e94\u7528\u7a0b\u5e8f\u3002Kivy\u652f\u6301\u591a\u70b9\u89e6\u63a7\u548c\u591a\u79cd\u8f93\u5165\u8bbe\u5907\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u79fb\u52a8\u5e94\u7528\u5f00\u53d1\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\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>        layout = BoxLayout(orientation=&#39;vertical&#39;)<\/p>\n<p>        self.label = Label(text=&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u5185\u5bb9\uff1a&quot;)<\/p>\n<p>        layout.add_widget(self.label)<\/p>\n<p>        self.input = TextInput()<\/p>\n<p>        layout.add_widget(self.input)<\/p>\n<p>        button = Button(text=&quot;\u63d0\u4ea4&quot;)<\/p>\n<p>        button.bind(on_press=self.get_input)<\/p>\n<p>        layout.add_widget(button)<\/p>\n<p>        return layout<\/p>\n<p>    def get_input(self, instance):<\/p>\n<p>        user_input = self.input.text<\/p>\n<p>        print(&quot;\u4f60\u8f93\u5165\u7684\u5185\u5bb9\u662f\uff1a&quot;, user_input)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165Kivy\u5e93<\/strong>\uff1a\u901a\u8fc7<code>from kivy.app import ...<\/code>\u5bfc\u5165\u5fc5\u8981\u7684\u7ec4\u4ef6\u3002<\/li>\n<li><strong>\u5b9a\u4e49\u5e94\u7528\u7a0b\u5e8f\u7c7b<\/strong>\uff1a\u7ee7\u627f<code>App<\/code>\u7c7b\uff0c\u5b9a\u4e49\u5e94\u7528\u7a0b\u5e8f\u7c7b<code>MyApp<\/code>\uff0c\u5e76\u5b9e\u73b0<code>build<\/code>\u65b9\u6cd5\u3002<\/li>\n<li><strong>\u521b\u5efa\u5e03\u5c40\u3001\u6807\u7b7e\u548c\u8f93\u5165\u6846<\/strong>\uff1a\u4f7f\u7528<code>BoxLayout<\/code>\u521b\u5efa\u5782\u76f4\u5e03\u5c40\uff0c\u5e76\u4f9d\u6b21\u6dfb\u52a0\u6807\u7b7e\u548c\u8f93\u5165\u6846\u5230\u5e03\u5c40\u4e2d\u3002<\/li>\n<li><strong>\u521b\u5efa\u6309\u94ae<\/strong>\uff1a\u4f7f\u7528<code>Button<\/code>\u521b\u5efa\u6309\u94ae\uff0c\u5e76\u7ed1\u5b9a\u6309\u94ae\u7684\u70b9\u51fb\u4e8b\u4ef6\u5230<code>get_input<\/code>\u65b9\u6cd5\u3002<\/li>\n<li><strong>\u5b9e\u73b0\u8f93\u5165\u83b7\u53d6\u903b\u8f91<\/strong>\uff1a\u5728<code>get_input<\/code>\u65b9\u6cd5\u4e2d\uff0c\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u5e76\u6253\u5370\u3002<\/li>\n<li><strong>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f<\/strong>\uff1a\u901a\u8fc7<code>MyApp().run()<\/code>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<\/ol>\n<hr>\n<h2><strong>\u4e09\u3001\u4f7f\u7528Web\u6846\u67b6<\/strong><\/h2>\n<p><p>Python\u8fd8\u53ef\u4ee5\u901a\u8fc7Web\u6846\u67b6\u6765\u521b\u5efaWeb\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u83b7\u53d6\u7528\u6237\u5728\u7f51\u9875\u8f93\u5165\u6846\u4e2d\u7684\u5185\u5bb9\u3002\u5e38\u7528\u7684Web\u6846\u67b6\u5305\u62ecFlask\u548cDjango\u3002<\/p>\n<\/p>\n<p><h2>3.1\u3001\u4f7f\u7528Flask\u6846\u67b6<\/h2>\n<\/p>\n<p><h3>Flask\u7b80\u4ecb\uff1a<\/h3>\n<\/p>\n<p><p>Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Web\u6846\u67b6\uff0c\u5b83\u7b80\u5355\u6613\u7528\u4e14\u7075\u6d3b\uff0c\u9002\u5408\u5c0f\u578b\u5e94\u7528\u7a0b\u5e8f\u548c\u5feb\u901f\u5f00\u53d1\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request, render_template_string<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;, methods=[&#39;GET&#39;, &#39;POST&#39;])<\/p>\n<p>def index():<\/p>\n<p>    if request.method == &#39;POST&#39;:<\/p>\n<p>        user_input = request.form[&#39;input&#39;]<\/p>\n<p>        return f&quot;\u4f60\u8f93\u5165\u7684\u5185\u5bb9\u662f\uff1a{user_input}&quot;<\/p>\n<p>    return &#39;&#39;&#39;<\/p>\n<p>        &lt;form method=&quot;post&quot;&gt;<\/p>\n<p>            \u8bf7\u8f93\u5165\u4e00\u4e9b\u5185\u5bb9\uff1a&lt;input type=&quot;text&quot; name=&quot;input&quot;&gt;<\/p>\n<p>            &lt;input type=&quot;submit&quot;&gt;<\/p>\n<p>        &lt;\/form&gt;<\/p>\n<p>    &#39;&#39;&#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><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165Flask\u5e93<\/strong>\uff1a\u901a\u8fc7<code>from flask import Flask, request<\/code>\u5bfc\u5165\u5fc5\u8981\u7684\u7ec4\u4ef6\u3002<\/li>\n<li><strong>\u521b\u5efaFlask\u5e94\u7528\u5b9e\u4f8b<\/strong>\uff1a\u4f7f\u7528<code>Flask(__name__)<\/code>\u521b\u5efaFlask\u5e94\u7528\u5b9e\u4f8b\u3002<\/li>\n<li><strong>\u5b9a\u4e49\u8def\u7531\u548c\u89c6\u56fe\u51fd\u6570<\/strong>\uff1a\u901a\u8fc7<code>@app.route<\/code>\u88c5\u9970\u5668\u5b9a\u4e49\u8def\u7531\u548c\u89c6\u56fe\u51fd\u6570<code>index<\/code>\uff0c\u5904\u7406GET\u548cPOST\u8bf7\u6c42\u3002<\/li>\n<li><strong>\u83b7\u53d6\u7528\u6237\u8f93\u5165\u5185\u5bb9<\/strong>\uff1a\u5728\u89c6\u56fe\u51fd\u6570\u4e2d\uff0c\u901a\u8fc7<code>request.form<\/code>\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\uff0c\u5e76\u8fd4\u56de\u663e\u793a\u3002<\/li>\n<li><strong>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f<\/strong>\uff1a\u901a\u8fc7<code>app.run(debug=True)<\/code>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<\/ol>\n<hr>\n<p><h2>3.2\u3001\u4f7f\u7528Django\u6846\u67b6<\/h2>\n<\/p>\n<p><h3>Django\u7b80\u4ecb\uff1a<\/h3>\n<\/p>\n<p><p>Django\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684Web\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ec4\u4ef6\u548c\u529f\u80fd\uff0c\u9002\u5408\u5927\u578b\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Django\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install django<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Django\u9879\u76ee\u548c\u5e94\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">django-admin startproject myproject<\/p>\n<p>cd myproject<\/p>\n<p>python manage.py startapp myapp<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7f16\u8f91<code>myproject\/myapp\/views.py<\/code>\u6587\u4ef6\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.shortcuts import render<\/p>\n<p>from django.http import HttpResponse<\/p>\n<p>def index(request):<\/p>\n<p>    if request.method == &#39;POST&#39;:<\/p>\n<p>        user_input = request.POST[&#39;input&#39;]<\/p>\n<p>        return HttpResponse(f&quot;\u4f60\u8f93\u5165\u7684\u5185\u5bb9\u662f\uff1a{user_input}&quot;)<\/p>\n<p>    return render(request, &#39;index.html&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa<code>myproject\/myapp\/templates\/index.html<\/code>\u6a21\u677f\u6587\u4ef6\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\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;\u8f93\u5165\u6846\u793a\u4f8b&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;form method=&quot;post&quot;&gt;<\/p>\n<p>        {% csrf_token %}<\/p>\n<p>        \u8bf7\u8f93\u5165\u4e00\u4e9b\u5185\u5bb9\uff1a&lt;input type=&quot;text&quot; name=&quot;input&quot;&gt;<\/p>\n<p>        &lt;input type=&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><p>\u7f16\u8f91<code>myproject\/myproject\/urls.py<\/code>\u6587\u4ef6\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.contrib import admin<\/p>\n<p>from django.urls import path<\/p>\n<p>from myapp import views<\/p>\n<p>urlpatterns = [<\/p>\n<p>    path(&#39;admin\/&#39;, admin.site.urls),<\/p>\n<p>    path(&#39;&#39;, views.index),<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u8fd0\u884cDjango\u5f00\u53d1\u670d\u52a1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python manage.py runserver<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u8bbf\u95ee<code>http:\/\/127.0.0.1:8000<\/code>\uff0c\u5373\u53ef\u770b\u5230\u8f93\u5165\u6846\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5Django\u5e93<\/strong>\uff1a\u901a\u8fc7<code>pip install django<\/code>\u5b89\u88c5Django\u5e93\u3002<\/li>\n<li><strong>\u521b\u5efaDjango\u9879\u76ee\u548c\u5e94\u7528<\/strong>\uff1a\u4f7f\u7528<code>django-admin startproject<\/code>\u548c<code>python manage.py startapp<\/code>\u521b\u5efa\u9879\u76ee\u548c\u5e94\u7528\u3002<\/li>\n<li><strong>\u5b9a\u4e49\u89c6\u56fe\u51fd\u6570<\/strong>\uff1a\u5728<code>views.py<\/code>\u6587\u4ef6\u4e2d\u5b9a\u4e49\u89c6\u56fe\u51fd\u6570<code>index<\/code>\uff0c\u5904\u7406GET\u548cPOST\u8bf7\u6c42\u3002<\/li>\n<li><strong>\u521b\u5efa\u6a21\u677f\u6587\u4ef6<\/strong>\uff1a\u5728<code>templates<\/code>\u76ee\u5f55\u4e0b\u521b\u5efaHTML\u6a21\u677f\u6587\u4ef6\uff0c\u5305\u542b\u8f93\u5165\u6846\u548c\u63d0\u4ea4\u6309\u94ae\u3002<\/li>\n<li><strong>\u914d\u7f6eURL\u8def\u7531<\/strong>\uff1a\u5728<code>urls.py<\/code>\u6587\u4ef6\u4e2d\u914d\u7f6eURL\u8def\u7531\uff0c\u5c06\u6839URL\u6620\u5c04\u5230\u89c6\u56fe\u51fd\u6570<code>index<\/code>\u3002<\/li>\n<li><strong>\u8fd0\u884c\u5f00\u53d1\u670d\u52a1\u5668<\/strong>\uff1a\u901a\u8fc7<code>python manage.py runserver<\/code>\u8fd0\u884cDjango\u5f00\u53d1\u670d\u52a1\u5668\u3002<\/li>\n<\/ol>\n<hr>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0cPython\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u8f93\u5165\u6846\u7684\u5185\u5bb9\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u547d\u4ee4\u884c\u8f93\u5165\u3001\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0c\u8fd8\u662fWeb\u5e94\u7528\u7a0b\u5e8f\uff0c\u90fd\u6709\u76f8\u5e94\u7684\u89e3\u51b3\u65b9\u6848\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u80fd\u591f\u6709\u6548\u63d0\u5347\u5f00\u53d1\u6548\u7387\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u83b7\u53d6\u7528\u6237\u8f93\u5165\u4e3b\u8981\u901a\u8fc7\u5185\u7f6e\u7684<code>input()<\/code>\u51fd\u6570\u3002\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u6682\u505c\u5e76\u7b49\u5f85\u7528\u6237\u8f93\u5165\uff0c\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u4f1a\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fd4\u56de\u3002\u53ef\u4ee5\u901a\u8fc7\u5728<code>input()<\/code>\u51fd\u6570\u4e2d\u6dfb\u52a0\u63d0\u793a\u4fe1\u606f\uff0c\u6765\u5f15\u5bfc\u7528\u6237\u8f93\u5165\u6240\u9700\u7684\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5728GUI\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u5982\u4f55\u83b7\u53d6\u8f93\u5165\u6846\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\u5982Tkinter\u6216PyQt\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u8f93\u5165\u6846\u5e76\u4f7f\u7528\u76f8\u5e94\u7684\u65b9\u6cd5\u83b7\u53d6\u8f93\u5165\u6846\u7684\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u5728Tkinter\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Entry<\/code>\u5c0f\u90e8\u4ef6\u521b\u5efa\u8f93\u5165\u6846\uff0c\u5e76\u901a\u8fc7\u8c03\u7528<code>get()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8f93\u5165\u6846\u5185\u5bb9\u7684\u9a8c\u8bc1\uff1f<\/strong><br \/>\u5728\u83b7\u53d6\u8f93\u5165\u6846\u7684\u5185\u5bb9\u540e\uff0c\u901a\u5e38\u9700\u8981\u5bf9\u8f93\u5165\u8fdb\u884c\u9a8c\u8bc1\u3002\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u68c0\u67e5\u8f93\u5165\u662f\u5426\u7b26\u5408\u9884\u671f\u683c\u5f0f\uff0c\u4f8b\u5982\u68c0\u67e5\u662f\u5426\u4e3a\u7a7a\u3001\u662f\u5426\u4e3a\u6570\u5b57\u6216\u8005\u662f\u5426\u7b26\u5408\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\u6a21\u5f0f\u3002\u901a\u8fc7\u8fd9\u4e9b\u9a8c\u8bc1\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u786e\u4fdd\u8f93\u5165\u6570\u636e\u7684\u6709\u6548\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u83b7\u53d6\u8f93\u5165\u6846\u5185\u5bb9\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u8f93\u5165\u51fd\u6570\u3001\u4f7f\u7528GUI\u5e93\u3001\u4f7f\u7528Web\u6846\u67b6\u3002\u5176\u4e2d\uff0c\u4f7f\u7528GUI\u5e93\u662f\u8f83\u4e3a\u5e38\u89c1 [&hellip;]","protected":false},"author":3,"featured_media":1043973,"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\/1043965"}],"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=1043965"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1043965\/revisions"}],"predecessor-version":[{"id":1043977,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1043965\/revisions\/1043977"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1043973"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1043965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1043965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1043965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}