{"id":1187895,"date":"2025-01-15T20:10:48","date_gmt":"2025-01-15T12:10:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1187895.html"},"modified":"2025-01-15T20:10:52","modified_gmt":"2025-01-15T12:10:52","slug":"python%e5%a6%82%e4%bd%95%e8%b7%b3%e8%bd%ac%e8%bf%90%e8%a1%8c%e7%95%8c%e9%9d%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1187895.html","title":{"rendered":"python\u5982\u4f55\u8df3\u8f6c\u8fd0\u884c\u754c\u9762"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25140348\/d286bf00-3377-4ae4-9010-079a1e46e66e.webp\" alt=\"python\u5982\u4f55\u8df3\u8f6c\u8fd0\u884c\u754c\u9762\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8df3\u8f6c\u8fd0\u884c\u754c\u9762\u3001\u5305\u62ec\u4f7f\u7528GUI\u5e93\u3001\u521b\u5efa\u591a\u7a97\u53e3\u5e94\u7528\u4ee5\u53ca\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u63a7\u5236\u754c\u9762\u8df3\u8f6c\u7b49\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528GUI\u5e93\uff08\u5982Tkinter\u3001PyQt\u3001Kivy\u7b49\uff09\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u591a\u4e2a\u7a97\u53e3\u5e76\u5728\u9700\u8981\u65f6\u5207\u6362\u7a97\u53e3\u6765\u5b9e\u73b0\u754c\u9762\u8df3\u8f6c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Tkinter\u5b9e\u73b0\u754c\u9762\u8df3\u8f6c<\/h3>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u975e\u5e38\u9002\u5408\u521d\u5b66\u8005\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528Tkinter\u5728\u4e0d\u540c\u7684\u7a97\u53e3\u4e4b\u95f4\u8df3\u8f6c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import ttk<\/p>\n<p>class M<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nApp(tk.Tk):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.title(&quot;Main Window&quot;)<\/p>\n<p>        self.geometry(&quot;300x200&quot;)<\/p>\n<p>        self.label = ttk.Label(self, text=&quot;This is the main window&quot;)<\/p>\n<p>        self.label.pack(pady=20)<\/p>\n<p>        self.open_button = ttk.Button(self, text=&quot;Open Second Window&quot;, command=self.open_second_window)<\/p>\n<p>        self.open_button.pack(pady=20)<\/p>\n<p>    def open_second_window(self):<\/p>\n<p>        self.withdraw()  # Hide the main window<\/p>\n<p>        second_window = SecondWindow(self)<\/p>\n<p>        second_window.mainloop()<\/p>\n<p>class SecondWindow(tk.Toplevel):<\/p>\n<p>    def __init__(self, parent):<\/p>\n<p>        super().__init__(parent)<\/p>\n<p>        self.title(&quot;Second Window&quot;)<\/p>\n<p>        self.geometry(&quot;300x200&quot;)<\/p>\n<p>        self.label = ttk.Label(self, text=&quot;This is the second window&quot;)<\/p>\n<p>        self.label.pack(pady=20)<\/p>\n<p>        self.back_button = ttk.Button(self, text=&quot;Back to Main Window&quot;, command=self.back_to_main)<\/p>\n<p>        self.back_button.pack(pady=20)<\/p>\n<p>    def back_to_main(self):<\/p>\n<p>        self.destroy()<\/p>\n<p>        self.master.deiconify()  # Show the main window again<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    app = MainApp()<\/p>\n<p>    app.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>MainApp<\/code>\u7c7b\u8868\u793a\u4e3b\u7a97\u53e3\uff0c<code>SecondWindow<\/code>\u7c7b\u8868\u793a\u7b2c\u4e8c\u4e2a\u7a97\u53e3\u3002\u901a\u8fc7<code>open_second_window<\/code>\u65b9\u6cd5\u53ef\u4ee5\u6253\u5f00\u7b2c\u4e8c\u4e2a\u7a97\u53e3\uff0c\u5e76\u9690\u85cf\u4e3b\u7a97\u53e3\uff1b\u901a\u8fc7<code>back_to_main<\/code>\u65b9\u6cd5\u53ef\u4ee5\u9500\u6bc1\u7b2c\u4e8c\u4e2a\u7a97\u53e3\uff0c\u5e76\u91cd\u65b0\u663e\u793a\u4e3b\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528PyQt\u5b9e\u73b0\u754c\u9762\u8df3\u8f6c<\/h3>\n<\/p>\n<p><p>PyQt\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684GUI\u5e93\uff0c\u9002\u5408\u9700\u8981\u66f4\u591a\u529f\u80fd\u548c\u590d\u6742\u754c\u9762\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PyQt5\u5b9e\u73b0\u754c\u9762\u8df3\u8f6c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel, QWidget, QVBoxLayout<\/p>\n<p>class MainApp(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&quot;Main Window&quot;)<\/p>\n<p>        self.setGeometry(100, 100, 300, 200)<\/p>\n<p>        self.label = QLabel(&quot;This is the main window&quot;, self)<\/p>\n<p>        self.label.move(50, 50)<\/p>\n<p>        self.open_button = QPushButton(&quot;Open Second Window&quot;, self)<\/p>\n<p>        self.open_button.move(50, 100)<\/p>\n<p>        self.open_button.clicked.connect(self.open_second_window)<\/p>\n<p>    def open_second_window(self):<\/p>\n<p>        self.second_window = SecondWindow(self)<\/p>\n<p>        self.second_window.show()<\/p>\n<p>        self.hide()<\/p>\n<p>class SecondWindow(QWidget):<\/p>\n<p>    def __init__(self, parent):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.parent = parent<\/p>\n<p>        self.setWindowTitle(&quot;Second Window&quot;)<\/p>\n<p>        self.setGeometry(100, 100, 300, 200)<\/p>\n<p>        layout = QVBoxLayout()<\/p>\n<p>        self.label = QLabel(&quot;This is the second window&quot;, self)<\/p>\n<p>        layout.addWidget(self.label)<\/p>\n<p>        self.back_button = QPushButton(&quot;Back to Main Window&quot;, self)<\/p>\n<p>        self.back_button.clicked.connect(self.back_to_main)<\/p>\n<p>        layout.addWidget(self.back_button)<\/p>\n<p>        self.setLayout(layout)<\/p>\n<p>    def back_to_main(self):<\/p>\n<p>        self.close()<\/p>\n<p>        self.parent.show()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    app = QApplication(sys.argv)<\/p>\n<p>    main_app = MainApp()<\/p>\n<p>    main_app.show()<\/p>\n<p>    sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f7f\u7528\u4e86PyQt5\u5e93\uff0c\u901a\u8fc7<code>QMainWindow<\/code>\u548c<code>QWidget<\/code>\u5b9e\u73b0\u4e3b\u7a97\u53e3\u548c\u7b2c\u4e8c\u4e2a\u7a97\u53e3\u4e4b\u95f4\u7684\u8df3\u8f6c\u3002\u70b9\u51fb\u6309\u94ae\u53ef\u4ee5\u6253\u5f00\u7b2c\u4e8c\u4e2a\u7a97\u53e3\u5e76\u9690\u85cf\u4e3b\u7a97\u53e3\uff0c\u70b9\u51fb\u8fd4\u56de\u6309\u94ae\u53ef\u4ee5\u5173\u95ed\u7b2c\u4e8c\u4e2a\u7a97\u53e3\u5e76\u91cd\u65b0\u663e\u793a\u4e3b\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Kivy\u5b9e\u73b0\u754c\u9762\u8df3\u8f6c<\/h3>\n<\/p>\n<p><p>Kivy\u662f\u4e00\u4e2a\u9002\u5408\u5f00\u53d1\u591a\u5e73\u53f0\u5e94\u7528\u7a0b\u5e8f\u7684GUI\u5e93\uff0c\u7279\u522b\u9002\u5408\u79fb\u52a8\u5e94\u7528\u5f00\u53d1\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Kivy\u5b9e\u73b0\u754c\u9762\u8df3\u8f6c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.screenmanager import ScreenManager, Screen<\/p>\n<p>class MainScreen(Screen):<\/p>\n<p>    pass<\/p>\n<p>class SecondScreen(Screen):<\/p>\n<p>    pass<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        sm = ScreenManager()<\/p>\n<p>        sm.add_widget(MainScreen(name=&#39;main&#39;))<\/p>\n<p>        sm.add_widget(SecondScreen(name=&#39;second&#39;))<\/p>\n<p>        return sm<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u4e86Kivy\u7684<code>ScreenManager<\/code>\u6765\u7ba1\u7406\u4e0d\u540c\u7684\u5c4f\u5e55\uff08\u754c\u9762\uff09\u3002\u901a\u8fc7\u6dfb\u52a0\u591a\u4e2a<code>Screen<\/code>\u7c7b\u5b9e\u4f8b\u5230<code>ScreenManager<\/code>\uff0c\u5e76\u5728\u9700\u8981\u65f6\u5207\u6362\u5c4f\u5e55\uff0c\u53ef\u4ee5\u5b9e\u73b0\u754c\u9762\u8df3\u8f6c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u63a7\u5236\u754c\u9762\u8df3\u8f6c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528GUI\u5e93\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u63a7\u5236\u754c\u9762\u8df3\u8f6c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u7684\u547d\u4ee4\u884c\u53c2\u6570\u6765\u51b3\u5b9a\u542f\u52a8\u54ea\u4e2a\u754c\u9762\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from tkinter import Tk, Label<\/p>\n<p>def main():<\/p>\n<p>    if len(sys.argv) &gt; 1 and sys.argv[1] == &#39;second&#39;:<\/p>\n<p>        start_second_window()<\/p>\n<p>    else:<\/p>\n<p>        start_main_window()<\/p>\n<p>def start_main_window():<\/p>\n<p>    root = Tk()<\/p>\n<p>    root.title(&quot;Main Window&quot;)<\/p>\n<p>    root.geometry(&quot;300x200&quot;)<\/p>\n<p>    label = Label(root, text=&quot;This is the main window&quot;)<\/p>\n<p>    label.pack(pady=20)<\/p>\n<p>    root.mainloop()<\/p>\n<p>def start_second_window():<\/p>\n<p>    root = Tk()<\/p>\n<p>    root.title(&quot;Second Window&quot;)<\/p>\n<p>    root.geometry(&quot;300x200&quot;)<\/p>\n<p>    label = Label(root, text=&quot;This is the second window&quot;)<\/p>\n<p>    label.pack(pady=20)<\/p>\n<p>    root.mainloop()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6839\u636e\u547d\u4ee4\u884c\u53c2\u6570\u51b3\u5b9a\u542f\u52a8\u54ea\u4e2a\u7a97\u53e3\u3002\u4f8b\u5982\uff0c\u8fd0\u884c<code>python script.py<\/code>\u542f\u52a8\u4e3b\u7a97\u53e3\uff0c\u8fd0\u884c<code>python script.py second<\/code>\u542f\u52a8\u7b2c\u4e8c\u4e2a\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u754c\u9762\u8df3\u8f6c\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec<strong>\u4f7f\u7528Tkinter\u3001\u4f7f\u7528PyQt\u3001\u4f7f\u7528Kivy\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u63a7\u5236\u7b49<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u754c\u9762\u8df3\u8f6c\u3002\u4f7f\u7528\u8fd9\u4e9bGUI\u5e93\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u5e76\u7ed3\u5408\u4e0a\u8ff0\u65b9\u6cd5\u5b9e\u73b0\u7075\u6d3b\u7684\u754c\u9762\u8df3\u8f6c\u529f\u80fd\u3002\u901a\u8fc7\u5b66\u4e60\u548c\u5b9e\u8df5\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u80fd\u591f\u66f4\u597d\u5730\u638c\u63e1Python GUI\u7f16\u7a0b\u6280\u5de7\uff0c\u5f00\u53d1\u51fa\u529f\u80fd\u4e30\u5bcc\u3001\u7528\u6237\u4f53\u9a8c\u826f\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\u5728Python\u4e2d\u521b\u5efa\u591a\u4e2a\u8fd0\u884c\u754c\u9762\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\u5982Tkinter\u3001PyQt\u6216wxPython\u6765\u521b\u5efa\u591a\u4e2a\u8fd0\u884c\u754c\u9762\u3002\u60a8\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u754c\u9762\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u5728\u7279\u5b9a\u4e8b\u4ef6\uff08\u5982\u6309\u94ae\u70b9\u51fb\uff09\u4e2d\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u4ece\u800c\u5b9e\u73b0\u754c\u9762\u7684\u8df3\u8f6c\u548c\u5207\u6362\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u754c\u9762\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012\uff1f<\/strong><br \/>\u4e3a\u4e86\u5728\u4e0d\u540c\u7684\u754c\u9762\u4e4b\u95f4\u4f20\u9012\u6570\u636e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u5b58\u50a8\u6570\u636e\uff0c\u6216\u8005\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002\u5f88\u591aGUI\u6846\u67b6\u4e5f\u63d0\u4f9b\u4e86\u65b9\u6cd5\u6765\u66f4\u65b0\u5176\u4ed6\u754c\u9762\u7684\u7ec4\u4ef6\u3002\u4f8b\u5982\uff0c\u901a\u8fc7\u4fe1\u53f7\u548c\u69fd\u673a\u5236\uff0c\u60a8\u53ef\u4ee5\u5728\u4e00\u4e2a\u754c\u9762\u4fee\u6539\u6570\u636e\u5e76\u5b9e\u65f6\u66f4\u65b0\u53e6\u4e00\u4e2a\u754c\u9762\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528Web\u6846\u67b6\u5b9e\u73b0\u8df3\u8f6c\u754c\u9762\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Flask\u6216Django\u7b49Web\u6846\u67b6\u521b\u5efaWeb\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u901a\u8fc7URL\u8def\u7531\u5b9e\u73b0\u754c\u9762\u7684\u8df3\u8f6c\u3002\u5728\u7528\u6237\u8bbf\u95ee\u4e0d\u540c\u7684URL\u65f6\uff0c\u6846\u67b6\u4f1a\u81ea\u52a8\u6e32\u67d3\u76f8\u5e94\u7684\u6a21\u677f\uff0c\u4ece\u800c\u5b9e\u73b0\u754c\u9762\u4e4b\u95f4\u7684\u5207\u6362\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u5408\u6784\u5efa\u57fa\u4e8e\u6d4f\u89c8\u5668\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8df3\u8f6c\u8fd0\u884c\u754c\u9762\u3001\u5305\u62ec\u4f7f\u7528GUI\u5e93\u3001\u521b\u5efa\u591a\u7a97\u53e3\u5e94\u7528\u4ee5\u53ca\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u63a7\u5236\u754c\u9762\u8df3\u8f6c\u7b49\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1187901,"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\/1187895"}],"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=1187895"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187895\/revisions"}],"predecessor-version":[{"id":1187902,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187895\/revisions\/1187902"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1187901"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1187895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1187895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1187895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}