{"id":954233,"date":"2024-12-27T01:54:16","date_gmt":"2024-12-26T17:54:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/954233.html"},"modified":"2024-12-27T01:54:18","modified_gmt":"2024-12-26T17:54:18","slug":"python%e6%8c%89%e9%92%ae%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%bc%b9%e7%aa%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/954233.html","title":{"rendered":"python\u6309\u94ae\u5982\u4f55\u5b9e\u73b0\u5f39\u7a97"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094526\/2b77bf10-7b9f-4b19-94d2-8e95c2290625.webp\" alt=\"python\u6309\u94ae\u5982\u4f55\u5b9e\u73b0\u5f39\u7a97\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5b9e\u73b0\u6309\u94ae\u89e6\u53d1\u5f39\u7a97\u7684\u529f\u80fd\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\u6765\u5b9e\u73b0\u3002<strong>\u5e38\u7528\u7684\u5e93\u5305\u62ecTkinter\u3001PyQt\u3001Kivy\u7b49\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u9700\u6c42\u548c\u4e2a\u4eba\u559c\u597d<\/strong>\u3002\u5176\u4e2d\uff0cTkinter\u662fPython\u6807\u51c6\u5e93\u4e2d\u81ea\u5e26\u7684GUI\u5e93\uff0c\u9002\u5408\u7b80\u5355\u7684\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\uff1bPyQt\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u590d\u6742\u7684\u5e94\u7528\u5f00\u53d1\uff1bKivy\u5219\u9002\u5408\u5f00\u53d1\u8de8\u5e73\u53f0\u5e94\u7528\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Tkinter\u5b9e\u73b0\u6309\u94ae\u89e6\u53d1\u5f39\u7a97\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Tkinter\u5b9e\u73b0\u6309\u94ae\u5f39\u7a97<\/h3>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u6613\u4e8e\u4f7f\u7528\u4e14\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002\u8981\u5b9e\u73b0\u6309\u94ae\u89e6\u53d1\u5f39\u7a97\uff0c\u901a\u5e38\u7684\u6b65\u9aa4\u662f\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\uff0c\u6dfb\u52a0\u4e00\u4e2a\u6309\u94ae\uff0c\u7136\u540e\u5b9a\u4e49\u4e00\u4e2a\u7528\u4e8e\u663e\u793a\u5f39\u7a97\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>1. \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\uff0c\u8fd9\u662f\u6240\u6709\u754c\u9762\u5143\u7d20\u7684\u5bb9\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_window():<\/p>\n<p>    # \u521b\u5efa\u4e3b\u7a97\u53e3<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    root.title(&quot;Main Window&quot;)<\/p>\n<p>    root.geometry(&quot;300x200&quot;)<\/p>\n<p>    return root<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6dfb\u52a0\u6309\u94ae<\/h4>\n<\/p>\n<p><p>\u5728\u4e3b\u7a97\u53e3\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u6309\u94ae\uff0c\u5e76\u7ed1\u5b9a\u70b9\u51fb\u4e8b\u4ef6\u5230\u4e00\u4e2a\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_button(root):<\/p>\n<p>    # \u521b\u5efa\u6309\u94ae\u5e76\u7ed1\u5b9a\u4e8b\u4ef6<\/p>\n<p>    button = tk.Button(root, text=&quot;Click Me&quot;, command=show_popup)<\/p>\n<p>    button.pack(pady=20)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5b9a\u4e49\u5f39\u7a97\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5728\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\u8c03\u7528\uff0c\u7528\u4e8e\u663e\u793a\u5f39\u7a97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def show_popup():<\/p>\n<p>    # \u521b\u5efa\u5f39\u7a97<\/p>\n<p>    popup = tk.Toplevel()<\/p>\n<p>    popup.title(&quot;Popup&quot;)<\/p>\n<p>    popup.geometry(&quot;200x100&quot;)<\/p>\n<p>    label = tk.Label(popup, text=&quot;This is a popup window&quot;)<\/p>\n<p>    label.pack(pady=10)<\/p>\n<p>    close_button = tk.Button(popup, text=&quot;Close&quot;, command=popup.destroy)<\/p>\n<p>    close_button.pack(pady=5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u8fd0\u884c\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u5c06\u4ee5\u4e0a\u90e8\u5206\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if __name__ == &quot;__main__&quot;:<\/p>\n<p>    root = main_window()<\/p>\n<p>    create_button(root)<\/p>\n<p>    root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528PyQt\u5b9e\u73b0\u6309\u94ae\u5f39\u7a97<\/h3>\n<\/p>\n<p><p>PyQt\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684GUI\u5e93\uff0c\u9002\u5408\u5f00\u53d1\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a7\u4ef6\u548c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5PyQt<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528PyQt\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install PyQt5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u5e94\u7528\u548c\u4e3b\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u5728PyQt\u4e2d\uff0c\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u548c\u4e3b\u7a97\u53e3\u662f\u5b9e\u73b0\u754c\u9762\u7684\u7b2c\u4e00\u6b65\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QMessageBox<\/p>\n<p>import sys<\/p>\n<p>def main():<\/p>\n<p>    app = QApplication(sys.argv)<\/p>\n<p>    window = QMainWindow()<\/p>\n<p>    window.setWindowTitle(&quot;Main Window&quot;)<\/p>\n<p>    window.setGeometry(100, 100, 300, 200)<\/p>\n<p>    return app, window<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6dfb\u52a0\u6309\u94ae\u548c\u4e8b\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u4e3b\u7a97\u53e3\u4e2d\u6dfb\u52a0\u6309\u94ae\uff0c\u5e76\u7ed1\u5b9a\u70b9\u51fb\u4e8b\u4ef6\u5230\u4e00\u4e2a\u5f39\u7a97\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_button(window):<\/p>\n<p>    button = QPushButton(&quot;Click Me&quot;, window)<\/p>\n<p>    button.setGeometry(100, 80, 100, 40)<\/p>\n<p>    button.clicked.connect(show_popup)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u5b9a\u4e49\u5f39\u7a97\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528QMessageBox\u6765\u5b9e\u73b0\u5f39\u7a97\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def show_popup():<\/p>\n<p>    msg = QMessageBox()<\/p>\n<p>    msg.setWindowTitle(&quot;Popup&quot;)<\/p>\n<p>    msg.setText(&quot;This is a popup window&quot;)<\/p>\n<p>    msg.setStandardButtons(QMessageBox.Ok)<\/p>\n<p>    msg.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u8fd0\u884c\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u5c06\u4ee5\u4e0a\u90e8\u5206\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if __name__ == &quot;__main__&quot;:<\/p>\n<p>    app, window = main()<\/p>\n<p>    create_button(window)<\/p>\n<p>    window.show()<\/p>\n<p>    sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Kivy\u5b9e\u73b0\u6309\u94ae\u5f39\u7a97<\/h3>\n<\/p>\n<p><p>Kivy\u662f\u4e00\u4e2a\u7528\u4e8e\u5f00\u53d1\u591a\u70b9\u89e6\u63a7\u5e94\u7528\u7684\u5f00\u6e90Python\u5e93\uff0c\u9002\u5408\u8de8\u5e73\u53f0\u5e94\u7528\u7684\u5f00\u53d1\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Kivy<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Kivy\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install kivy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u5e94\u7528\u7c7b<\/h4>\n<\/p>\n<p><p>\u5728Kivy\u4e2d\uff0c\u5e94\u7528\u7a0b\u5e8f\u901a\u5e38\u662f\u901a\u8fc7\u7ee7\u627f\u81ea<code>App<\/code>\u7c7b\u6765\u5b9e\u73b0\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.button import Button<\/p>\n<p>from kivy.uix.boxlayout import BoxLayout<\/p>\n<p>from kivy.uix.popup import Popup<\/p>\n<p>from kivy.uix.label import Label<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        layout = BoxLayout()<\/p>\n<p>        button = Button(text=&quot;Click Me&quot;)<\/p>\n<p>        button.bind(on_press=self.show_popup)<\/p>\n<p>        layout.add_widget(button)<\/p>\n<p>        return layout<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5b9a\u4e49\u5f39\u7a97\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Kivy\u7684Popup\u7c7b\u6765\u521b\u5efa\u5f39\u7a97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">    def show_popup(self, instance):<\/p>\n<p>        content = BoxLayout(orientation=&#39;vertical&#39;)<\/p>\n<p>        content.add_widget(Label(text=&#39;This is a popup window&#39;))<\/p>\n<p>        close_button = Button(text=&#39;Close&#39;)<\/p>\n<p>        content.add_widget(close_button)<\/p>\n<p>        popup = Popup(title=&#39;Popup&#39;, content=content, size_hint=(0.6, 0.4))<\/p>\n<p>        close_button.bind(on_press=popup.dismiss)<\/p>\n<p>        popup.open()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u8fd0\u884c\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Kivy\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if __name__ == &#39;__main__&#39;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528\u5408\u9002\u7684GUI\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u6309\u94ae\u89e6\u53d1\u5f39\u7a97\u7684\u529f\u80fd\u3002<strong>Tkinter\u9002\u5408\u7b80\u5355\u7684\u684c\u9762\u5e94\u7528\uff0cPyQt\u9002\u5408\u529f\u80fd\u590d\u6742\u7684\u5e94\u7528\uff0c\u800cKivy\u5219\u9002\u5408\u8de8\u5e73\u53f0\u5f00\u53d1<\/strong>\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u9700\u6c42\u548c\u4e2a\u4eba\u6280\u672f\u6808\u6765\u51b3\u5b9a\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u5e93\uff0c\u6838\u5fc3\u601d\u8def\u90fd\u662f\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\uff0c\u6dfb\u52a0\u6309\u94ae\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u5904\u7406\u5f39\u7a97\u903b\u8f91\u3002\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\u4ee3\u7801\uff0c\u53ef\u4ee5\u5feb\u901f\u4e0a\u624b\u5b9e\u73b0Python\u4e2d\u6309\u94ae\u89e6\u53d1\u5f39\u7a97\u7684\u529f\u80fd\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\u6309\u94ae\u4ee5\u663e\u793a\u5f39\u7a97\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u3002\u8981\u5b9e\u73b0\u6309\u94ae\u5f39\u7a97\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528Tkinter\u7684<code>Button<\/code>\u548c<code>messagebox<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u5bfc\u5165Tkinter\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u6309\u94ae\uff0c\u7ed1\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\uff0c\u8be5\u51fd\u6570\u4f1a\u663e\u793a\u5f39\u7a97\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5f39\u7a97\u7684\u5185\u5bb9\u548c\u6837\u5f0f\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528Tkinter\u7684<code>messagebox<\/code>\u6a21\u5757\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5f39\u7a97\u7684\u6807\u9898\u548c\u5185\u5bb9\u3002\u4f8b\u5982\uff0c<code>messagebox.showinfo(&quot;\u6807\u9898&quot;, &quot;\u8fd9\u91cc\u662f\u5f39\u7a97\u5185\u5bb9&quot;)<\/code>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u4fe1\u606f\u5f39\u7a97\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6309\u94ae\u7684\u989c\u8272\u3001\u5b57\u4f53\u7b49\u5c5e\u6027\u6765\u6539\u53d8\u6309\u94ae\u7684\u5916\u89c2\uff0c\u8fbe\u5230\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p><strong>\u5728\u5f39\u7a97\u4e2d\u53ef\u4ee5\u6dfb\u52a0\u54ea\u4e9b\u529f\u80fd\uff1f<\/strong><br \/>\u5f39\u7a97\u4e0d\u4ec5\u53ef\u4ee5\u663e\u793a\u4fe1\u606f\uff0c\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u6309\u94ae\uff0c\u5141\u8bb8\u7528\u6237\u8fdb\u884c\u9009\u62e9\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>messagebox.askyesno(&quot;\u6807\u9898&quot;, &quot;\u4f60\u786e\u5b9a\u8981\u7ee7\u7eed\u5417\uff1f&quot;)<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u201c\u662f\u201d\u548c\u201c\u5426\u201d\u6309\u94ae\u7684\u5f39\u7a97\u3002\u6839\u636e\u7528\u6237\u7684\u9009\u62e9\uff0c\u53ef\u4ee5\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\uff0c\u4ece\u800c\u589e\u52a0\u7a0b\u5e8f\u7684\u4ea4\u4e92\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9e\u73b0\u6309\u94ae\u89e6\u53d1\u5f39\u7a97\u7684\u529f\u80fd\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\u6765\u5b9e\u73b0\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecTkinter [&hellip;]","protected":false},"author":3,"featured_media":954237,"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\/954233"}],"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=954233"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/954233\/revisions"}],"predecessor-version":[{"id":954239,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/954233\/revisions\/954239"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/954237"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=954233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=954233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=954233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}