{"id":1140685,"date":"2025-01-08T22:23:50","date_gmt":"2025-01-08T14:23:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1140685.html"},"modified":"2025-01-08T22:23:52","modified_gmt":"2025-01-08T14:23:52","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%81%9a%e4%b8%80%e4%b8%aa%e6%a1%8c%e9%9d%a2%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1140685.html","title":{"rendered":"\u5982\u4f55\u7528python\u505a\u4e00\u4e2a\u684c\u9762\u5e94\u7528\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103618\/9506eca5-5fb1-4c15-8a9d-bbe6a5af6714.webp\" alt=\"\u5982\u4f55\u7528python\u505a\u4e00\u4e2a\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u505a\u4e00\u4e2a\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u9009\u62e9\u5408\u9002\u7684GUI\u5e93\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u3001\u5b9e\u73b0\u6838\u5fc3\u529f\u80fd\u3001\u8c03\u8bd5\u548c\u4f18\u5316\u3001\u6253\u5305\u548c\u53d1\u5e03\u3002<\/strong>\u5176\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684GUI\u5e93\u662f\u6700\u5173\u952e\u7684\u4e00\u6b65\uff0c\u56e0\u4e3a\u5b83\u51b3\u5b9a\u4e86\u5f00\u53d1\u7684\u590d\u6742\u5ea6\u548c\u6700\u7ec8\u5e94\u7528\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>Python\u6709\u591a\u4e2a\u6d41\u884c\u7684GUI\u5e93\u53ef\u4f9b\u9009\u62e9\uff0c\u5982Tkinter\u3001PyQt\u3001Kivy\u7b49\u3002\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u4f18\u70b9\u548c\u7f3a\u70b9\uff0c\u4f8b\u5982\uff0cTkinter\u662fPython\u81ea\u5e26\u7684\u5e93\uff0c\u5bb9\u6613\u4e0a\u624b\uff0c\u4f46\u529f\u80fd\u76f8\u5bf9\u7b80\u5355\uff1bPyQt\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u590d\u6742\u7684\u5e94\u7528\uff0c\u4f46\u5b66\u4e60\u66f2\u7ebf\u8f83\u9661\uff1bKivy\u9002\u7528\u4e8e\u591a\u5e73\u53f0\u5f00\u53d1\uff0c\u5c24\u5176\u662f\u79fb\u52a8\u5e94\u7528\uff0c\u4f46\u5176\u751f\u6001\u76f8\u5bf9\u8f83\u5c0f\u3002\u672c\u6587\u5c06\u4ee5PyQt\u4e3a\u4f8b\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u7528Python\u5236\u4f5c\u4e00\u4e2a\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684GUI\u5e93<\/h3>\n<\/p>\n<p><h4>1. Tkinter<\/h4>\n<\/p>\n<p><p>Tkinter\u662fPython\u5185\u7f6e\u7684GUI\u5e93\u3002\u5b83\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u521d\u5b66\u8005\uff0c\u4f46\u529f\u80fd\u76f8\u5bf9\u8f83\u4e3a\u57fa\u7840\u3002\u4f7f\u7528Tkinter\u53ef\u4ee5\u5feb\u901f\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def on_click():<\/p>\n<p>    label.config(text=&quot;Button clicked!&quot;)<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Tkinter Example&quot;)<\/p>\n<p>label = tk.Label(root, text=&quot;Hello, Tkinter!&quot;)<\/p>\n<p>label.pack()<\/p>\n<p>button = tk.Button(root, text=&quot;Click Me&quot;, command=on_click)<\/p>\n<p>button.pack()<\/p>\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><h4>2. PyQt<\/h4>\n<\/p>\n<p><p>PyQt\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u8de8\u5e73\u53f0GUI\u5e93\uff0c\u652f\u6301\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u3002\u5176API\u4e30\u5bcc\uff0c\u80fd\u591f\u6ee1\u8db3\u5927\u591a\u6570\u684c\u9762\u5e94\u7528\u7684\u9700\u6c42\uff0c\u4f46\u5b66\u4e60\u66f2\u7ebf\u8f83\u4e3a\u9661\u5ced\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QPushButton<\/p>\n<p>class MainWindow(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&quot;PyQt Example&quot;)<\/p>\n<p>        self.label = QLabel(&quot;Hello, PyQt!&quot;, self)<\/p>\n<p>        self.label.move(100, 100)<\/p>\n<p>        self.button = QPushButton(&quot;Click Me&quot;, self)<\/p>\n<p>        self.button.move(100, 150)<\/p>\n<p>        self.button.clicked.connect(self.on_click)<\/p>\n<p>    def on_click(self):<\/p>\n<p>        self.label.setText(&quot;Button clicked!&quot;)<\/p>\n<p>app = QApplication([])<\/p>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<p>app.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. Kivy<\/h4>\n<\/p>\n<p><p>Kivy\u662f\u4e00\u4e2a\u5f00\u6e90\u7684Python\u5e93\uff0c\u4e13\u6ce8\u4e8e\u591a\u70b9\u89e6\u63a7\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\uff0c\u9002\u7528\u4e8e\u5f00\u53d1\u8de8\u5e73\u53f0\u5e94\u7528\uff0c\u5c24\u5176\u662f\u79fb\u52a8\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.label import Label<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        return Label(text=&quot;Hello, Kivy!&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u754c\u9762\uff08UI\uff09\u7684\u8bbe\u8ba1\u662f\u4e00\u4e2a\u81f3\u5173\u91cd\u8981\u7684\u6b65\u9aa4\uff0c\u5b83\u76f4\u63a5\u5f71\u54cd\u7528\u6237\u7684\u4f53\u9a8c\u548c\u5e94\u7528\u7684\u6613\u7528\u6027\u3002\u5728\u8bbe\u8ba1UI\u65f6\uff0c\u9700\u8981\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5e03\u5c40<\/strong>\uff1a\u5408\u7406\u5b89\u6392\u5404\u4e2a\u63a7\u4ef6\u7684\u4f4d\u7f6e\uff0c\u4f7f\u754c\u9762\u7f8e\u89c2\u4e14\u6613\u4e8e\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u4ea4\u4e92\u6027<\/strong>\uff1a\u786e\u4fdd\u7528\u6237\u7684\u64cd\u4f5c\u80fd\u5f97\u5230\u53ca\u65f6\u548c\u6b63\u786e\u7684\u53cd\u9988\u3002<\/li>\n<li><strong>\u4e00\u81f4\u6027<\/strong>\uff1a\u4fdd\u6301\u754c\u9762\u7684\u98ce\u683c\u4e00\u81f4\uff0c\u4f7f\u7528\u6237\u80fd\u591f\u5feb\u901f\u4e0a\u624b\u3002<\/li>\n<\/ul>\n<p><h4>1. \u5e03\u5c40<\/h4>\n<\/p>\n<p><p>\u5e03\u5c40\u662fUI\u8bbe\u8ba1\u7684\u57fa\u7840\u3002\u5728PyQt\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u5e03\u5c40\u7ba1\u7406\u5668\u6765\u63a7\u5236\u63a7\u4ef6\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\u3002\u4f8b\u5982\uff0cQVBoxLayout\u548cQHBoxLayout\u7528\u4e8e\u5782\u76f4\u548c\u6c34\u5e73\u5e03\u5c40\uff0cQGridLayout\u7528\u4e8e\u7f51\u683c\u5e03\u5c40\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QPushButton, QVBoxLayout, QWidget<\/p>\n<p>class MainWindow(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&quot;PyQt Layout Example&quot;)<\/p>\n<p>        layout = QVBoxLayout()<\/p>\n<p>        self.label = QLabel(&quot;Hello, PyQt!&quot;)<\/p>\n<p>        layout.addWidget(self.label)<\/p>\n<p>        self.button = QPushButton(&quot;Click Me&quot;)<\/p>\n<p>        self.button.clicked.connect(self.on_click)<\/p>\n<p>        layout.addWidget(self.button)<\/p>\n<p>        container = QWidget()<\/p>\n<p>        container.setLayout(layout)<\/p>\n<p>        self.setCentralWidget(container)<\/p>\n<p>    def on_click(self):<\/p>\n<p>        self.label.setText(&quot;Button clicked!&quot;)<\/p>\n<p>app = QApplication([])<\/p>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<p>app.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4ea4\u4e92\u6027<\/h4>\n<\/p>\n<p><p>\u4ea4\u4e92\u6027\u662fUI\u8bbe\u8ba1\u7684\u5173\u952e\u3002\u7528\u6237\u7684\u6bcf\u4e00\u4e2a\u64cd\u4f5c\u90fd\u9700\u8981\u5f97\u5230\u53ca\u65f6\u4e14\u6b63\u786e\u7684\u53cd\u9988\u3002\u53ef\u4ee5\u901a\u8fc7\u4e8b\u4ef6\u5904\u7406\u673a\u5236\u5b9e\u73b0\u4ea4\u4e92\u6027\uff0c\u4f8b\u5982\u6309\u94ae\u70b9\u51fb\u3001\u6587\u672c\u8f93\u5165\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QLabel, QLineEdit, QMainWindow, QPushButton, QVBoxLayout, QWidget<\/p>\n<p>class MainWindow(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&quot;PyQt Interaction Example&quot;)<\/p>\n<p>        layout = QVBoxLayout()<\/p>\n<p>        self.label = QLabel(&quot;Enter your name:&quot;)<\/p>\n<p>        layout.addWidget(self.label)<\/p>\n<p>        self.textbox = QLineEdit()<\/p>\n<p>        layout.addWidget(self.textbox)<\/p>\n<p>        self.button = QPushButton(&quot;Submit&quot;)<\/p>\n<p>        self.button.clicked.connect(self.on_click)<\/p>\n<p>        layout.addWidget(self.button)<\/p>\n<p>        container = QWidget()<\/p>\n<p>        container.setLayout(layout)<\/p>\n<p>        self.setCentralWidget(container)<\/p>\n<p>    def on_click(self):<\/p>\n<p>        name = self.textbox.text()<\/p>\n<p>        self.label.setText(f&quot;Hello, {name}!&quot;)<\/p>\n<p>app = QApplication([])<\/p>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<p>app.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5b9e\u73b0\u6838\u5fc3\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u5728\u8bbe\u8ba1\u597d\u7528\u6237\u754c\u9762\u4e4b\u540e\uff0c\u5c31\u9700\u8981\u5b9e\u73b0\u5e94\u7528\u7684\u6838\u5fc3\u529f\u80fd\u3002\u6838\u5fc3\u529f\u80fd\u7684\u5b9e\u73b0\u901a\u5e38\u5305\u62ec\u6570\u636e\u5904\u7406\u3001\u4e1a\u52a1\u903b\u8f91\u548c\u4e0e\u5916\u90e8\u7cfb\u7edf\u7684\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u5904\u7406\u662f\u5e94\u7528\u7a0b\u5e8f\u7684\u57fa\u7840\u3002\u65e0\u8bba\u662f\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\uff0c\u8fd8\u662f\u4ece\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u636e\uff0c\u90fd\u9700\u8981\u8fdb\u884c\u5408\u7406\u7684\u6570\u636e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def create_table():<\/p>\n<p>    conn = sqlite3.connect(&quot;example.db&quot;)<\/p>\n<p>    c = conn.cursor()<\/p>\n<p>    c.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)&#39;&#39;&#39;)<\/p>\n<p>    conn.commit()<\/p>\n<p>    conn.close()<\/p>\n<p>def insert_user(name):<\/p>\n<p>    conn = sqlite3.connect(&quot;example.db&quot;)<\/p>\n<p>    c = conn.cursor()<\/p>\n<p>    c.execute(&quot;INSERT INTO users (name) VALUES (?)&quot;, (name,))<\/p>\n<p>    conn.commit()<\/p>\n<p>    conn.close()<\/p>\n<p>def get_users():<\/p>\n<p>    conn = sqlite3.connect(&quot;example.db&quot;)<\/p>\n<p>    c = conn.cursor()<\/p>\n<p>    c.execute(&quot;SELECT * FROM users&quot;)<\/p>\n<p>    users = c.fetchall()<\/p>\n<p>    conn.close()<\/p>\n<p>    return users<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4e1a\u52a1\u903b\u8f91<\/h4>\n<\/p>\n<p><p>\u4e1a\u52a1\u903b\u8f91\u662f\u5e94\u7528\u7a0b\u5e8f\u7684\u6838\u5fc3\u3002\u5b83\u5b9a\u4e49\u4e86\u5e94\u7528\u7a0b\u5e8f\u5982\u4f55\u5904\u7406\u6570\u636e\u3001\u5982\u4f55\u4e0e\u7528\u6237\u4ea4\u4e92\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QLabel, QLineEdit, QMainWindow, QPushButton, QVBoxLayout, QWidget<\/p>\n<p>import sqlite3<\/p>\n<p>class MainWindow(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&quot;PyQt Core Functionality Example&quot;)<\/p>\n<p>        layout = QVBoxLayout()<\/p>\n<p>        self.label = QLabel(&quot;Enter your name:&quot;)<\/p>\n<p>        layout.addWidget(self.label)<\/p>\n<p>        self.textbox = QLineEdit()<\/p>\n<p>        layout.addWidget(self.textbox)<\/p>\n<p>        self.button = QPushButton(&quot;Submit&quot;)<\/p>\n<p>        self.button.clicked.connect(self.on_click)<\/p>\n<p>        layout.addWidget(self.button)<\/p>\n<p>        self.result_label = QLabel(&quot;&quot;)<\/p>\n<p>        layout.addWidget(self.result_label)<\/p>\n<p>        container = QWidget()<\/p>\n<p>        container.setLayout(layout)<\/p>\n<p>        self.setCentralWidget(container)<\/p>\n<p>    def on_click(self):<\/p>\n<p>        name = self.textbox.text()<\/p>\n<p>        self.insert_user(name)<\/p>\n<p>        users = self.get_users()<\/p>\n<p>        self.result_label.setText(f&quot;Users: {&#39;, &#39;.join([user[1] for user in users])}&quot;)<\/p>\n<p>    def insert_user(self, name):<\/p>\n<p>        conn = sqlite3.connect(&quot;example.db&quot;)<\/p>\n<p>        c = conn.cursor()<\/p>\n<p>        c.execute(&quot;INSERT INTO users (name) VALUES (?)&quot;, (name,))<\/p>\n<p>        conn.commit()<\/p>\n<p>        conn.close()<\/p>\n<p>    def get_users(self):<\/p>\n<p>        conn = sqlite3.connect(&quot;example.db&quot;)<\/p>\n<p>        c = conn.cursor()<\/p>\n<p>        c.execute(&quot;SELECT * FROM users&quot;)<\/p>\n<p>        users = c.fetchall()<\/p>\n<p>        conn.close()<\/p>\n<p>        return users<\/p>\n<p>app = QApplication([])<\/p>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<p>app.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4e0e\u5916\u90e8\u7cfb\u7edf\u4ea4\u4e92<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u4e0e\u5916\u90e8\u7cfb\u7edf\u4ea4\u4e92\uff0c\u4f8b\u5982\u901a\u8fc7API\u83b7\u53d6\u6570\u636e\uff0c\u6216\u4e0e\u5176\u4ed6\u8f6f\u4ef6\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def get_data_from_api():<\/p>\n<p>    response = requests.get(&quot;https:\/\/api.example.com\/data&quot;)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        return response.json()<\/p>\n<p>    else:<\/p>\n<p>        return None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8c03\u8bd5\u548c\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u8c03\u8bd5\u548c\u4f18\u5316\u662f\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u9ad8\u6548\u8fd0\u884c\u7684\u5173\u952e\u6b65\u9aa4\u3002\u53ef\u4ee5\u901a\u8fc7\u65e5\u5fd7\u8bb0\u5f55\u3001\u5355\u5143\u6d4b\u8bd5\u7b49\u624b\u6bb5\u6765\u8c03\u8bd5\u548c\u4f18\u5316\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>1. \u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>\u65e5\u5fd7\u8bb0\u5f55\u662f\u8c03\u8bd5\u7684\u91cd\u8981\u624b\u6bb5\u3002\u901a\u8fc7\u8bb0\u5f55\u5e94\u7528\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\uff0c\u53ef\u4ee5\u5feb\u901f\u5b9a\u4f4d\u548c\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.INFO)<\/p>\n<p>def insert_user(name):<\/p>\n<p>    conn = sqlite3.connect(&quot;example.db&quot;)<\/p>\n<p>    c = conn.cursor()<\/p>\n<p>    try:<\/p>\n<p>        c.execute(&quot;INSERT INTO users (name) VALUES (?)&quot;, (name,))<\/p>\n<p>        conn.commit()<\/p>\n<p>        logging.info(f&quot;Inserted user: {name}&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        logging.error(f&quot;Error inserting user: {e}&quot;)<\/p>\n<p>    finally:<\/p>\n<p>        conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5355\u5143\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u5355\u5143\u6d4b\u8bd5\u662f\u786e\u4fdd\u4ee3\u7801\u8d28\u91cf\u7684\u91cd\u8981\u624b\u6bb5\u3002\u901a\u8fc7\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\uff0c\u53ef\u4ee5\u9a8c\u8bc1\u4ee3\u7801\u7684\u6b63\u786e\u6027\uff0c\u5e76\u5728\u4fee\u6539\u4ee3\u7801\u65f6\u786e\u4fdd\u5176\u4e0d\u4f1a\u5f15\u5165\u65b0\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>class TestDatabase(unittest.TestCase):<\/p>\n<p>    def test_insert_user(self):<\/p>\n<p>        insert_user(&quot;test_user&quot;)<\/p>\n<p>        users = get_users()<\/p>\n<p>        self.assertIn((&quot;test_user&quot;,), users)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6253\u5305\u548c\u53d1\u5e03<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u5b8c\u6210\u5e76\u7ecf\u8fc7\u5145\u5206\u7684\u8c03\u8bd5\u548c\u4f18\u5316\u540e\uff0c\u5c31\u53ef\u4ee5\u5c06\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u5e76\u53d1\u5e03\u3002<\/p>\n<\/p>\n<p><h4>1. \u6253\u5305<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528PyInstaller\u7b49\u5de5\u5177\u5c06Python\u811a\u672c\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u65b9\u4fbf\u5206\u53d1\u548c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile main.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u53d1\u5e03<\/h4>\n<\/p>\n<p><p>\u53d1\u5e03\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u9009\u62e9\u591a\u79cd\u65b9\u5f0f\uff0c\u4f8b\u5982\u901a\u8fc7GitHub\u53d1\u5e03\u3001\u5728\u81ea\u5df1\u7684\u670d\u52a1\u5668\u4e0a\u63d0\u4f9b\u4e0b\u8f7d\u94fe\u63a5\uff0c\u6216\u901a\u8fc7\u5e94\u7528\u5546\u5e97\u53d1\u5e03\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-markdown\"># \u53d1\u5e03\u8bf4\u660e<\/p>\n<p>## \u7248\u672c 1.0.0<\/p>\n<p>- \u521d\u59cb\u7248\u672c<\/p>\n<p>- \u529f\u80fd\u5217\u8868\uff1a<\/p>\n<p>  - \u7528\u6237\u8f93\u5165<\/p>\n<p>  - \u6570\u636e\u5e93\u64cd\u4f5c<\/p>\n<p>  - API\u4ea4\u4e92<\/p>\n<p>\u4e0b\u8f7d\u94fe\u63a5\uff1a[example.com\/download](http:\/\/example.com\/download)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\u8d77\u6765\uff0c\u7528Python\u5236\u4f5c\u4e00\u4e2a\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u7ecf\u8fc7\u9009\u62e9\u5408\u9002\u7684GUI\u5e93\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u3001\u5b9e\u73b0\u6838\u5fc3\u529f\u80fd\u3001\u8c03\u8bd5\u548c\u4f18\u5316\u3001\u6253\u5305\u548c\u53d1\u5e03\u4e94\u4e2a\u4e3b\u8981\u6b65\u9aa4\u3002\u6bcf\u4e00\u6b65\u90fd\u9700\u8981\u4ed4\u7ec6\u89c4\u5212\u548c\u5b9e\u65bd\uff0c\u4ee5\u786e\u4fdd\u6700\u7ec8\u5e94\u7528\u7684\u529f\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u3002\u901a\u8fc7\u4e0d\u65ad\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u53ef\u4ee5\u5236\u4f5c\u51fa\u529f\u80fd\u5f3a\u5927\u4e14\u7528\u6237\u53cb\u597d\u7684\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u9002\u5408\u7684Python\u5e93\u6765\u5f00\u53d1\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5728\u5f00\u53d1\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684Python\u5e93\u81f3\u5173\u91cd\u8981\u3002\u6d41\u884c\u7684\u9009\u62e9\u5305\u62ecTkinter\u3001PyQt\u548cwxPython\u3002Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u9002\u5408\u7b80\u5355\u7684\u5e94\u7528\u5f00\u53d1\uff1bPyQt\u5219\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u66f4\u597d\u7684\u754c\u9762\u8bbe\u8ba1\uff0c\u9002\u5408\u4e2d\u5927\u578b\u9879\u76ee\uff1bwxPython\u5219\u662f\u4e00\u4e2a\u5c01\u88c5\u4e86C++\u5e93wxWidgets\u7684\u5e93\uff0c\u80fd\u591f\u521b\u5efa\u539f\u751f\u98ce\u683c\u7684\u754c\u9762\u3002\u6839\u636e\u9879\u76ee\u9700\u6c42\u548c\u4e2a\u4eba\u7ecf\u9a8c\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u5b9e\u73b0\u76ee\u6807\u3002<\/p>\n<p><strong>Python\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u6d41\u7a0b\u662f\u600e\u6837\u7684\uff1f<\/strong><br \/>\u5f00\u53d1\u4e00\u4e2aPython\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u901a\u5e38\u5305\u62ec\u51e0\u4e2a\u5173\u952e\u6b65\u9aa4\uff1a\u5b9a\u4e49\u9879\u76ee\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684GUI\u6846\u67b6\uff0c\u8bbe\u8ba1\u7528\u6237\u754c\u9762\uff0c\u7f16\u5199\u5e94\u7528\u7a0b\u5e8f\u903b\u8f91\uff0c\u4ee5\u53ca\u6d4b\u8bd5\u548c\u8c03\u8bd5\u3002\u9996\u5148\uff0c\u660e\u786e\u5e94\u7528\u7a0b\u5e8f\u7684\u529f\u80fd\u548c\u7528\u6237\u9700\u6c42\uff1b\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528\u6240\u9009\u7684GUI\u6846\u67b6\u6784\u5efa\u754c\u9762\uff0c\u5e76\u5b9e\u73b0\u4ea4\u4e92\u903b\u8f91\uff1b\u6700\u540e\uff0c\u8fdb\u884c\u5168\u9762\u6d4b\u8bd5\uff0c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u7a33\u5b9a\u53ef\u9760\u3002<\/p>\n<p><strong>\u5982\u4f55\u6253\u5305\u6211\u7684Python\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u4ee5\u4fbf\u4e8e\u5206\u53d1\uff1f<\/strong><br \/>\u4e3a\u4e86\u4fbf\u4e8e\u5206\u53d1\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982PyInstaller\u6216cx_Freeze\u5c06Python\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5c06Python\u4ee3\u7801\u53ca\u5176\u4f9d\u8d56\u9879\u6253\u5305\u5230\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\uff0c\u7528\u6237\u65e0\u9700\u5b89\u88c5Python\u73af\u5883\u5373\u53ef\u8fd0\u884c\u3002\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u65f6\uff0c\u9700\u8981\u6309\u7167\u6587\u6863\u4e2d\u7684\u6307\u793a\u914d\u7f6e\u6253\u5305\u9009\u9879\uff0c\u786e\u4fdd\u6240\u6709\u5fc5\u8981\u7684\u6587\u4ef6\u548c\u8d44\u6e90\u90fd\u88ab\u5305\u542b\u5728\u5185\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u505a\u4e00\u4e2a\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u9009\u62e9\u5408\u9002\u7684GUI\u5e93\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u3001\u5b9e\u73b0\u6838\u5fc3\u529f\u80fd\u3001\u8c03\u8bd5\u548c\u4f18\u5316\u3001\u6253\u5305 [&hellip;]","protected":false},"author":3,"featured_media":1140691,"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\/1140685"}],"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=1140685"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1140685\/revisions"}],"predecessor-version":[{"id":1140692,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1140685\/revisions\/1140692"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1140691"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1140685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1140685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1140685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}