{"id":933094,"date":"2024-12-26T18:10:21","date_gmt":"2024-12-26T10:10:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/933094.html"},"modified":"2024-12-26T18:10:23","modified_gmt":"2024-12-26T10:10:23","slug":"python%e5%a6%82%e4%bd%95%e6%94%b9%e8%be%93%e5%85%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/933094.html","title":{"rendered":"python\u5982\u4f55\u6539\u8f93\u5165"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070654\/187648a9-3b65-40ad-b5d1-98082a826ad6.webp\" alt=\"python\u5982\u4f55\u6539\u8f93\u5165\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4fee\u6539\u8f93\u5165\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528\u66ff\u4ee3\u8f93\u5165\u51fd\u6570\u3001\u4fee\u6539\u6807\u51c6\u8f93\u5165\u6d41\u3001\u4f7f\u7528GUI\u5e93\u7b49<\/strong>\u3002\u66ff\u4ee3\u8f93\u5165\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u5305\u88c5\u6216\u91cd\u5199\u5185\u7f6e\u8f93\u5165\u51fd\u6570\u6765\u5b9e\u73b0\uff0c\u4fee\u6539\u6807\u51c6\u8f93\u5165\u6d41\u5219\u53ef\u4ee5\u901a\u8fc7\u91cd\u5b9a\u5411sys.stdin\u6765\u5b9e\u73b0\uff0c\u4f7f\u7528GUI\u5e93\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u6765\u63a5\u6536\u7528\u6237\u8f93\u5165\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u66ff\u4ee3\u8f93\u5165\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u66ff\u4ee3\u8f93\u5165\u51fd\u6570\u662f\u901a\u8fc7\u5b9a\u4e49\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u66ff\u4ee3Python\u5185\u7f6e\u7684<code>input()<\/code>\u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u5e76\u4e14\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\uff0c\u5982\u8f93\u5165\u9a8c\u8bc1\u3001\u683c\u5f0f\u5316\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u81ea\u5b9a\u4e49\u8f93\u5165\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u65b0\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u5176\u4e2d\u5b9e\u73b0\u5bf9\u7528\u6237\u8f93\u5165\u7684\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u6dfb\u52a0\u6570\u636e\u9a8c\u8bc1\u3001\u5f02\u5e38\u5904\u7406\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_input(prompt):<\/p>\n<p>    while True:<\/p>\n<p>        try:<\/p>\n<p>            user_input = input(prompt)<\/p>\n<p>            # \u5728\u8fd9\u91cc\u53ef\u4ee5\u8fdb\u884c\u8f93\u5165\u9a8c\u8bc1\u6216\u683c\u5f0f\u5316<\/p>\n<p>            if not user_input:<\/p>\n<p>                r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;\u8f93\u5165\u4e0d\u80fd\u4e3a\u7a7a\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;)<\/p>\n<p>            return user_input<\/p>\n<p>        except ValueError as e:<\/p>\n<p>            print(e)<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u8f93\u5165\u51fd\u6570<\/strong><\/h2>\n<p>name = custom_input(&quot;\u8bf7\u8f93\u5165\u60a8\u7684\u540d\u5b57: &quot;)<\/p>\n<p>print(f&quot;\u60a8\u597d, {name}!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u81ea\u52a8\u5316\u6d4b\u8bd5\u4e2d\u7684\u6a21\u62df\u8f93\u5165<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u8fdb\u884c\u81ea\u52a8\u5316\u6d4b\u8bd5\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u6a21\u62df\u7528\u6237\u8f93\u5165\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>unittest<\/code>\u5e93\u4e2d\u7684<code>patch<\/code>\u529f\u80fd\u6765\u6a21\u62df\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from unittest.mock import patch<\/p>\n<p>def get_username():<\/p>\n<p>    return input(&quot;\u8bf7\u8f93\u5165\u60a8\u7684\u7528\u6237\u540d: &quot;)<\/p>\n<h2><strong>\u4f7f\u7528patch\u6a21\u62df\u8f93\u5165<\/strong><\/h2>\n<p>with patch(&#39;builtins.input&#39;, return_value=&#39;test_user&#39;):<\/p>\n<p>    username = get_username()<\/p>\n<p>    print(f&quot;\u7528\u6237\u540d\u662f: {username}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u4e0d\u9700\u8981\u5b9e\u9645\u7528\u6237\u8f93\u5165\u7684\u60c5\u51b5\u4e0b\u6d4b\u8bd5\u7a0b\u5e8f\u7684\u4e0d\u540c\u8f93\u5165\u60c5\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4fee\u6539\u6807\u51c6\u8f93\u5165\u6d41<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4fee\u6539Python\u7684\u6807\u51c6\u8f93\u5165\u6d41\uff0c\u53ef\u4ee5\u4ece\u6587\u4ef6\u3001\u7f51\u7edc\u7b49\u591a\u79cd\u6765\u6e90\u63a5\u6536\u8f93\u5165\u6570\u636e\u3002<code>sys.stdin<\/code>\u53ef\u4ee5\u88ab\u66ff\u6362\u4e3a\u5176\u4ed6\u8f93\u5165\u6d41\u5bf9\u8c61\uff0c\u4ece\u800c\u5b9e\u73b0\u5bf9\u8f93\u5165\u6e90\u7684\u81ea\u5b9a\u4e49\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4ece\u6587\u4ef6\u8bfb\u53d6\u8f93\u5165<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5c06<code>sys.stdin<\/code>\u91cd\u5b9a\u5411\u5230\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61\u6765\u6a21\u62df\u8f93\u5165\u3002\u8fd9\u5728\u9700\u8981\u8fdb\u884c\u6279\u91cf\u5904\u7406\u6216\u6d4b\u8bd5\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3ainput.txt\u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u6a21\u62df\u8f93\u5165\u7684\u6570\u636e<\/strong><\/h2>\n<p>with open(&#39;input.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    sys.stdin = file<\/p>\n<p>    # \u5728\u8fd9\u91cc\u8c03\u7528\u9700\u8981\u8bfb\u53d6\u8f93\u5165\u7684\u51fd\u6570<\/p>\n<p>    data = input()<\/p>\n<p>    print(f&quot;\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u7684\u6570\u636e: {data}&quot;)<\/p>\n<h2><strong>\u6062\u590dsys.stdin<\/strong><\/h2>\n<p>sys.stdin = sys.__stdin__<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4ece\u5176\u4ed6\u8f93\u5165\u6d41\u8bfb\u53d6<\/strong><\/li>\n<\/ol>\n<p><p>\u9664\u4e86\u6587\u4ef6\u5916\uff0c\u8fd8\u53ef\u4ee5\u4ece\u5176\u4ed6\u7c7b\u578b\u7684\u8f93\u5165\u6d41\u8bfb\u53d6\u6570\u636e\uff0c\u6bd4\u5982\u7f51\u7edc\u8fde\u63a5\u3001\u7ba1\u9053\u7b49\u3002\u8fd9\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u4e0e\u8fd9\u4e9b\u6765\u6e90\u76f8\u5bf9\u5e94\u7684\u8f93\u5165\u6d41\u5bf9\u8c61\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9<code>sys.stdin<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528GUI\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u56fe\u5f62\u7528\u6237\u754c\u9762\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684GUI\u5e93\uff08\u5982Tkinter\u3001PyQt\u3001Kivy\u7b49\uff09\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u4ea4\u4e92\u5f0f\u7528\u6237\u754c\u9762\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528Tkinter\u521b\u5efa\u8f93\u5165\u6846<\/strong><\/li>\n<\/ol>\n<p><p>Tkinter\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684GUI\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u57fa\u672c\u7684\u7528\u6237\u754c\u9762\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import simpledialog<\/p>\n<p>def get_user_input():<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    root.withdraw()  # \u9690\u85cf\u4e3b\u7a97\u53e3<\/p>\n<p>    # \u5f39\u51fa\u8f93\u5165\u6846<\/p>\n<p>    user_input = simpledialog.askstring(title=&quot;\u8f93\u5165\u6846&quot;, prompt=&quot;\u8bf7\u8f93\u5165\u60a8\u7684\u540d\u5b57:&quot;)<\/p>\n<p>    print(f&quot;\u60a8\u597d, {user_input}!&quot;)<\/p>\n<p>    root.destroy()<\/p>\n<p>get_user_input()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528PyQt\u521b\u5efa\u8f93\u5165\u6846<\/strong><\/li>\n<\/ol>\n<p><p>PyQt\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684GUI\u5e93\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5 import QtWidgets<\/p>\n<p>def get_user_input():<\/p>\n<p>    app = QtWidgets.QApplication([])<\/p>\n<p>    user_input, ok = QtWidgets.QInputDialog.getText(None, &quot;\u8f93\u5165\u6846&quot;, &quot;\u8bf7\u8f93\u5165\u60a8\u7684\u540d\u5b57:&quot;)<\/p>\n<p>    if ok:<\/p>\n<p>        print(f&quot;\u60a8\u597d, {user_input}!&quot;)<\/p>\n<p>    app.exit()<\/p>\n<p>get_user_input()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u5728Python\u4e2d\u4fee\u6539\u8f93\u5165\u7684\u51e0\u79cd\u5e38\u7528\u65b9\u6cd5\u3002\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5bf9\u8f93\u5165\u7684\u81ea\u5b9a\u4e49\u5904\u7406\u3002\u901a\u8fc7\u81ea\u5b9a\u4e49\u8f93\u5165\u51fd\u6570\u3001\u4fee\u6539\u6807\u51c6\u8f93\u5165\u6d41\u4ee5\u53ca\u4f7f\u7528GUI\u5e93\u7b49\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7075\u6d3b\u3001\u591a\u6837\u5316\u7684\u8f93\u5165\u673a\u5236\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u4ea4\u4e92\u6027\u548c\u53ef\u7528\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4fee\u6539\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u3002\u5982\u679c\u60f3\u8981\u5728\u83b7\u53d6\u5230\u8f93\u5165\u540e\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\uff0c\u53ef\u4ee5\u5c06\u8f93\u5165\u5185\u5bb9\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u7136\u540e\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u8fdb\u884c\u64cd\u4f5c\uff0c\u6bd4\u5982<code>strip()<\/code>\u6765\u53bb\u9664\u591a\u4f59\u7684\u7a7a\u683c\uff0c\u6216<code>replace()<\/code>\u6765\u66ff\u6362\u7279\u5b9a\u7684\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u591a\u884c\u8f93\u5165\uff1f<\/strong><br \/>\u5904\u7406\u591a\u884c\u8f93\u5165\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u6765\u5b9e\u73b0\u3002\u7528\u6237\u53ef\u4ee5\u591a\u6b21\u8c03\u7528<code>input()<\/code>\u51fd\u6570\uff0c\u76f4\u5230\u8f93\u5165\u7279\u5b9a\u7684\u7ed3\u675f\u6807\u5fd7\uff0c\u5982\u201c\u7ed3\u675f\u201d\u6216\u201c\u9000\u51fa\u201d\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u5c06\u6240\u6709\u8f93\u5165\u7684\u5185\u5bb9\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u683c\u5f0f\u662f\u5426\u6b63\u786e\uff1f<\/strong><br \/>\u9a8c\u8bc1\u8f93\u5165\u683c\u5f0f\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757<code>re<\/code>\u3002\u901a\u8fc7\u7f16\u5199\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u68c0\u67e5\u7528\u6237\u8f93\u5165\u662f\u5426\u7b26\u5408\u7279\u5b9a\u7684\u683c\u5f0f\u8981\u6c42\uff0c\u6bd4\u5982\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3001\u7535\u8bdd\u53f7\u7801\u6216\u65e5\u671f\u683c\u5f0f\u7b49\u3002\u5982\u679c\u8f93\u5165\u4e0d\u7b26\u5408\u8981\u6c42\uff0c\u53ef\u4ee5\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4fee\u6539\u8f93\u5165\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528\u66ff\u4ee3\u8f93\u5165\u51fd\u6570\u3001\u4fee\u6539\u6807\u51c6\u8f93\u5165\u6d41\u3001\u4f7f\u7528GUI\u5e93\u7b49\u3002\u66ff\u4ee3\u8f93\u5165\u51fd [&hellip;]","protected":false},"author":3,"featured_media":933097,"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\/933094"}],"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=933094"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/933094\/revisions"}],"predecessor-version":[{"id":933098,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/933094\/revisions\/933098"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/933097"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=933094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=933094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=933094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}