{"id":1080642,"date":"2025-01-08T12:32:00","date_gmt":"2025-01-08T04:32:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1080642.html"},"modified":"2025-01-08T12:32:03","modified_gmt":"2025-01-08T04:32:03","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%be%93%e5%85%a5%e4%b8%80%e6%ae%b5%e6%96%87%e5%ad%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1080642.html","title":{"rendered":"\u5982\u4f55\u7528python\u8f93\u5165\u4e00\u6bb5\u6587\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183057\/055dc217-d8a4-44f1-8241-75da5bee2cb4.webp\" alt=\"\u5982\u4f55\u7528python\u8f93\u5165\u4e00\u6bb5\u6587\u5b57\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u8f93\u5165\u4e00\u6bb5\u6587\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528input()\u51fd\u6570\u3001\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684sys\u6a21\u5757\u3001\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff08\u5982PyQt\u6216Tkinter\uff09<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u89c1\u548c\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528input()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>input()<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u4ece\u7528\u6237\u8f93\u5165\u83b7\u53d6\u6570\u636e\u3002\u5b83\u4f1a\u6682\u505c\u7a0b\u5e8f\u6267\u884c\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165\u6570\u636e\u5e76\u6309\u4e0bEnter\u952e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 input() \u51fd\u6570\u8f93\u5165\u4e00\u6bb5\u6587\u5b57<\/p>\n<p>user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u6bb5\u6587\u5b57\uff1a&quot;)<\/p>\n<p>print(&quot;\u4f60\u8f93\u5165\u7684\u6587\u5b57\u662f\uff1a&quot;, user_input)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u6bb5\u6587\u5b57\uff0c\u5e76\u5c06\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u5b58\u50a8\u5728\u53d8\u91cf<code>user_input<\/code>\u4e2d\uff0c\u6700\u540e\u8f93\u51fa\u7528\u6237\u8f93\u5165\u7684\u6587\u5b57\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> <code>input()<\/code>\u51fd\u6570\u7684\u4f7f\u7528\u975e\u5e38\u7b80\u5355\u548c\u76f4\u63a5\u3002\u5b83\u53ef\u4ee5\u63a5\u6536\u7528\u6237\u8f93\u5165\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u7b80\u5355\u7528\u6237\u4ea4\u4e92\u7684\u7a0b\u5e8f\u3002\u7531\u4e8e\u5176\u7b80\u5355\u6027\u548c\u76f4\u89c2\u6027\uff0c<code>input()<\/code>\u51fd\u6570\u901a\u5e38\u662f\u521d\u5b66\u8005\u5b66\u4e60Python\u65f6\u9996\u5148\u63a5\u89e6\u7684\u8f93\u5165\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684sys\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684<code>sys<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u89e3\u91ca\u5668\u4f7f\u7528\u73af\u5883\u7684\u8bbf\u95ee\uff0c\u53ef\u4ee5\u7528\u6765\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u63d0\u793a\u7528\u6237\u8f93\u5165<\/strong><\/h2>\n<p>print(&quot;\u8bf7\u8f93\u5165\u4e00\u6bb5\u6587\u5b57\uff1a&quot;)<\/p>\n<h2><strong>\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>user_input = sys.stdin.read()<\/p>\n<p>print(&quot;\u4f60\u8f93\u5165\u7684\u6587\u5b57\u662f\uff1a&quot;, user_input)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u6570\u636e\uff0c\u76f4\u5230\u7528\u6237\u4f7f\u7528EOF\uff08Ctrl+D\u6216Ctrl+Z\uff09\u7ed3\u675f\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\u53ef\u4ee5\u63d0\u4f9b\u66f4\u52a0\u53cb\u597d\u548c\u76f4\u89c2\u7684\u7528\u6237\u4f53\u9a8c\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cdGUI\u5e93\uff0c\u5982Tkinter\u548cPyQt\u3002\u4e0b\u9762\u662f\u4f7f\u7528Tkinter\u521b\u5efa\u7b80\u5355\u8f93\u5165\u6846\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import simpledialog<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.withdraw()  # \u9690\u85cf\u4e3b\u7a97\u53e3<\/p>\n<h2><strong>\u63d0\u793a\u7528\u6237\u8f93\u5165<\/strong><\/h2>\n<p>user_input = simpledialog.askstring(&quot;\u8f93\u5165&quot;, &quot;\u8bf7\u8f93\u5165\u4e00\u6bb5\u6587\u5b57\uff1a&quot;)<\/p>\n<p>print(&quot;\u4f60\u8f93\u5165\u7684\u6587\u5b57\u662f\uff1a&quot;, user_input)<\/p>\n<h2><strong>\u5173\u95ed\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root.destroy()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>simpledialog.askstring<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u8f93\u5165\u5bf9\u8bdd\u6846\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u5bf9\u8bdd\u6846\u4e2d\u8f93\u5165\u6587\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Tkinter\uff0cPyQt\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684GUI\u5e93\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PyQt\u521b\u5efa\u7b80\u5355\u8f93\u5165\u6846\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QInputDialog<\/p>\n<h2><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61<\/strong><\/h2>\n<p>app = QApplication([])<\/p>\n<h2><strong>\u63d0\u793a\u7528\u6237\u8f93\u5165<\/strong><\/h2>\n<p>text, ok = QInputDialog.getText(None, &quot;\u8f93\u5165&quot;, &quot;\u8bf7\u8f93\u5165\u4e00\u6bb5\u6587\u5b57\uff1a&quot;)<\/p>\n<p>if ok:<\/p>\n<p>    print(&quot;\u4f60\u8f93\u5165\u7684\u6587\u5b57\u662f\uff1a&quot;, text)<\/p>\n<h2><strong>\u9000\u51fa\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>app.exit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>QInputDialog.getText<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u8f93\u5165\u5bf9\u8bdd\u6846\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u5bf9\u8bdd\u6846\u4e2d\u8f93\u5165\u6587\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c<strong>\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u3001\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684sys\u6a21\u5757\u3001\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff08\u5982PyQt\u6216Tkinter\uff09<\/strong>\u90fd\u662f\u5728Python\u4e2d\u8f93\u5165\u4e00\u6bb5\u6587\u5b57\u7684\u6709\u6548\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u547d\u4ee4\u884c\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\uff1b\u5982\u679c\u9700\u8981\u5904\u7406\u6807\u51c6\u8f93\u5165\u8f93\u51fa\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys<\/code>\u6a21\u5757\uff1b\u5982\u679c\u9700\u8981\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0c\u53ef\u4ee5\u4f7f\u7528Tkinter\u6216PyQt\u7b49GUI\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u4e86\u89e3\u5e76\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u5347\u7a0b\u5e8f\u7684\u7528\u6237\u4ea4\u4e92\u4f53\u9a8c\uff0c\u8fd8\u80fd\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u6587\u672c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>input()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u6587\u672c\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u6682\u505c\u7a0b\u5e8f\u7684\u6267\u884c\uff0c\u7b49\u5f85\u7528\u6237\u5728\u63a7\u5236\u53f0\u8f93\u5165\u6587\u672c\uff0c\u5e76\u6309\u4e0b\u56de\u8f66\u952e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u6bb5\u6587\u5b57\uff1a&quot;)\nprint(&quot;\u60a8\u8f93\u5165\u7684\u5185\u5bb9\u662f\uff1a&quot;, user_input)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u6bb5\u6587\u5b57\uff0c\u5e76\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u6587\u672c\uff1f<\/strong><br \/>\u5728\u83b7\u53d6\u7528\u6237\u8f93\u5165\u540e\uff0c\u53ef\u4ee5\u5bf9\u8f93\u5165\u7684\u6587\u672c\u8fdb\u884c\u5404\u79cd\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u6765\u4fee\u6539\u6587\u672c\uff0c\u6216\u8005\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u641c\u7d22\u7279\u5b9a\u6a21\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5c06\u7528\u6237\u8f93\u5165\u7684\u6587\u672c\u8f6c\u6362\u4e3a\u5927\u5199\uff1a  <\/p>\n<pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u6bb5\u6587\u5b57\uff1a&quot;)\nuppercase_input = user_input.upper()\nprint(&quot;\u8f6c\u6362\u4e3a\u5927\u5199\u540e\uff1a&quot;, uppercase_input)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u628a\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u8f6c\u6362\u4e3a\u5927\u5199\u5e76\u8f93\u51fa\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u6709\u6548\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u7b26\u5408\u7279\u5b9a\u7684\u8981\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u8fdb\u884c\u9a8c\u8bc1\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u68c0\u67e5\u7528\u6237\u8f93\u5165\u662f\u5426\u4e3a\u7a7a\u6216\u662f\u5426\u7b26\u5408\u7279\u5b9a\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u6bb5\u6587\u5b57\uff1a&quot;)\nif user_input.strip():  # \u68c0\u67e5\u8f93\u5165\u662f\u5426\u4e3a\u7a7a\n    print(&quot;\u60a8\u8f93\u5165\u7684\u5185\u5bb9\u662f\uff1a&quot;, user_input)\nelse:\n    print(&quot;\u8f93\u5165\u4e0d\u80fd\u4e3a\u7a7a\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u68c0\u67e5\u7528\u6237\u8f93\u5165\u662f\u5426\u4e3a\u7a7a\uff0c\u5e76\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u8f93\u5165\u4e00\u6bb5\u6587\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528input()\u51fd\u6570\u3001\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684sys\u6a21\u5757\u3001\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u3001\u4f7f\u7528\u7b2c\u4e09 [&hellip;]","protected":false},"author":3,"featured_media":1080647,"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\/1080642"}],"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=1080642"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080642\/revisions"}],"predecessor-version":[{"id":1080648,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080642\/revisions\/1080648"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1080647"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1080642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1080642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1080642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}