{"id":958854,"date":"2024-12-27T03:35:22","date_gmt":"2024-12-26T19:35:22","guid":{"rendered":""},"modified":"2024-12-27T03:35:23","modified_gmt":"2024-12-26T19:35:23","slug":"python%e5%a6%82%e4%bd%95%e5%bb%ba%e7%ab%8b%e7%99%bb%e5%bd%95%e7%aa%97%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/958854.html","title":{"rendered":"python\u5982\u4f55\u5efa\u7acb\u767b\u5f55\u7a97\u53e3"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101750\/1c645a65-fd63-4f10-98b5-6f63af8890f1.webp\" alt=\"python\u5982\u4f55\u5efa\u7acb\u767b\u5f55\u7a97\u53e3\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5efa\u7acb\u767b\u5f55\u7a97\u53e3\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\uff0c\u5982Tkinter\u3001PyQt\u3001Kivy\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u5e93\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7528\u6237\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\u7684\u754c\u9762\uff0c\u5e76\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u3002\u63a8\u8350\u4f7f\u7528Tkinter\uff0c\u56e0\u4e3a\u5b83\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u7b80\u5355\u6613\u5b66\u3001\u8de8\u5e73\u53f0\u3001\u5185\u7f6e\u5728Python\u4e2d\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528TKINTER\u521b\u5efa\u767b\u5f55\u7a97\u53e3<\/p>\n<\/p>\n<p><p>Tkinter\u662fPython\u5185\u7f6e\u7684GUI\u5e93\uff0c\u6613\u4e8e\u4f7f\u7528\u4e14\u8de8\u5e73\u53f0\u3002\u901a\u8fc7Tkinter\uff0c\u4f60\u53ef\u4ee5\u5feb\u901f\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u767b\u5f55\u7a97\u53e3\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u5bfc\u5165Tkinter<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5927\u591a\u6570Python\u5b89\u88c5\u4e2d\uff0cTkinter\u662f\u9ed8\u8ba4\u5b89\u88c5\u7684\u3002\u56e0\u6b64\uff0c\u4f60\u53ea\u9700\u5bfc\u5165\u76f8\u5173\u6a21\u5757\u5373\u53ef\u4f7f\u7528\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u67d0\u4e9b\u7cbe\u7b80\u7248\u7684Python\u73af\u5883\uff0c\u53ef\u80fd\u9700\u8981\u5355\u72ec\u5b89\u88c5Tkinter\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import messagebox<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528Tkinter\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\u662f\u521b\u5efa\u4efb\u4f55GUI\u5e94\u7528\u7a0b\u5e8f\u7684\u7b2c\u4e00\u6b65\u3002\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u7a97\u53e3\u7684\u6807\u9898\u3001\u5927\u5c0f\u548c\u5176\u4ed6\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">root = tk.Tk()<\/p>\n<p>root.title(&quot;\u767b\u5f55\u7a97\u53e3&quot;)<\/p>\n<p>root.geometry(&quot;300x150&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6dfb\u52a0\u7528\u6237\u754c\u9762\u7ec4\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u521b\u5efa\u767b\u5f55\u7a97\u53e3\u65f6\uff0c\u4f60\u9700\u8981\u6dfb\u52a0\u6807\u7b7e\u3001\u6587\u672c\u8f93\u5165\u6846\u548c\u6309\u94ae\u3002\u6807\u7b7e\u7528\u4e8e\u6307\u793a\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\uff08\u7528\u6237\u540d\u548c\u5bc6\u7801\uff09\uff0c\u6587\u672c\u6846\u7528\u4e8e\u63a5\u53d7\u7528\u6237\u7684\u8f93\u5165\uff0c\u6309\u94ae\u7528\u4e8e\u63d0\u4ea4\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6807\u7b7e<\/p>\n<p>username_label = tk.Label(root, text=&quot;\u7528\u6237\u540d:&quot;)<\/p>\n<p>password_label = tk.Label(root, text=&quot;\u5bc6\u7801:&quot;)<\/p>\n<h2><strong>\u6587\u672c\u8f93\u5165\u6846<\/strong><\/h2>\n<p>username_entry = tk.Entry(root)<\/p>\n<p>password_entry = tk.Entry(root, show=&quot;*&quot;)<\/p>\n<h2><strong>\u5e03\u5c40<\/strong><\/h2>\n<p>username_label.grid(row=0, column=0, padx=10, pady=10)<\/p>\n<p>username_entry.grid(row=0, column=1, padx=10, pady=10)<\/p>\n<p>password_label.grid(row=1, column=0, padx=10, pady=10)<\/p>\n<p>password_entry.grid(row=1, column=1, padx=10, pady=10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u5b9e\u73b0\u767b\u5f55\u529f\u80fd<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u7528\u6237\u70b9\u51fb\u767b\u5f55\u6309\u94ae\u65f6\uff0c\u4f60\u9700\u8981\u9a8c\u8bc1\u8f93\u5165\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u662f\u5426\u6b63\u786e\u3002\u8fd9\u901a\u5e38\u6d89\u53ca\u4e0e\u6570\u636e\u5e93\u6216\u5176\u4ed6\u5f62\u5f0f\u7684\u7528\u6237\u6570\u636e\u5b58\u50a8\u8fdb\u884c\u6bd4\u8f83\u3002\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7b80\u5355\u7684\u786c\u7f16\u7801\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def login():<\/p>\n<p>    username = username_entry.get()<\/p>\n<p>    password = password_entry.get()<\/p>\n<p>    if username == &quot;admin&quot; and password == &quot;password&quot;:  # \u7b80\u5355\u9a8c\u8bc1<\/p>\n<p>        messagebox.showinfo(&quot;\u63d0\u793a&quot;, &quot;\u767b\u5f55\u6210\u529f&quot;)<\/p>\n<p>    else:<\/p>\n<p>        messagebox.showerror(&quot;\u9519\u8bef&quot;, &quot;\u7528\u6237\u540d\u6216\u5bc6\u7801\u4e0d\u6b63\u786e&quot;)<\/p>\n<h2><strong>\u767b\u5f55\u6309\u94ae<\/strong><\/h2>\n<p>login_button = tk.Button(root, text=&quot;\u767b\u5f55&quot;, command=login)<\/p>\n<p>login_button.grid(row=2, column=1, pady=10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"5\">\n<li><strong>\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f<\/strong><\/li>\n<\/ol>\n<p><p>\u6700\u540e\uff0c\u542f\u52a8Tkinter\u7684\u4e3b\u4e8b\u4ef6\u5faa\u73af\u4ee5\u663e\u793a\u7a97\u53e3\u5e76\u7b49\u5f85\u7528\u6237\u4ea4\u4e92\u3002<\/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><strong>\u8fd9\u4e00\u5c0f\u8282\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u767b\u5f55\u7a97\u53e3\u793a\u4f8b\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u6269\u5c55\u548c\u81ea\u5b9a\u4e49\uff0c\u5982\u5c06\u7528\u6237\u540d\u548c\u5bc6\u7801\u9a8c\u8bc1\u4e0e\u6570\u636e\u5e93\u7ed3\u5408\uff0c\u6216\u4f7f\u7528\u66f4\u591a\u7684Tkinter\u7ec4\u4ef6\u6765\u589e\u5f3a\u7528\u6237\u754c\u9762\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528PYQT\u521b\u5efa\u767b\u5f55\u7a97\u53e3<\/p>\n<\/p>\n<p><p>PyQt\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684Python GUI\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ec4\u4ef6\u548c\u7075\u6d3b\u7684\u5e03\u5c40\u7ba1\u7406\u3002\u867d\u7136\u5b83\u6bd4Tkinter\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\uff0c\u4f46\u5b83\u7684\u529f\u80fd\u66f4\u5f3a\u5927\uff0c\u9002\u5408\u7528\u4e8e\u66f4\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u5bfc\u5165PyQt<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5PyQt5\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\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>\u7136\u540e\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QMessageBox<\/p>\n<p>import sys<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u548c\u7a97\u53e3<\/strong><\/li>\n<\/ol>\n<p><p>\u521b\u5efa\u4e00\u4e2aPyQt\u5e94\u7528\u7a0b\u5e8f\u548c\u4e00\u4e2a\u4e3b\u7a97\u53e3\u3002\u4f60\u9700\u8981\u8bbe\u7f6e\u7a97\u53e3\u7684\u6807\u9898\u548c\u5e03\u5c40\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">app = QApplication(sys.argv)<\/p>\n<p>window = QWidget()<\/p>\n<p>window.setWindowTitle(&#39;\u767b\u5f55\u7a97\u53e3&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6dfb\u52a0\u7528\u6237\u754c\u9762\u7ec4\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528PyQt\u7ec4\u4ef6\u521b\u5efa\u6807\u7b7e\u3001\u6587\u672c\u8f93\u5165\u6846\u548c\u6309\u94ae\u3002\u4f7f\u7528QVBoxLayout\u6765\u7ba1\u7406\u7ec4\u4ef6\u7684\u5e03\u5c40\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">layout = QVBoxLayout()<\/p>\n<p>username_label = QLabel(&#39;\u7528\u6237\u540d:&#39;)<\/p>\n<p>username_entry = QLineEdit()<\/p>\n<p>password_label = QLabel(&#39;\u5bc6\u7801:&#39;)<\/p>\n<p>password_entry = QLineEdit()<\/p>\n<p>password_entry.setEchoMode(QLineEdit.Password)<\/p>\n<p>login_button = QPushButton(&#39;\u767b\u5f55&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u5b9e\u73b0\u767b\u5f55\u529f\u80fd<\/strong><\/li>\n<\/ol>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u5904\u7406\u767b\u5f55\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u3002\u4f60\u53ef\u4ee5\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def login():<\/p>\n<p>    username = username_entry.text()<\/p>\n<p>    password = password_entry.text()<\/p>\n<p>    if username == &#39;admin&#39; and password == &#39;password&#39;:  # \u7b80\u5355\u9a8c\u8bc1<\/p>\n<p>        QMessageBox.information(window, &#39;\u63d0\u793a&#39;, &#39;\u767b\u5f55\u6210\u529f&#39;)<\/p>\n<p>    else:<\/p>\n<p>        QMessageBox.critical(window, &#39;\u9519\u8bef&#39;, &#39;\u7528\u6237\u540d\u6216\u5bc6\u7801\u4e0d\u6b63\u786e&#39;)<\/p>\n<p>login_button.clicked.connect(login)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"5\">\n<li><strong>\u8bbe\u7f6e\u7ec4\u4ef6\u5e03\u5c40\u5e76\u663e\u793a\u7a97\u53e3<\/strong><\/li>\n<\/ol>\n<p><p>\u5c06\u6240\u6709\u7ec4\u4ef6\u6dfb\u52a0\u5230\u5e03\u5c40\u4e2d\uff0c\u5e76\u8bbe\u7f6e\u7a97\u53e3\u7684\u5e03\u5c40\u3002\u6700\u540e\uff0c\u663e\u793a\u7a97\u53e3\u5e76\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">layout.addWidget(username_label)<\/p>\n<p>layout.addWidget(username_entry)<\/p>\n<p>layout.addWidget(password_label)<\/p>\n<p>layout.addWidget(password_entry)<\/p>\n<p>layout.addWidget(login_button)<\/p>\n<p>window.setLayout(layout)<\/p>\n<p>window.show()<\/p>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528PyQt\u521b\u5efa\u7684\u767b\u5f55\u7a97\u53e3\u76f8\u8f83\u4e8eTkinter\u66f4\u52a0\u4e30\u5bcc\uff0c\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u5e03\u5c40\u7ba1\u7406\u548c\u66f4\u5f3a\u5927\u7684\u7ec4\u4ef6\u652f\u6301\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u9700\u8981\u9ad8\u7ea7\u7528\u6237\u754c\u9762\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528KIVY\u521b\u5efa\u767b\u5f55\u7a97\u53e3<\/p>\n<\/p>\n<p><p>Kivy\u662f\u4e00\u4e2a\u7528\u4e8e\u5f00\u53d1\u591a\u70b9\u89e6\u63a7\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u6e90Python\u5e93\uff0c\u652f\u6301\u8de8\u5e73\u53f0\u5f00\u53d1\u3002\u867d\u7136\u5b83\u7684\u5b66\u4e60\u66f2\u7ebf\u7a0d\u9661\uff0c\u4f46\u5b83\u9002\u7528\u4e8e\u9700\u8981\u8fd0\u884c\u5728\u591a\u4e2a\u8bbe\u5907\u4e0a\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u5bfc\u5165Kivy<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Kivy\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install kivy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.boxlayout import BoxLayout<\/p>\n<p>from kivy.uix.label import Label<\/p>\n<p>from kivy.uix.textinput import TextInput<\/p>\n<p>from kivy.uix.button import Button<\/p>\n<p>from kivy.uix.popup import Popup<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u521b\u5efa\u4e3b\u7a97\u53e3\u548c\u5e03\u5c40<\/strong><\/li>\n<\/ol>\n<p><p>Kivy\u4f7f\u7528\u5e03\u5c40\u7c7b\u6765\u7ba1\u7406\u7ec4\u4ef6\u7684\u4f4d\u7f6e\u3002BoxLayout\u662f\u6700\u5e38\u7528\u7684\u4e00\u79cd\u5e03\u5c40\uff0c\u5b83\u53ef\u4ee5\u8ba9\u7ec4\u4ef6\u6c34\u5e73\u6216\u5782\u76f4\u6392\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class LoginScreen(BoxLayout):<\/p>\n<p>    def __init__(self, kwargs):<\/p>\n<p>        super(LoginScreen, self).__init__(kwargs)<\/p>\n<p>        self.orientation = &#39;vertical&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6dfb\u52a0\u7528\u6237\u754c\u9762\u7ec4\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u5728LoginScreen\u7c7b\u4e2d\u6dfb\u52a0\u6807\u7b7e\u3001\u6587\u672c\u8f93\u5165\u6846\u548c\u6309\u94ae\u3002\u6587\u672c\u8f93\u5165\u6846\u7528\u4e8e\u63a5\u6536\u7528\u6237\u8f93\u5165\uff0c\u6309\u94ae\u7528\u4e8e\u63d0\u4ea4\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">        self.add_widget(Label(text=&#39;\u7528\u6237\u540d:&#39;))<\/p>\n<p>        self.username_input = TextInput(multiline=False)<\/p>\n<p>        self.add_widget(self.username_input)<\/p>\n<p>        self.add_widget(Label(text=&#39;\u5bc6\u7801:&#39;))<\/p>\n<p>        self.password_input = TextInput(password=True, multiline=False)<\/p>\n<p>        self.add_widget(self.password_input)<\/p>\n<p>        login_button = Button(text=&#39;\u767b\u5f55&#39;)<\/p>\n<p>        login_button.bind(on_press=self.login)<\/p>\n<p>        self.add_widget(login_button)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u5b9e\u73b0\u767b\u5f55\u529f\u80fd<\/strong><\/li>\n<\/ol>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u5904\u7406\u767b\u5f55\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528Popup\u6765\u663e\u793a\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">    def login(self, instance):<\/p>\n<p>        username = self.username_input.text<\/p>\n<p>        password = self.password_input.text<\/p>\n<p>        if username == &#39;admin&#39; and password == &#39;password&#39;:  # \u7b80\u5355\u9a8c\u8bc1<\/p>\n<p>            popup = Popup(title=&#39;\u63d0\u793a&#39;, content=Label(text=&#39;\u767b\u5f55\u6210\u529f&#39;), size_hint=(None, None), size=(200, 200))<\/p>\n<p>        else:<\/p>\n<p>            popup = Popup(title=&#39;\u9519\u8bef&#39;, content=Label(text=&#39;\u7528\u6237\u540d\u6216\u5bc6\u7801\u4e0d\u6b63\u786e&#39;), size_hint=(None, None), size=(200, 200))<\/p>\n<p>        popup.open()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"5\">\n<li><strong>\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f<\/strong><\/li>\n<\/ol>\n<p><p>\u521b\u5efa\u4e00\u4e2aApp\u7c7b\uff0c\u5b9e\u4f8b\u5316LoginScreen\uff0c\u5e76\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class LoginApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        return LoginScreen()<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    LoginApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u901a\u8fc7Kivy\u521b\u5efa\u7684\u767b\u5f55\u7a97\u53e3\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u8de8\u5e73\u53f0\u5f00\u53d1\uff0c\u9002\u5408\u9700\u8981\u5728\u79fb\u52a8\u8bbe\u5907\u4e0a\u8fd0\u884c\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u5b83\u7684UI\u5b9a\u4e49\u65b9\u5f0f\u4e0e\u5176\u4ed6\u5e93\u7565\u6709\u4e0d\u540c\uff0c\u4f46\u63d0\u4f9b\u4e86\u9ad8\u5ea6\u7684\u5b9a\u5236\u5316\u80fd\u529b\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u56db\u3001\u6269\u5c55\u4e0e\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u767b\u5f55\u7a97\u53e3\u53ea\u662f\u7528\u6237\u8ba4\u8bc1\u7cfb\u7edf\u7684\u4e00\u90e8\u5206\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u53ef\u4ee5\u6269\u5c55\u548c\u4f18\u5316\u7684\u65b9\u5411\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4e0e\u6570\u636e\u5e93\u96c6\u6210<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7528\u6237\u540d\u548c\u5bc6\u7801\u662f\u786c\u7f16\u7801\u7684\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8fd9\u4e9b\u4fe1\u606f\u901a\u5e38\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528SQLite\u3001MySQL\u6216\u5176\u4ed6\u6570\u636e\u5e93\uff0c\u5c06\u7528\u6237\u4fe1\u606f\u5b58\u50a8\u5728\u8868\u4e2d\uff0c\u5e76\u5728\u767b\u5f55\u65f6\u8fdb\u884c\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5bc6\u7801\u52a0\u5bc6<\/strong><\/li>\n<\/ol>\n<p><p>\u4e3a\u63d0\u9ad8\u5b89\u5168\u6027\uff0c\u901a\u5e38\u4f1a\u5bf9\u7528\u6237\u5bc6\u7801\u8fdb\u884c\u52a0\u5bc6\u5b58\u50a8\u3002\u53ef\u4ee5\u4f7f\u7528bcrypt\u3001hashlib\u7b49\u5e93\u8fdb\u884c\u5bc6\u7801\u7684\u54c8\u5e0c\u52a0\u5bc6\uff0c\u5e76\u5728\u9a8c\u8bc1\u65f6\u8fdb\u884c\u5339\u914d\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong><\/li>\n<\/ol>\n<p><p>\u589e\u52a0\u5bf9\u7528\u6237\u8f93\u5165\u9519\u8bef\u7684\u5904\u7406\uff0c\u6bd4\u5982\u8f93\u5165\u4e3a\u7a7a\u3001\u683c\u5f0f\u4e0d\u6b63\u786e\u7b49\u3002\u53ef\u4ee5\u901a\u8fc7\u5728UI\u4e0a\u63d0\u4f9b\u5373\u65f6\u53cd\u9988\u6765\u6539\u5584\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<ol start=\"4\">\n<li><strong>UI\u7f8e\u5316<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528\u6837\u5f0f\u8868\uff08\u5982Tkinter\u7684ttk\u3001PyQt\u7684QSS\uff09\u6216Kivy\u7684KV\u8bed\u8a00\u6765\u7f8e\u5316\u754c\u9762\uff0c\u4f7f\u5e94\u7528\u7a0b\u5e8f\u66f4\u5177\u5438\u5f15\u529b\u3002<\/p>\n<\/p>\n<ol start=\"5\">\n<li><strong>\u591a\u8bed\u8a00\u652f\u6301<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u6dfb\u52a0\u591a\u8bed\u8a00\u652f\u6301\uff0c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u53ef\u7528\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002\u53ef\u4ee5\u5728\u754c\u9762\u4e2d\u63d0\u4f9b\u8bed\u8a00\u9009\u62e9\uff0c\u5e76\u6839\u636e\u7528\u6237\u9009\u62e9\u52a8\u6001\u52a0\u8f7d\u8bed\u8a00\u8d44\u6e90\u3002<\/p>\n<\/p>\n<ol start=\"6\">\n<li><strong>\u7f51\u7edc\u8bf7\u6c42<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5206\u5e03\u5f0f\u5e94\u7528\u4e2d\uff0c\u767b\u5f55\u4fe1\u606f\u53ef\u80fd\u9700\u8981\u901a\u8fc7\u7f51\u7edc\u8bf7\u6c42\u8fdb\u884c\u9a8c\u8bc1\u3002\u53ef\u4ee5\u4f7f\u7528requests\u5e93\u6216\u5176\u4ed6HTTP\u5ba2\u6237\u7aef\u5e93\u4e0e\u670d\u52a1\u5668\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><p><strong>\u521b\u5efa\u4e00\u4e2a\u767b\u5f55\u7a97\u53e3\u53ea\u662f\u7528\u6237\u9a8c\u8bc1\u7684\u5f00\u59cb\uff0c\u5b9e\u9645\u5e94\u7528\u4e2d\u9700\u8981\u7ed3\u5408\u591a\u79cd\u6280\u672f\u6765\u5b9e\u73b0\u5b89\u5168\u3001\u7528\u6237\u53cb\u597d\u7684\u8ba4\u8bc1\u7cfb\u7edf\u3002\u901a\u8fc7\u4e0d\u65ad\u7684\u4f18\u5316\u548c\u6269\u5c55\uff0c\u53ef\u4ee5\u4f7f\u4f60\u7684\u5e94\u7528\u66f4\u5177\u7ade\u4e89\u529b\u548c\u5b9e\u7528\u6027\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u767b\u5f55\u7a97\u53e3\uff1f<\/strong><br \/>\u8981\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u767b\u5f55\u7a97\u53e3\uff0c\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\uff0c\u8fd9\u662fPython\u7684\u6807\u51c6GUI\u5e93\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2aTk\u7a97\u53e3\uff0c\u6dfb\u52a0\u6807\u7b7e\u3001\u8f93\u5165\u6846\u548c\u6309\u94ae\u6765\u5b9e\u73b0\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import tkinter as tk\nfrom tkinter import messagebox\n\ndef login():\n    username = entry_username.get()\n    password = entry_password.get()\n    if username == &quot;admin&quot; and password == &quot;123456&quot;:\n        messagebox.showinfo(&quot;\u767b\u5f55\u6210\u529f&quot;, &quot;\u6b22\u8fce\u6765\u5230\u7cfb\u7edf\uff01&quot;)\n    else:\n        messagebox.showerror(&quot;\u767b\u5f55\u5931\u8d25&quot;, &quot;\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef\uff01&quot;)\n\nroot = tk.Tk()\nroot.title(&quot;\u767b\u5f55\u7a97\u53e3&quot;)\n\nlabel_username = tk.Label(root, text=&quot;\u7528\u6237\u540d:&quot;)\nlabel_username.pack()\n\nentry_username = tk.Entry(root)\nentry_username.pack()\n\nlabel_password = tk.Label(root, text=&quot;\u5bc6\u7801:&quot;)\nlabel_password.pack()\n\nentry_password = tk.Entry(root, show=&quot;*&quot;)\nentry_password.pack()\n\nbutton_login = tk.Button(root, text=&quot;\u767b\u5f55&quot;, command=login)\nbutton_login.pack()\n\nroot.mainloop()\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u767b\u5f55\u7a97\u53e3\u7684\u7528\u6237\u8f93\u5165\uff1f<\/strong><br \/>\u7528\u6237\u8f93\u5165\u7684\u5904\u7406\u53ef\u4ee5\u901a\u8fc7Tkinter\u4e2d\u7684Entry\u63a7\u4ef6\u6765\u5b9e\u73b0\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>get()<\/code>\u65b9\u6cd5\u83b7\u53d6\u8f93\u5165\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002\u4e3a\u4e86\u786e\u4fdd\u8f93\u5165\u7684\u5b89\u5168\u6027\uff0c\u53ef\u4ee5\u5728\u5bc6\u7801\u8f93\u5165\u6846\u4e2d\u4f7f\u7528<code>show<\/code>\u53c2\u6570\u6765\u9690\u85cf\u8f93\u5165\u7684\u5185\u5bb9\u3002\u6b64\u5916\uff0c\u901a\u8fc7\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u5728\u7528\u6237\u70b9\u51fb\u767b\u5f55\u6309\u94ae\u65f6\u9a8c\u8bc1\u8f93\u5165\u7684\u5185\u5bb9\u5e76\u7ed9\u51fa\u53cd\u9988\u3002<\/p>\n<p><strong>\u5982\u4f55\u4e3aPython\u767b\u5f55\u7a97\u53e3\u6dfb\u52a0\u6837\u5f0f\u548c\u5e03\u5c40\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u5347\u7528\u6237\u4f53\u9a8c\uff0c\u53ef\u4ee5\u901a\u8fc7Tkinter\u7684<code>grid()<\/code>\u6216<code>pack()<\/code>\u65b9\u6cd5\u6765\u4f18\u5316\u5e03\u5c40\uff0c\u5e76\u4f7f\u7528<code>ttk<\/code>\u6a21\u5757\u6765\u589e\u52a0\u6837\u5f0f\u3002\u60a8\u53ef\u4ee5\u8c03\u6574\u63a7\u4ef6\u7684\u989c\u8272\u3001\u5b57\u4f53\u4ee5\u53ca\u7a97\u53e3\u7684\u5927\u5c0f\u7b49\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>frame<\/code>\u6765\u7ec4\u7ec7\u63a7\u4ef6\uff0c\u6216\u901a\u8fc7\u8bbe\u7f6e<code>bg<\/code>\u5c5e\u6027\u66f4\u6539\u80cc\u666f\u989c\u8272\u3002\u5177\u4f53\u7684\u6837\u5f0f\u548c\u5e03\u5c40\u8bbe\u7f6e\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8fdb\u884c\u8c03\u6574\uff0c\u4ee5\u521b\u5efa\u66f4\u52a0\u7f8e\u89c2\u548c\u7528\u6237\u53cb\u597d\u7684\u754c\u9762\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5efa\u7acb\u767b\u5f55\u7a97\u53e3\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\uff0c\u5982Tkinter\u3001PyQt\u3001Kivy\u7b49\u3002\u901a\u8fc7 [&hellip;]","protected":false},"author":3,"featured_media":958856,"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\/958854"}],"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=958854"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/958854\/revisions"}],"predecessor-version":[{"id":958857,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/958854\/revisions\/958857"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/958856"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=958854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=958854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=958854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}