{"id":1135438,"date":"2025-01-08T21:25:24","date_gmt":"2025-01-08T13:25:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1135438.html"},"modified":"2025-01-08T21:25:28","modified_gmt":"2025-01-08T13:25:28","slug":"%e5%a6%82%e4%bd%95%e8%bf%9b%e8%a1%8c%e7%ac%ac%e4%ba%8c%e4%b8%aa%e8%be%93%e5%85%a5python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1135438.html","title":{"rendered":"\u5982\u4f55\u8fdb\u884c\u7b2c\u4e8c\u4e2a\u8f93\u5165python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104057\/9c04ccfb-9c8b-464a-a48e-8dfac92ad288.webp\" alt=\"\u5982\u4f55\u8fdb\u884c\u7b2c\u4e8c\u4e2a\u8f93\u5165python\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8fdb\u884c\u7b2c\u4e8c\u4e2a\u8f93\u5165\u7684\u5e38\u89c1\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528input()\u51fd\u6570\u3001\u547d\u4ee4\u884c\u53c2\u6570\u3001\u6587\u4ef6\u8bfb\u53d6\u548c\u56fe\u5f62\u7528\u6237\u754c\u9762\u7b49<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528input()\u51fd\u6570<\/strong>\u662f\u6700\u76f4\u63a5\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7input()\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u5730\u63a5\u6536\u7528\u6237\u7684\u8f93\u5165\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528input()\u51fd\u6570\u8fdb\u884c\u7b2c\u4e8c\u4e2a\u8f93\u5165\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u7b2c\u4e00\u4e2a\u8f93\u5165<\/p>\n<p>first_input = input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u4e2a\u503c\uff1a&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u8f93\u5165<\/strong><\/h2>\n<p>second_input = input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u4e2a\u503c\uff1a&quot;)<\/p>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u8f93\u5165\u662f\uff1a{first_input}, \u7b2c\u4e8c\u4e2a\u8f93\u5165\u662f\uff1a{second_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u4e24\u4e2a\u8f93\u5165\u64cd\u4f5c\uff0c\u5e76\u5c06\u7ed3\u679c\u6253\u5370\u8f93\u51fa\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528input()\u51fd\u6570\u8fdb\u884c\u591a\u4e2a\u8f93\u5165<\/h3>\n<\/p>\n<p><p>input()\u51fd\u6570\u662fPython\u4e2d\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u57fa\u672c\u65b9\u6cd5\u3002\u901a\u8fc7\u8be5\u51fd\u6570\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u63a5\u6536\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u8bf4\u660e\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5355\u72ec\u8c03\u7528input()\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u76f4\u63a5\u8c03\u7528input()\u51fd\u6570\u63a5\u6536\u7528\u6237\u8f93\u5165\u662f\u6700\u7b80\u4fbf\u7684\u65b9\u6cd5\u3002\u6bcf\u6b21\u8c03\u7528input()\u51fd\u6570\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u6682\u505c\u5e76\u7b49\u5f85\u7528\u6237\u8f93\u5165\uff0c\u76f4\u5230\u7528\u6237\u6309\u4e0b\u56de\u8f66\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u7b2c\u4e00\u4e2a\u8f93\u5165<\/p>\n<p>first_input = input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u4e2a\u503c\uff1a&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u8f93\u5165<\/strong><\/h2>\n<p>second_input = input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u4e2a\u503c\uff1a&quot;)<\/p>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u8f93\u5165\u662f\uff1a{first_input}, \u7b2c\u4e8c\u4e2a\u8f93\u5165\u662f\uff1a{second_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u9996\u5148\u63d0\u793a\u7528\u6237\u8f93\u5165\u7b2c\u4e00\u4e2a\u503c\uff0c\u63a5\u6536\u8f93\u5165\u5e76\u5b58\u50a8\u5728\u53d8\u91cf<code>first_input<\/code>\u4e2d\u3002\u7136\u540e\uff0c\u7a0b\u5e8f\u63d0\u793a\u7528\u6237\u8f93\u5165\u7b2c\u4e8c\u4e2a\u503c\uff0c\u5e76\u5c06\u8f93\u5165\u5b58\u50a8\u5728\u53d8\u91cf<code>second_input<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u7a0b\u5e8f\u5c06\u4e24\u4e2a\u8f93\u5165\u6253\u5370\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528split()\u51fd\u6570\u5904\u7406\u591a\u9879\u8f93\u5165<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u7528\u6237\u53ef\u80fd\u5e0c\u671b\u5728\u4e00\u6b21\u8f93\u5165\u4e2d\u63d0\u4f9b\u591a\u4e2a\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u7a7a\u683c\u6216\u9017\u53f7\u5206\u9694\u591a\u4e2a\u503c\uff0c\u7136\u540e\u901a\u8fc7split()\u51fd\u6570\u62c6\u5206\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u7528\u6237\u8f93\u5165<\/p>\n<p>user_input = input(&quot;\u8bf7\u8f93\u5165\u4e24\u4e2a\u503c\uff08\u7528\u7a7a\u683c\u5206\u9694\uff09\uff1a&quot;)<\/p>\n<h2><strong>\u62c6\u5206\u8f93\u5165<\/strong><\/h2>\n<p>first_input, second_input = user_input.split()<\/p>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u8f93\u5165\u662f\uff1a{first_input}, \u7b2c\u4e8c\u4e2a\u8f93\u5165\u662f\uff1a{second_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u4e00\u6b21\u8f93\u5165\u4e2d\u63d0\u4f9b\u4e24\u4e2a\u503c\uff0c\u7528\u7a7a\u683c\u5206\u9694\u3002split()\u51fd\u6570\u5c06\u8f93\u5165\u62c6\u5206\u4e3a\u4e24\u4e2a\u90e8\u5206\uff0c\u5e76\u5206\u522b\u5b58\u50a8\u5728\u53d8\u91cf<code>first_input<\/code>\u548c<code>second_input<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u83b7\u53d6\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u9664\u4e86input()\u51fd\u6570\uff0cPython\u8fd8\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u83b7\u53d6\u8f93\u5165\u3002\u4f7f\u7528sys.argv\u53ef\u4ee5\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5728\u8fd0\u884c\u811a\u672c\u65f6\u63d0\u4f9b\u8f93\u5165\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>sys.argv\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u547d\u4ee4\u884c\u4e2d\u63d0\u4f9b\u7684\u53c2\u6570\u3002\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u811a\u672c\u540d\u79f0\uff0c\u5176\u4f59\u5143\u7d20\u662f\u63d0\u4f9b\u7684\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570<\/strong><\/h2>\n<p>if len(sys.argv) != 3:<\/p>\n<p>    print(&quot;\u8bf7\u63d0\u4f9b\u4e24\u4e2a\u53c2\u6570&quot;)<\/p>\n<p>else:<\/p>\n<p>    first_input = sys.argv[1]<\/p>\n<p>    second_input = sys.argv[2]<\/p>\n<p>    print(f&quot;\u7b2c\u4e00\u4e2a\u8f93\u5165\u662f\uff1a{first_input}, \u7b2c\u4e8c\u4e2a\u8f93\u5165\u662f\uff1a{second_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u901a\u8fc7sys.argv\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570\u3002\u5982\u679c\u53c2\u6570\u6570\u91cf\u4e0d\u7b49\u4e8e3\uff08\u5305\u62ec\u811a\u672c\u540d\u79f0\uff09\uff0c\u7a0b\u5e8f\u5c06\u63d0\u793a\u7528\u6237\u63d0\u4f9b\u4e24\u4e2a\u53c2\u6570\u3002\u5426\u5219\uff0c\u7a0b\u5e8f\u5c06\u53c2\u6570\u5b58\u50a8\u5728\u53d8\u91cf<code>first_input<\/code>\u548c<code>second_input<\/code>\u4e2d\uff0c\u5e76\u6253\u5370\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528argparse\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u547d\u4ee4\u884c\u53c2\u6570\u5904\u7406\uff0cargparse\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u529f\u80fd\u548c\u7075\u6d3b\u6027\u3002argparse\u6a21\u5757\u5141\u8bb8\u5b9a\u4e49\u53c2\u6570\u7c7b\u578b\u3001\u9ed8\u8ba4\u503c\u548c\u5e2e\u52a9\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<h2><strong>\u521b\u5efa\u89e3\u6790\u5668<\/strong><\/h2>\n<p>parser = argparse.ArgumentParser(description=&quot;\u83b7\u53d6\u4e24\u4e2a\u8f93\u5165&quot;)<\/p>\n<h2><strong>\u6dfb\u52a0\u53c2\u6570<\/strong><\/h2>\n<p>parser.add_argument(&quot;first_input&quot;, type=str, help=&quot;\u7b2c\u4e00\u4e2a\u8f93\u5165&quot;)<\/p>\n<p>parser.add_argument(&quot;second_input&quot;, type=str, help=&quot;\u7b2c\u4e8c\u4e2a\u8f93\u5165&quot;)<\/p>\n<h2><strong>\u89e3\u6790\u53c2\u6570<\/strong><\/h2>\n<p>args = parser.parse_args()<\/p>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u8f93\u5165\u662f\uff1a{args.first_input}, \u7b2c\u4e8c\u4e2a\u8f93\u5165\u662f\uff1a{args.second_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u9996\u5148\u521b\u5efa\u4e00\u4e2aargparse.ArgumentParser\u5bf9\u8c61\uff0c\u5e76\u6dfb\u52a0\u4e24\u4e2a\u53c2\u6570\u3002\u89e3\u6790\u53c2\u6570\u540e\uff0c\u7a0b\u5e8f\u5c06\u53c2\u6570\u5b58\u50a8\u5728args\u5bf9\u8c61\u4e2d\uff0c\u5e76\u6253\u5370\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7528\u6237\u8f93\u5165\u53ef\u80fd\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528open()\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>open()\u51fd\u6570\u662fPython\u4e2d\u8bfb\u53d6\u6587\u4ef6\u7684\u57fa\u672c\u65b9\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528\u8be5\u51fd\u6570\u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6<\/p>\n<p>with open(&quot;input.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    lines = file.readlines()<\/p>\n<h2><strong>\u83b7\u53d6\u7b2c\u4e00\u4e2a\u548c\u7b2c\u4e8c\u4e2a\u8f93\u5165<\/strong><\/h2>\n<p>first_input = lines[0].strip()<\/p>\n<p>second_input = lines[1].strip()<\/p>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u8f93\u5165\u662f\uff1a{first_input}, \u7b2c\u4e8c\u4e2a\u8f93\u5165\u662f\uff1a{second_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u6253\u5f00\u540d\u4e3ainput.txt\u7684\u6587\u4ef6\uff0c\u5e76\u8bfb\u53d6\u6240\u6709\u884c\u3002\u7136\u540e\uff0c\u7a0b\u5e8f\u5206\u522b\u83b7\u53d6\u6587\u4ef6\u4e2d\u7684\u7b2c\u4e00\u884c\u548c\u7b2c\u4e8c\u884c\uff0c\u5e76\u5b58\u50a8\u5728\u53d8\u91cf<code>first_input<\/code>\u548c<code>second_input<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528pandas\u8bfb\u53d6\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u7ed3\u6784\u5316\u6570\u636e\u6587\u4ef6\uff0c\u5982CSV\uff0cpandas\u5e93\u63d0\u4f9b\u4e86\u66f4\u65b9\u4fbf\u7684\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_csv(&quot;input.csv&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u7b2c\u4e00\u4e2a\u548c\u7b2c\u4e8c\u4e2a\u8f93\u5165<\/strong><\/h2>\n<p>first_input = df.iloc[0, 0]<\/p>\n<p>second_input = df.iloc[0, 1]<\/p>\n<p>print(f&quot;\u7b2c\u4e00\u4e2a\u8f93\u5165\u662f\uff1a{first_input}, \u7b2c\u4e8c\u4e2a\u8f93\u5165\u662f\uff1a{second_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f7f\u7528pandas.read_csv()\u51fd\u6570\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7iloc\u5c5e\u6027\u83b7\u53d6\u7b2c\u4e00\u884c\u4e2d\u7684\u4e24\u4e2a\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\u83b7\u53d6\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u53cb\u597d\u7684\u7528\u6237\u4f53\u9a8c\uff0c\u53ef\u4ee5\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u83b7\u53d6\u8f93\u5165\u3002Python\u6709\u591a\u4e2aGUI\u5e93\uff0c\u5982Tkinter\u3001PyQt\u548cwxPython\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528Tkinter\u83b7\u53d6\u8f93\u5165<\/h4>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u6613\u4e8e\u4f7f\u7528\u4e14\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;\u83b7\u53d6\u8f93\u5165&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6807\u7b7e\u548c\u8f93\u5165\u6846<\/strong><\/h2>\n<p>tk.Label(root, text=&quot;\u7b2c\u4e00\u4e2a\u8f93\u5165\uff1a&quot;).pack()<\/p>\n<p>first_entry = tk.Entry(root)<\/p>\n<p>first_entry.pack()<\/p>\n<p>tk.Label(root, text=&quot;\u7b2c\u4e8c\u4e2a\u8f93\u5165\uff1a&quot;).pack()<\/p>\n<p>second_entry = tk.Entry(root)<\/p>\n<p>second_entry.pack()<\/p>\n<h2><strong>\u521b\u5efa\u6309\u94ae<\/strong><\/h2>\n<p>def get_inputs():<\/p>\n<p>    first_input = first_entry.get()<\/p>\n<p>    second_input = second_entry.get()<\/p>\n<p>    print(f&quot;\u7b2c\u4e00\u4e2a\u8f93\u5165\u662f\uff1a{first_input}, \u7b2c\u4e8c\u4e2a\u8f93\u5165\u662f\uff1a{second_input}&quot;)<\/p>\n<p>tk.Button(root, text=&quot;\u63d0\u4ea4&quot;, command=get_inputs).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><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f7f\u7528Tkinter\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684GUI\u7a97\u53e3\uff0c\u5305\u542b\u4e24\u4e2a\u8f93\u5165\u6846\u548c\u4e00\u4e2a\u6309\u94ae\u3002\u7528\u6237\u53ef\u4ee5\u5728\u8f93\u5165\u6846\u4e2d\u8f93\u5165\u503c\uff0c\u5e76\u70b9\u51fb\u6309\u94ae\u83b7\u53d6\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528PyQt\u83b7\u53d6\u8f93\u5165<\/h4>\n<\/p>\n<p><p>PyQt\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684GUI\u5e93\uff0c\u9002\u7528\u4e8e\u9700\u8981\u66f4\u590d\u6742\u754c\u9762\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton<\/p>\n<h2><strong>\u521b\u5efa\u5e94\u7528<\/strong><\/h2>\n<p>app = QApplication(sys.argv)<\/p>\n<h2><strong>\u521b\u5efa\u7a97\u53e3<\/strong><\/h2>\n<p>window = QWidget()<\/p>\n<p>window.setWindowTitle(&quot;\u83b7\u53d6\u8f93\u5165&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5e03\u5c40<\/strong><\/h2>\n<p>layout = QVBoxLayout()<\/p>\n<h2><strong>\u521b\u5efa\u6807\u7b7e\u548c\u8f93\u5165\u6846<\/strong><\/h2>\n<p>layout.addWidget(QLabel(&quot;\u7b2c\u4e00\u4e2a\u8f93\u5165\uff1a&quot;))<\/p>\n<p>first_input = QLineEdit()<\/p>\n<p>layout.addWidget(first_input)<\/p>\n<p>layout.addWidget(QLabel(&quot;\u7b2c\u4e8c\u4e2a\u8f93\u5165\uff1a&quot;))<\/p>\n<p>second_input = QLineEdit()<\/p>\n<p>layout.addWidget(second_input)<\/p>\n<h2><strong>\u521b\u5efa\u6309\u94ae<\/strong><\/h2>\n<p>def get_inputs():<\/p>\n<p>    first_value = first_input.text()<\/p>\n<p>    second_value = second_input.text()<\/p>\n<p>    print(f&quot;\u7b2c\u4e00\u4e2a\u8f93\u5165\u662f\uff1a{first_value}, \u7b2c\u4e8c\u4e2a\u8f93\u5165\u662f\uff1a{second_value}&quot;)<\/p>\n<p>button = QPushButton(&quot;\u63d0\u4ea4&quot;)<\/p>\n<p>button.clicked.connect(get_inputs)<\/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<\/strong><\/h2>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f7f\u7528PyQt\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u8f93\u5165\u6846\u548c\u4e00\u4e2a\u6309\u94ae\u7684GUI\u7a97\u53e3\u3002\u7528\u6237\u53ef\u4ee5\u5728\u8f93\u5165\u6846\u4e2d\u8f93\u5165\u503c\uff0c\u5e76\u70b9\u51fb\u6309\u94ae\u83b7\u53d6\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8fdb\u884c\u7b2c\u4e8c\u4e2a\u8f93\u5165\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002input()\u51fd\u6570\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u7528\u6237\u4ea4\u4e92\u3002\u547d\u4ee4\u884c\u53c2\u6570\u548c\u6587\u4ef6\u8bfb\u53d6\u9002\u7528\u4e8e\u6279\u5904\u7406\u548c\u81ea\u52a8\u5316\u4efb\u52a1\uff0c\u800c\u56fe\u5f62\u7528\u6237\u754c\u9762\u9002\u7528\u4e8e\u9700\u8981\u66f4\u53cb\u597d\u7528\u6237\u4f53\u9a8c\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u8fdb\u884c\u9009\u62e9\u3002\u5e0c\u671b\u672c\u6587\u63d0\u4f9b\u7684\u8be6\u7ec6\u4ecb\u7ecd\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u7528\u6237\u8f93\u5165\u7684\u591a\u6b21\u8bfb\u53d6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u7528\u6237\u7684\u8f93\u5165\u3002\u5982\u679c\u4f60\u9700\u8981\u591a\u6b21\u8bfb\u53d6\u8f93\u5165\uff0c\u53ef\u4ee5\u5c06\u5176\u653e\u5728\u5faa\u73af\u7ed3\u6784\u4e2d\u3002\u6bd4\u5982\uff0c\u4f7f\u7528<code>while<\/code>\u5faa\u73af\uff0c\u76f4\u5230\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\uff0c\u6216\u8005\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u6839\u636e\u9700\u8981\u6307\u5b9a\u8f93\u5165\u6b21\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">while True:\n    user_input = input(&quot;\u8bf7\u8f93\u5165\u5185\u5bb9\uff08\u8f93\u5165&#39;\u9000\u51fa&#39;\u7ed3\u675f\uff09\uff1a&quot;)\n    if user_input == &#39;\u9000\u51fa&#39;:\n        break\n    print(f&quot;\u60a8\u8f93\u5165\u7684\u5185\u5bb9\u662f\uff1a{user_input}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u6536\u96c6\u591a\u6b21\u8f93\u5165\uff0c\u76f4\u5230\u7528\u6237\u9009\u62e9\u505c\u6b62\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u9519\u8bef\u6216\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u65e0\u6548\u6570\u636e\u6216\u9519\u8bef\u683c\u5f0f\u3002\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u53ef\u4ee5\u6355\u6349\u5f02\u5e38\u5e76\u8fdb\u884c\u76f8\u5e94\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u5e0c\u671b\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u6570\uff0c\u53ef\u4ee5\u7528\u4ee5\u4e0b\u65b9\u5f0f\u6765\u786e\u4fdd\u8f93\u5165\u6709\u6548\u6027\uff1a  <\/p>\n<pre><code class=\"language-python\">while True:\n    try:\n        user_input = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570\uff1a&quot;))\n        print(f&quot;\u60a8\u8f93\u5165\u7684\u6574\u6570\u662f\uff1a{user_input}&quot;)\n        break\n    except ValueError:\n        print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u786e\u4fdd\u8f93\u5165\u7684\u662f\u6574\u6570\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u7a0b\u5e8f\u56e0\u65e0\u6548\u8f93\u5165\u800c\u5d29\u6e83\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u7528\u6237\u8f93\u5165\u5b58\u50a8\u5230\u5217\u8868\u4e2d\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u591a\u6b21\u8f93\u5165\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u4f7f\u7528<code>append()<\/code>\u65b9\u6cd5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5c06\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u5b58\u50a8\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff1a  <\/p>\n<pre><code class=\"language-python\">user_inputs = []\nwhile True:\n    user_input = input(&quot;\u8bf7\u8f93\u5165\u5185\u5bb9\uff08\u8f93\u5165&#39;\u7ed3\u675f&#39;\u4fdd\u5b58\u5e76\u9000\u51fa\uff09\uff1a&quot;)\n    if user_input == &#39;\u7ed3\u675f&#39;:\n        break\n    user_inputs.append(user_input)\n\nprint(&quot;\u60a8\u8f93\u5165\u7684\u5185\u5bb9\u5217\u8868\uff1a&quot;, user_inputs)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u7528\u6237\u8f93\u5165\u591a\u9879\u5185\u5bb9\uff0c\u5e76\u5728\u6700\u7ec8\u4fdd\u5b58\u5230\u5217\u8868\u4e2d\uff0c\u65b9\u4fbf\u540e\u7eed\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8fdb\u884c\u7b2c\u4e8c\u4e2a\u8f93\u5165\u7684\u5e38\u89c1\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528input()\u51fd\u6570\u3001\u547d\u4ee4\u884c\u53c2\u6570\u3001\u6587\u4ef6\u8bfb\u53d6\u548c\u56fe\u5f62\u7528\u6237\u754c\u9762\u7b49\u3002\u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1135450,"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\/1135438"}],"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=1135438"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135438\/revisions"}],"predecessor-version":[{"id":1135451,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135438\/revisions\/1135451"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1135450"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1135438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1135438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1135438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}