{"id":1102603,"date":"2025-01-08T16:01:43","date_gmt":"2025-01-08T08:01:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1102603.html"},"modified":"2025-01-08T16:01:45","modified_gmt":"2025-01-08T08:01:45","slug":"python%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e8%be%93%e5%85%a5%e6%a1%86-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1102603.html","title":{"rendered":"python\u5982\u4f55\u6dfb\u52a0\u4e00\u4e2a\u8f93\u5165\u6846"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064821\/e54b266e-9258-47f6-bf93-a1d1227f35ee.webp\" alt=\"python\u5982\u4f55\u6dfb\u52a0\u4e00\u4e2a\u8f93\u5165\u6846\" \/><\/p>\n<p><h3>PYTHON\u5982\u4f55\u6dfb\u52a0\u4e00\u4e2a\u8f93\u5165\u6846<\/h3>\n<\/p>\n<p><p><strong>Python\u4e2d\u6dfb\u52a0\u8f93\u5165\u6846\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528Tkinter\u3001PyQt\u3001\u6216\u8005\u7b80\u5355\u7684\u547d\u4ee4\u884c\u8f93\u5165\uff08input()\u51fd\u6570\uff09<\/strong>\u3002\u5176\u4e2d\uff0c<strong>Tkinter\u662fPython\u5185\u7f6e\u7684GUI\u5e93\uff0c\u4f7f\u7528\u7b80\u5355\u4e14\u529f\u80fd\u5f3a\u5927\uff0c\u975e\u5e38\u9002\u5408\u521d\u5b66\u8005<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Tkinter\u5e93\u5728Python\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u8f93\u5165\u6846\uff0c\u5e76\u5c55\u793a\u4e00\u4e9b\u5177\u4f53\u7684\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Tkinter\u6dfb\u52a0\u8f93\u5165\u6846<\/h3>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u63d0\u4f9b\u4e86\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u7684\u7b80\u5355\u65b9\u6cd5\u3002\u901a\u8fc7Tkinter\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u7a97\u53e3\u3001\u6807\u7b7e\u3001\u6309\u94ae\u548c\u8f93\u5165\u6846\u7b49\u7ec4\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u5bfc\u5165Tkinter<\/h4>\n<\/p>\n<p><p>Tkinter\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u901a\u5e38\u4e0d\u9700\u8981\u5b89\u88c5\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u6807\u51c6\u7684Python\u53d1\u884c\u7248\uff0cTkinter\u5df2\u7ecf\u5305\u542b\u5728\u5185\u3002\u53ea\u9700\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165Tkinter\u6a21\u5757\u5373\u53ef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u4e3b\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Tkinter\u65f6\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\u3002\u53ef\u4ee5\u901a\u8fc7<code>Tk()<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u9876\u7ea7\u7a97\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">root = tk.Tk()<\/p>\n<p>root.title(&quot;\u8f93\u5165\u6846\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6dfb\u52a0\u8f93\u5165\u6846\uff08Entry\u63a7\u4ef6\uff09<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>Entry<\/code>\u63a7\u4ef6\u53ef\u4ee5\u5728\u7a97\u53e3\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u8f93\u5165\u6846\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">entry = tk.Entry(root)<\/p>\n<p>entry.pack(pady=10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u6dfb\u52a0\u6807\u7b7e\u548c\u6309\u94ae<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b8c\u6574\u5730\u5c55\u793a\u8f93\u5165\u6846\u7684\u529f\u80fd\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u6807\u7b7e\u548c\u4e00\u4e2a\u6309\u94ae\u3002\u5f53\u7528\u6237\u5728\u8f93\u5165\u6846\u4e2d\u8f93\u5165\u5185\u5bb9\u5e76\u70b9\u51fb\u6309\u94ae\u65f6\uff0c\u6807\u7b7e\u4f1a\u663e\u793a\u8f93\u5165\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def show_input():<\/p>\n<p>    input_text = entry.get()<\/p>\n<p>    label.config(text=&quot;\u8f93\u5165\u7684\u5185\u5bb9: &quot; + input_text)<\/p>\n<p>label = tk.Label(root, text=&quot;&quot;)<\/p>\n<p>label.pack(pady=10)<\/p>\n<p>button = tk.Button(root, text=&quot;\u663e\u793a\u8f93\u5165&quot;, command=show_input)<\/p>\n<p>button.pack(pady=10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u8fd0\u884c\u4e3b\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u8fd0\u884c\u4e3b\u5faa\u73af\u4ee5\u663e\u793a\u7a97\u53e3\u5e76\u7b49\u5f85\u7528\u6237\u4ea4\u4e92\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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>\u5b8c\u6574\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def show_input():<\/p>\n<p>    input_text = entry.get()<\/p>\n<p>    label.config(text=&quot;\u8f93\u5165\u7684\u5185\u5bb9: &quot; + input_text)<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;\u8f93\u5165\u6846\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<p>entry = tk.Entry(root)<\/p>\n<p>entry.pack(pady=10)<\/p>\n<p>label = tk.Label(root, text=&quot;&quot;)<\/p>\n<p>label.pack(pady=10)<\/p>\n<p>button = tk.Button(root, text=&quot;\u663e\u793a\u8f93\u5165&quot;, command=show_input)<\/p>\n<p>button.pack(pady=10)<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528PyQt\u6dfb\u52a0\u8f93\u5165\u6846<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Tkinter\uff0cPyQt\u4e5f\u662f\u4e00\u4e2a\u6d41\u884c\u7684Python GUI\u5e93\u3002PyQt\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u63a7\u4ef6\u548c\u66f4\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u4f46\u76f8\u5bf9\u6765\u8bf4\u5b66\u4e60\u66f2\u7ebf\u4e5f\u66f4\u9661\u5ced\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u5bfc\u5165PyQt<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5PyQt\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyQt5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165PyQt\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLineEdit, QPushButton, QLabel<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u4e3b\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u5728PyQt\u4e2d\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">app = QApplication([])<\/p>\n<p>window = QWidget()<\/p>\n<p>window.setWindowTitle(&quot;\u8f93\u5165\u6846\u793a\u4f8b&quot;)<\/p>\n<p>window.setGeometry(100, 100, 300, 200)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6dfb\u52a0\u8f93\u5165\u6846\uff08QLineEdit\u63a7\u4ef6\uff09<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>QLineEdit<\/code>\u63a7\u4ef6\u53ef\u4ee5\u5728\u7a97\u53e3\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u8f93\u5165\u6846\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">entry = QLineEdit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u6dfb\u52a0\u6807\u7b7e\u548c\u6309\u94ae<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u6807\u7b7e\u548c\u4e00\u4e2a\u6309\u94ae\u3002\u5f53\u7528\u6237\u5728\u8f93\u5165\u6846\u4e2d\u8f93\u5165\u5185\u5bb9\u5e76\u70b9\u51fb\u6309\u94ae\u65f6\uff0c\u6807\u7b7e\u4f1a\u663e\u793a\u8f93\u5165\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">label = QLabel()<\/p>\n<p>def show_input():<\/p>\n<p>    input_text = entry.text()<\/p>\n<p>    label.setText(&quot;\u8f93\u5165\u7684\u5185\u5bb9: &quot; + input_text)<\/p>\n<p>button = QPushButton(&quot;\u663e\u793a\u8f93\u5165&quot;)<\/p>\n<p>button.clicked.connect(show_input)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u5e03\u5c40\u7ba1\u7406<\/h4>\n<\/p>\n<p><p>PyQt\u63d0\u4f9b\u4e86\u591a\u79cd\u5e03\u5c40\u7ba1\u7406\u5668\u6765\u5b89\u6392\u63a7\u4ef6\u7684\u4f4d\u7f6e\u3002\u8fd9\u91cc\u4f7f\u7528<code>QVBoxLayout<\/code>\u5c06\u63a7\u4ef6\u5782\u76f4\u6392\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">layout = QVBoxLayout()<\/p>\n<p>layout.addWidget(entry)<\/p>\n<p>layout.addWidget(button)<\/p>\n<p>layout.addWidget(label)<\/p>\n<p>window.setLayout(layout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6. \u663e\u793a\u7a97\u53e3\u5e76\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u663e\u793a\u7a97\u53e3\u5e76\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">window.show()<\/p>\n<p>app.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b8c\u6574\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLineEdit, QPushButton, QLabel<\/p>\n<p>app = QApplication([])<\/p>\n<p>window = QWidget()<\/p>\n<p>window.setWindowTitle(&quot;\u8f93\u5165\u6846\u793a\u4f8b&quot;)<\/p>\n<p>window.setGeometry(100, 100, 300, 200)<\/p>\n<p>entry = QLineEdit()<\/p>\n<p>label = QLabel()<\/p>\n<p>def show_input():<\/p>\n<p>    input_text = entry.text()<\/p>\n<p>    label.setText(&quot;\u8f93\u5165\u7684\u5185\u5bb9: &quot; + input_text)<\/p>\n<p>button = QPushButton(&quot;\u663e\u793a\u8f93\u5165&quot;)<\/p>\n<p>button.clicked.connect(show_input)<\/p>\n<p>layout = QVBoxLayout()<\/p>\n<p>layout.addWidget(entry)<\/p>\n<p>layout.addWidget(button)<\/p>\n<p>layout.addWidget(label)<\/p>\n<p>window.setLayout(layout)<\/p>\n<p>window.show()<\/p>\n<p>app.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u8f93\u5165\uff08input()\u51fd\u6570\uff09<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0cPython\u8fd8\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u8f93\u5165\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3002\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u8f93\u5165\u6846\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">input_text = input(&quot;\u8bf7\u8f93\u5165\u5185\u5bb9: &quot;)<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u5185\u5bb9: &quot; + input_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5728Python\u4e2d\u6dfb\u52a0\u8f93\u5165\u6846\u7684\u4e09\u79cd\u5e38\u89c1\u65b9\u6cd5\uff1a\u4f7f\u7528Tkinter\u3001\u4f7f\u7528PyQt\u4ee5\u53ca\u4f7f\u7528\u547d\u4ee4\u884c\u8f93\u5165\u3002<strong>Tkinter\u548cPyQt\u90fd\u662f\u5f3a\u5927\u7684GUI\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a7\u4ef6\u548c\u5e03\u5c40\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u521b\u5efa\u529f\u80fd\u5f3a\u5927\u3001\u754c\u9762\u7f8e\u89c2\u7684\u5e94\u7528\u7a0b\u5e8f<\/strong>\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u4e2a\u4eba\u7684\u504f\u597d\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u8ba9\u4f60\u5728Python\u7f16\u7a0b\u4e2d\u66f4\u52a0\u6e38\u5203\u6709\u4f59\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u8f93\u5165\u6846\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\u6765\u521b\u5efa\u8f93\u5165\u6846\u3002Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u80fd\u591f\u8f7b\u677e\u5730\u6784\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u3002\u9996\u5148\uff0c\u786e\u4fdd\u60a8\u7684Python\u73af\u5883\u4e2d\u5df2\u5b89\u88c5Tkinter\u3002\u63a5\u7740\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7a97\u53e3\uff0c\u5305\u542b\u4e00\u4e2a\u8f93\u5165\u6846\u548c\u4e00\u4e2a\u6309\u94ae\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff1a<\/p>\n<pre><code class=\"language-python\">import tkinter as tk\n\ndef get_input():\n    user_input = entry.get()\n    print(f&quot;\u7528\u6237\u8f93\u5165: {user_input}&quot;)\n\nwindow = tk.Tk()\nwindow.title(&quot;\u8f93\u5165\u6846\u793a\u4f8b&quot;)\n\nentry = tk.Entry(window)\nentry.pack()\n\nsubmit_button = tk.Button(window, text=&quot;\u63d0\u4ea4&quot;, command=get_input)\nsubmit_button.pack()\n\nwindow.mainloop()\n<\/code><\/pre>\n<p><strong>\u53ef\u4ee5\u5728\u8f93\u5165\u6846\u4e2d\u8bbe\u7f6e\u5b57\u7b26\u9650\u5236\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ed1\u5b9a\u4e8b\u4ef6\u6765\u9650\u5236\u8f93\u5165\u6846\u4e2d\u5b57\u7b26\u7684\u6570\u91cf\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Tkinter\u7684<code>trace<\/code>\u65b9\u6cd5\u6765\u76d1\u63a7\u8f93\u5165\u6846\u7684\u5185\u5bb9\u957f\u5ea6\uff0c\u5e76\u5728\u8d85\u8fc7\u9650\u5236\u65f6\u963b\u6b62\u8fdb\u4e00\u6b65\u8f93\u5165\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u9650\u5236\u8f93\u5165\u6846\u6700\u591a\u53ea\u80fd\u8f93\u516510\u4e2a\u5b57\u7b26\uff1a<\/p>\n<pre><code class=\"language-python\">def limit_input(*args):\n    if len(entry.get()) &gt; 10:\n        entry.set(entry.get()[:10])\n\nentry_var = tk.StringVar()\nentry_var.trace(&quot;w&quot;, limit_input)\nentry = tk.Entry(window, textvariable=entry_var)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728\u8f93\u5165\u6846\u4e2d\u6dfb\u52a0\u63d0\u793a\u6587\u672c\uff08\u5360\u4f4d\u7b26\uff09\uff1f<\/strong><br \/>\u5728Tkinter\u4e2d\uff0c\u8f93\u5165\u6846\u672c\u8eab\u4e0d\u652f\u6301\u5360\u4f4d\u7b26\u529f\u80fd\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u989d\u5916\u7684\u4ee3\u7801\u5b9e\u73b0\u3002\u53ef\u4ee5\u5728\u8f93\u5165\u6846\u4e2d\u663e\u793a\u63d0\u793a\u6587\u672c\uff0c\u5f53\u7528\u6237\u70b9\u51fb\u8f93\u5165\u6846\u65f6\uff0c\u8be5\u6587\u672c\u5c06\u6d88\u5931\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">def on_entry_click(event):\n    if entry.get() == &#39;\u8bf7\u8f93\u5165\u5185\u5bb9&#39;:\n        entry.delete(0, tk.END)  # \u5220\u9664\u5360\u4f4d\u7b26\n        entry.config(fg=&#39;black&#39;)  # \u6539\u53d8\u6587\u672c\u989c\u8272\n\ndef on_focusout(event):\n    if entry.get() == &#39;&#39;:\n        entry.insert(0, &#39;\u8bf7\u8f93\u5165\u5185\u5bb9&#39;)  # \u91cd\u65b0\u663e\u793a\u5360\u4f4d\u7b26\n        entry.config(fg=&#39;grey&#39;)  # \u6539\u53d8\u6587\u672c\u989c\u8272\n\nentry.insert(0, &#39;\u8bf7\u8f93\u5165\u5185\u5bb9&#39;)  # \u9ed8\u8ba4\u5360\u4f4d\u7b26\nentry.bind(&#39;&lt;FocusIn&gt;&#39;, on_entry_click)\nentry.bind(&#39;&lt;FocusOut&gt;&#39;, on_focusout)\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u6bb5\u63d0\u4f9b\u4e86\u5982\u4f55\u521b\u5efa\u8f93\u5165\u6846\u7684\u793a\u4f8b\u53ca\u5176\u529f\u80fd\u6269\u5c55\uff0c\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u5b9e\u73b0\u7528\u6237\u8f93\u5165\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"PYTHON\u5982\u4f55\u6dfb\u52a0\u4e00\u4e2a\u8f93\u5165\u6846 Python\u4e2d\u6dfb\u52a0\u8f93\u5165\u6846\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528Tkinter\u3001PyQt [&hellip;]","protected":false},"author":3,"featured_media":1102610,"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\/1102603"}],"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=1102603"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1102603\/revisions"}],"predecessor-version":[{"id":1102613,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1102603\/revisions\/1102613"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1102610"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1102603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1102603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1102603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}