{"id":1166801,"date":"2025-01-15T15:36:22","date_gmt":"2025-01-15T07:36:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1166801.html"},"modified":"2025-01-15T15:36:25","modified_gmt":"2025-01-15T07:36:25","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e7%bc%96%e8%be%91%e6%a1%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1166801.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u7f16\u8f91\u6846"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25210829\/dedcafed-d452-4758-b95b-9021e200444d.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u7f16\u8f91\u6846\" \/><\/p>\n<p><p> \u8981\u5728Python\u4e2d\u5b9e\u73b0\u7f16\u8f91\u6846\uff0c\u53ef\u4ee5\u4f7f\u7528\u51e0\u4e2a\u4e0d\u540c\u7684\u5e93\uff0c\u5305\u62ecTkinter\u3001PyQt\u548cwxPython\u3002<strong>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0cPyQt\u63d0\u4f9b\u4e86\u975e\u5e38\u5f3a\u5927\u7684\u529f\u80fd\uff0cwxPython\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684GUI\u5e93<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e09\u4e2a\u5e93\u6765\u5b9e\u73b0\u7f16\u8f91\u6846\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528Tkinter\u5b9e\u73b0\u7f16\u8f91\u6846<\/p>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u521b\u5efa\u7a97\u53e3\u3001\u6309\u94ae\u3001\u6807\u7b7e\u7b49GUI\u7ec4\u4ef6\u7684\u529f\u80fd\u3002\u4f7f\u7528Tkinter\u5b9e\u73b0\u7f16\u8f91\u6846\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u51e0\u4e2a\u6b65\u9aa4\u5373\u53ef\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5Tkinter<\/h3>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u901a\u5e38\u65e0\u9700\u5355\u72ec\u5b89\u88c5\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fPython 3.x\uff0cTkinter\u5df2\u7ecf\u5305\u542b\u5728\u5176\u4e2d\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fPython 2.x\uff0c\u53ef\u80fd\u9700\u8981\u5355\u72ec\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h3>\u521b\u5efa\u57fa\u672c\u7684Tkinter\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Tkinter\u7a97\u53e3\uff1a<\/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;Tkinter \u7f16\u8f91\u6846\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;400x300&quot;)<\/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>\u6dfb\u52a0\u7f16\u8f91\u6846<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u4e86\u57fa\u672c\u7a97\u53e3\u4e4b\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u7f16\u8f91\u6846\u6765\u6269\u5c55\u529f\u80fd\u3002Tkinter\u63d0\u4f9b\u4e86<code>Entry<\/code>\u5c0f\u90e8\u4ef6\u6765\u521b\u5efa\u5355\u884c\u6587\u672c\u7f16\u8f91\u6846\uff0c<code>Text<\/code>\u5c0f\u90e8\u4ef6\u6765\u521b\u5efa\u591a\u884c\u6587\u672c\u7f16\u8f91\u6846\u3002<\/p>\n<\/p>\n<p><h4>\u5355\u884c\u6587\u672c\u7f16\u8f91\u6846<\/h4>\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;Tkinter \u7f16\u8f91\u6846\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;400x300&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5355\u884c\u6587\u672c\u7f16\u8f91\u6846<\/strong><\/h2>\n<p>entry = tk.Entry(root, width=30)<\/p>\n<p>entry.pack(pady=20)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u591a\u884c\u6587\u672c\u7f16\u8f91\u6846<\/h4>\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;Tkinter \u7f16\u8f91\u6846\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;400x300&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u591a\u884c\u6587\u672c\u7f16\u8f91\u6846<\/strong><\/h2>\n<p>text = tk.Text(root, width=40, height=10)<\/p>\n<p>text.pack(pady=20)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6dfb\u52a0\u6309\u94ae\u548c\u4e8b\u4ef6\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u6309\u94ae\u548c\u4e8b\u4ef6\u5904\u7406\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u6309\u94ae\uff0c\u70b9\u51fb\u6309\u94ae\u540e\u663e\u793a\u7f16\u8f91\u6846\u4e2d\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import messagebox<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Tkinter \u7f16\u8f91\u6846\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;400x300&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u591a\u884c\u6587\u672c\u7f16\u8f91\u6846<\/strong><\/h2>\n<p>text = tk.Text(root, width=40, height=10)<\/p>\n<p>text.pack(pady=20)<\/p>\n<h2><strong>\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u5904\u7406\u51fd\u6570<\/strong><\/h2>\n<p>def show_text():<\/p>\n<p>    content = text.get(&quot;1.0&quot;, tk.END)<\/p>\n<p>    messagebox.showinfo(&quot;\u6587\u672c\u5185\u5bb9&quot;, content)<\/p>\n<h2><strong>\u521b\u5efa\u6309\u94ae<\/strong><\/h2>\n<p>button = tk.Button(root, text=&quot;\u663e\u793a\u6587\u672c&quot;, command=show_text)<\/p>\n<p>button.pack(pady=10)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528PyQt\u5b9e\u73b0\u7f16\u8f91\u6846<\/p>\n<\/p>\n<p><p>PyQt\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684Python GUI\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a7\u4ef6\u548c\u5e03\u5c40\u7ba1\u7406\u529f\u80fd\u3002\u4f7f\u7528PyQt\u53ef\u4ee5\u521b\u5efa\u529f\u80fd\u4e30\u5bcc\u7684\u7f16\u8f91\u6846\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5PyQt<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528pip\u5b89\u88c5PyQt\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyQt5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u521b\u5efa\u57fa\u672c\u7684PyQt\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684PyQt\u7a97\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QWidget<\/p>\n<h2><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>app = QApplication(sys.argv)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>window = QWidget()<\/p>\n<p>window.setWindowTitle(&quot;PyQt \u7f16\u8f91\u6846\u793a\u4f8b&quot;)<\/p>\n<p>window.setGeometry(100, 100, 400, 300)<\/p>\n<h2><strong>\u663e\u793a\u7a97\u53e3<\/strong><\/h2>\n<p>window.show()<\/p>\n<h2><strong>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6dfb\u52a0\u7f16\u8f91\u6846<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u4e86\u57fa\u672c\u7a97\u53e3\u4e4b\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u7f16\u8f91\u6846\u6765\u6269\u5c55\u529f\u80fd\u3002PyQt\u63d0\u4f9b\u4e86<code>QLineEdit<\/code>\u5c0f\u90e8\u4ef6\u6765\u521b\u5efa\u5355\u884c\u6587\u672c\u7f16\u8f91\u6846\uff0c<code>QTextEdit<\/code>\u5c0f\u90e8\u4ef6\u6765\u521b\u5efa\u591a\u884c\u6587\u672c\u7f16\u8f91\u6846\u3002<\/p>\n<\/p>\n<p><h4>\u5355\u884c\u6587\u672c\u7f16\u8f91\u6846<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLineEdit<\/p>\n<h2><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>app = QApplication(sys.argv)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>window = QWidget()<\/p>\n<p>window.setWindowTitle(&quot;PyQt \u7f16\u8f91\u6846\u793a\u4f8b&quot;)<\/p>\n<p>window.setGeometry(100, 100, 400, 300)<\/p>\n<h2><strong>\u521b\u5efa\u5e03\u5c40<\/strong><\/h2>\n<p>layout = QVBoxLayout()<\/p>\n<h2><strong>\u521b\u5efa\u5355\u884c\u6587\u672c\u7f16\u8f91\u6846<\/strong><\/h2>\n<p>line_edit = QLineEdit()<\/p>\n<p>layout.addWidget(line_edit)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5e03\u5c40<\/strong><\/h2>\n<p>window.setLayout(layout)<\/p>\n<h2><strong>\u663e\u793a\u7a97\u53e3<\/strong><\/h2>\n<p>window.show()<\/p>\n<h2><strong>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u591a\u884c\u6587\u672c\u7f16\u8f91\u6846<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTextEdit<\/p>\n<h2><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>app = QApplication(sys.argv)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>window = QWidget()<\/p>\n<p>window.setWindowTitle(&quot;PyQt \u7f16\u8f91\u6846\u793a\u4f8b&quot;)<\/p>\n<p>window.setGeometry(100, 100, 400, 300)<\/p>\n<h2><strong>\u521b\u5efa\u5e03\u5c40<\/strong><\/h2>\n<p>layout = QVBoxLayout()<\/p>\n<h2><strong>\u521b\u5efa\u591a\u884c\u6587\u672c\u7f16\u8f91\u6846<\/strong><\/h2>\n<p>text_edit = QTextEdit()<\/p>\n<p>layout.addWidget(text_edit)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5e03\u5c40<\/strong><\/h2>\n<p>window.setLayout(layout)<\/p>\n<h2><strong>\u663e\u793a\u7a97\u53e3<\/strong><\/h2>\n<p>window.show()<\/p>\n<h2><strong>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6dfb\u52a0\u6309\u94ae\u548c\u4e8b\u4ef6\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u6309\u94ae\u548c\u4e8b\u4ef6\u5904\u7406\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u6309\u94ae\uff0c\u70b9\u51fb\u6309\u94ae\u540e\u663e\u793a\u7f16\u8f91\u6846\u4e2d\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTextEdit, QPushButton, QMessageBox<\/p>\n<h2><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>app = QApplication(sys.argv)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>window = QWidget()<\/p>\n<p>window.setWindowTitle(&quot;PyQt \u7f16\u8f91\u6846\u793a\u4f8b&quot;)<\/p>\n<p>window.setGeometry(100, 100, 400, 300)<\/p>\n<h2><strong>\u521b\u5efa\u5e03\u5c40<\/strong><\/h2>\n<p>layout = QVBoxLayout()<\/p>\n<h2><strong>\u521b\u5efa\u591a\u884c\u6587\u672c\u7f16\u8f91\u6846<\/strong><\/h2>\n<p>text_edit = QTextEdit()<\/p>\n<p>layout.addWidget(text_edit)<\/p>\n<h2><strong>\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u5904\u7406\u51fd\u6570<\/strong><\/h2>\n<p>def show_text():<\/p>\n<p>    content = text_edit.toPlainText()<\/p>\n<p>    QMessageBox.information(window, &quot;\u6587\u672c\u5185\u5bb9&quot;, content)<\/p>\n<h2><strong>\u521b\u5efa\u6309\u94ae<\/strong><\/h2>\n<p>button = QPushButton(&quot;\u663e\u793a\u6587\u672c&quot;)<\/p>\n<p>button.clicked.connect(show_text)<\/p>\n<p>layout.addWidget(button)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5e03\u5c40<\/strong><\/h2>\n<p>window.setLayout(layout)<\/p>\n<h2><strong>\u663e\u793a\u7a97\u53e3<\/strong><\/h2>\n<p>window.show()<\/p>\n<h2><strong>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528wxPython\u5b9e\u73b0\u7f16\u8f91\u6846<\/p>\n<\/p>\n<p><p>wxPython\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684GUI\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a7\u4ef6\u548c\u5e03\u5c40\u7ba1\u7406\u529f\u80fd\u3002\u4f7f\u7528wxPython\u53ef\u4ee5\u521b\u5efa\u529f\u80fd\u4e30\u5bcc\u7684\u7f16\u8f91\u6846\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5wxPython<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528pip\u5b89\u88c5wxPython\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install wxPython<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u521b\u5efa\u57fa\u672c\u7684wxPython\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684wxPython\u7a97\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import wx<\/p>\n<h2><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>app = wx.App(False)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>frame = wx.Frame(None, wx.ID_ANY, &quot;wxPython \u7f16\u8f91\u6846\u793a\u4f8b&quot;, size=(400, 300))<\/p>\n<h2><strong>\u663e\u793a\u7a97\u53e3<\/strong><\/h2>\n<p>frame.Show(True)<\/p>\n<h2><strong>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>app.MainLoop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6dfb\u52a0\u7f16\u8f91\u6846<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u4e86\u57fa\u672c\u7a97\u53e3\u4e4b\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u7f16\u8f91\u6846\u6765\u6269\u5c55\u529f\u80fd\u3002wxPython\u63d0\u4f9b\u4e86<code>wx.TextCtrl<\/code>\u5c0f\u90e8\u4ef6\u6765\u521b\u5efa\u5355\u884c\u548c\u591a\u884c\u6587\u672c\u7f16\u8f91\u6846\u3002<\/p>\n<\/p>\n<p><h4>\u5355\u884c\u6587\u672c\u7f16\u8f91\u6846<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import wx<\/p>\n<h2><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>app = wx.App(False)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>frame = wx.Frame(None, wx.ID_ANY, &quot;wxPython \u7f16\u8f91\u6846\u793a\u4f8b&quot;, size=(400, 300))<\/p>\n<h2><strong>\u521b\u5efa\u5355\u884c\u6587\u672c\u7f16\u8f91\u6846<\/strong><\/h2>\n<p>text_ctrl = wx.TextCtrl(frame, wx.ID_ANY, size=(300, -1))<\/p>\n<h2><strong>\u521b\u5efa\u5e03\u5c40<\/strong><\/h2>\n<p>sizer = wx.BoxSizer(wx.VERTICAL)<\/p>\n<p>sizer.Add(text_ctrl, 0, wx.ALL | wx.CENTER, 20)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5e03\u5c40<\/strong><\/h2>\n<p>frame.SetSizer(sizer)<\/p>\n<h2><strong>\u663e\u793a\u7a97\u53e3<\/strong><\/h2>\n<p>frame.Show(True)<\/p>\n<h2><strong>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>app.MainLoop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u591a\u884c\u6587\u672c\u7f16\u8f91\u6846<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import wx<\/p>\n<h2><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>app = wx.App(False)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>frame = wx.Frame(None, wx.ID_ANY, &quot;wxPython \u7f16\u8f91\u6846\u793a\u4f8b&quot;, size=(400, 300))<\/p>\n<h2><strong>\u521b\u5efa\u591a\u884c\u6587\u672c\u7f16\u8f91\u6846<\/strong><\/h2>\n<p>text_ctrl = wx.TextCtrl(frame, wx.ID_ANY, size=(300, 200), style=wx.TE_MULTILINE)<\/p>\n<h2><strong>\u521b\u5efa\u5e03\u5c40<\/strong><\/h2>\n<p>sizer = wx.BoxSizer(wx.VERTICAL)<\/p>\n<p>sizer.Add(text_ctrl, 0, wx.ALL | wx.CENTER, 20)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5e03\u5c40<\/strong><\/h2>\n<p>frame.SetSizer(sizer)<\/p>\n<h2><strong>\u663e\u793a\u7a97\u53e3<\/strong><\/h2>\n<p>frame.Show(True)<\/p>\n<h2><strong>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>app.MainLoop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6dfb\u52a0\u6309\u94ae\u548c\u4e8b\u4ef6\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u6309\u94ae\u548c\u4e8b\u4ef6\u5904\u7406\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u6309\u94ae\uff0c\u70b9\u51fb\u6309\u94ae\u540e\u663e\u793a\u7f16\u8f91\u6846\u4e2d\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import wx<\/p>\n<h2><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>app = wx.App(False)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>frame = wx.Frame(None, wx.ID_ANY, &quot;wxPython \u7f16\u8f91\u6846\u793a\u4f8b&quot;, size=(400, 300))<\/p>\n<h2><strong>\u521b\u5efa\u591a\u884c\u6587\u672c\u7f16\u8f91\u6846<\/strong><\/h2>\n<p>text_ctrl = wx.TextCtrl(frame, wx.ID_ANY, size=(300, 200), style=wx.TE_MULTILINE)<\/p>\n<h2><strong>\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u5904\u7406\u51fd\u6570<\/strong><\/h2>\n<p>def show_text(event):<\/p>\n<p>    content = text_ctrl.GetValue()<\/p>\n<p>    wx.MessageBox(content, &quot;\u6587\u672c\u5185\u5bb9&quot;, wx.OK | wx.ICON_INFORMATION)<\/p>\n<h2><strong>\u521b\u5efa\u6309\u94ae<\/strong><\/h2>\n<p>button = wx.Button(frame, wx.ID_ANY, &quot;\u663e\u793a\u6587\u672c&quot;)<\/p>\n<p>button.Bind(wx.EVT_BUTTON, show_text)<\/p>\n<h2><strong>\u521b\u5efa\u5e03\u5c40<\/strong><\/h2>\n<p>sizer = wx.BoxSizer(wx.VERTICAL)<\/p>\n<p>sizer.Add(text_ctrl, 0, wx.ALL | wx.CENTER, 20)<\/p>\n<p>sizer.Add(button, 0, wx.ALL | wx.CENTER, 10)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5e03\u5c40<\/strong><\/h2>\n<p>frame.SetSizer(sizer)<\/p>\n<h2><strong>\u663e\u793a\u7a97\u53e3<\/strong><\/h2>\n<p>frame.Show(True)<\/p>\n<h2><strong>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>app.MainLoop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u793a\u4f8b\u53ef\u4ee5\u770b\u5230\uff0c\u5728Python\u4e2d\u4f7f\u7528Tkinter\u3001PyQt\u548cwxPython\u90fd\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u7f16\u8f91\u6846\u3002\u8fd9\u4e9b\u5e93\u5404\u6709\u4f18\u52a3\uff0c\u9009\u62e9\u54ea\u4e00\u4e2a\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u4e2a\u4eba\u504f\u597d\u3002<strong>Tkinter\u662fPython\u7684\u6807\u51c6\u5e93\uff0c\u9002\u5408\u7b80\u5355\u7684GUI\u5e94\u7528\uff1bPyQt\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u590d\u6742\u7684\u5e94\u7528\uff1bwxPython\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u5e93\uff0c\u4e5f\u5177\u6709\u5f88\u5f3a\u7684\u529f\u80fd<\/strong>\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u5b9e\u73b0\u7f16\u8f91\u6846\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\u7f16\u8f91\u6846\uff1f<\/strong><br \/>\u8981\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7f16\u8f91\u6846\uff0c\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\u3002Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u63d0\u4f9b\u4e86\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u7684\u5de5\u5177\u3002\u53ef\u4ee5\u4f7f\u7528<code>Entry<\/code>\u5c0f\u90e8\u4ef6\u521b\u5efa\u5355\u884c\u7f16\u8f91\u6846\uff0c\u6216\u4f7f\u7528<code>Text<\/code>\u5c0f\u90e8\u4ef6\u521b\u5efa\u591a\u884c\u7f16\u8f91\u6846\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u521b\u5efa\u5355\u884c\u7f16\u8f91\u6846\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import tkinter as tk\n\nroot = tk.Tk()\nentry = tk.Entry(root)\nentry.pack()\nroot.mainloop()\n<\/code><\/pre>\n<p>\u901a\u8fc7\u4fee\u6539<code>pack()<\/code>\u65b9\u6cd5\u7684\u53c2\u6570\uff0c\u53ef\u4ee5\u8c03\u6574\u7f16\u8f91\u6846\u7684\u5e03\u5c40\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u83b7\u53d6\u7f16\u8f91\u6846\u4e2d\u7684\u8f93\u5165\u5185\u5bb9\uff1f<\/strong><br \/>\u83b7\u53d6\u7f16\u8f91\u6846\u4e2d\u7684\u5185\u5bb9\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u76f8\u5e94\u5c0f\u90e8\u4ef6\u7684<code>get()<\/code>\u65b9\u6cd5\u3002\u5bf9\u4e8e<code>Entry<\/code>\u5c0f\u90e8\u4ef6\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<pre><code class=\"language-python\">input_value = entry.get()\nprint(input_value)\n<\/code><\/pre>\n<p>\u5bf9\u4e8e<code>Text<\/code>\u5c0f\u90e8\u4ef6\uff0c\u4f7f\u7528<code>get(&quot;1.0&quot;, &quot;end-1c&quot;)<\/code>\u6765\u83b7\u53d6\u6240\u6709\u5185\u5bb9\u3002\u8fd9\u6837\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u4e3aPython\u7f16\u8f91\u6846\u6dfb\u52a0\u4e8b\u4ef6\u5904\u7406\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Tkinter\u7684\u7ed1\u5b9a\u529f\u80fd\u4e3a\u7f16\u8f91\u6846\u6dfb\u52a0\u4e8b\u4ef6\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5e0c\u671b\u5728\u7528\u6237\u6309\u4e0b\u56de\u8f66\u952e\u65f6\u6267\u884c\u67d0\u4e2a\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">def on_enter(event):\n    print(&quot;\u4f60\u8f93\u5165\u7684\u5185\u5bb9\u662f:&quot;, entry.get())\n\nentry.bind(&quot;&lt;Return&gt;&quot;, on_enter)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5f53\u7528\u6237\u5728\u7f16\u8f91\u6846\u4e2d\u6309\u4e0b\u56de\u8f66\u952e\u65f6\uff0c<code>on_enter<\/code>\u51fd\u6570\u5c06\u88ab\u8c03\u7528\uff0c\u4ece\u800c\u5b9e\u73b0\u4ea4\u4e92\u5f0f\u529f\u80fd\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u5e76\u4f7f\u5e94\u7528\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u5b9e\u73b0\u7f16\u8f91\u6846\uff0c\u53ef\u4ee5\u4f7f\u7528\u51e0\u4e2a\u4e0d\u540c\u7684\u5e93\uff0c\u5305\u62ecTkinter\u3001PyQt\u548cwxPython\u3002Tkint [&hellip;]","protected":false},"author":3,"featured_media":1166804,"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\/1166801"}],"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=1166801"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1166801\/revisions"}],"predecessor-version":[{"id":1166808,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1166801\/revisions\/1166808"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1166804"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1166801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1166801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1166801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}