{"id":1118355,"date":"2025-01-08T18:34:40","date_gmt":"2025-01-08T10:34:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1118355.html"},"modified":"2025-01-08T18:34:43","modified_gmt":"2025-01-08T10:34:43","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80%e7%aa%97%e5%8f%a3%e9%a6%96%e9%80%89%e9%a1%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1118355.html","title":{"rendered":"python\u5982\u4f55\u6253\u5f00\u7a97\u53e3\u9996\u9009\u9879"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081859\/2843e632-d5eb-45dc-80cb-ce58a1d199ec.webp\" alt=\"python\u5982\u4f55\u6253\u5f00\u7a97\u53e3\u9996\u9009\u9879\" \/><\/p>\n<p><p> <strong>Python\u6253\u5f00\u7a97\u53e3\u9996\u9009\u9879\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528tkinter\u5e93\u3001\u4f7f\u7528PyQt\u5e93\u3001\u4f7f\u7528wxPython\u5e93<\/strong>\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0ctkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u6613\u4e8e\u4f7f\u7528\u548c\u5b66\u4e60\uff0c\u975e\u5e38\u9002\u5408\u521d\u5b66\u8005\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528tkinter\u5e93\u6765\u6253\u5f00\u7a97\u53e3\u9996\u9009\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Tkinter\u5e93<\/h3>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\uff08Graphical User Interface\uff09\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e9b\u57fa\u672c\u7684\u7ec4\u4ef6\uff0c\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u7b80\u5355\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Tkinter\u5e93\u6253\u5f00\u7a97\u53e3\u9996\u9009\u9879\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import messagebox<\/p>\n<p>def show_preferences():<\/p>\n<p>    prefs_window = tk.Toplevel(root)<\/p>\n<p>    prefs_window.title(&quot;Preferences&quot;)<\/p>\n<p>    prefs_window.geometry(&quot;300x200&quot;)<\/p>\n<p>    tk.Label(prefs_window, text=&quot;Preference 1:&quot;).pack(pady=10)<\/p>\n<p>    tk.Entry(prefs_window).pack(pady=10)<\/p>\n<p>    tk.Label(prefs_window, text=&quot;Preference 2:&quot;).pack(pady=10)<\/p>\n<p>    tk.Entry(prefs_window).pack(pady=10)<\/p>\n<p>    tk.Button(prefs_window, text=&quot;Save&quot;, command=lambda: messagebox.showinfo(&quot;Info&quot;, &quot;Preferences Saved&quot;)).pack(pady=10)<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;M<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n Window&quot;)<\/p>\n<p>root.geometry(&quot;400x300&quot;)<\/p>\n<p>tk.Button(root, text=&quot;Open Preferences&quot;, command=show_preferences).pack(pady=50)<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u4e3b\u7a97\u53e3<code>root<\/code>\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6309\u94ae\u3002\u5f53\u70b9\u51fb\u6309\u94ae\u65f6\uff0c\u4f1a\u521b\u5efa\u5e76\u6253\u5f00\u4e00\u4e2a\u65b0\u7684\u9876\u7ea7\u7a97\u53e3<code>prefs_window<\/code>\uff0c\u8fd9\u4e2a\u7a97\u53e3\u5305\u542b\u4e00\u4e9b\u6807\u7b7e\u3001\u8f93\u5165\u6846\u548c\u4e00\u4e2a\u4fdd\u5b58\u6309\u94ae\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528PyQt\u5e93<\/h3>\n<\/p>\n<p><p>PyQt\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684Python GUI\u5e93\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u590d\u6742\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528PyQt\u5e93\u6253\u5f00\u7a97\u53e3\u9996\u9009\u9879\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, QDialog, QVBoxLayout, QLabel, QLineEdit, QMessageBox<\/p>\n<p>class PreferencesDialog(QDialog):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&quot;Preferences&quot;)<\/p>\n<p>        self.setGeometry(100, 100, 300, 200)<\/p>\n<p>        layout = QVBoxLayout()<\/p>\n<p>        layout.addWidget(QLabel(&quot;Preference 1:&quot;))<\/p>\n<p>        layout.addWidget(QLineEdit())<\/p>\n<p>        layout.addWidget(QLabel(&quot;Preference 2:&quot;))<\/p>\n<p>        layout.addWidget(QLineEdit())<\/p>\n<p>        save_button = QPushButton(&quot;Save&quot;)<\/p>\n<p>        save_button.clicked.connect(self.save_preferences)<\/p>\n<p>        layout.addWidget(save_button)<\/p>\n<p>        self.setLayout(layout)<\/p>\n<p>    def save_preferences(self):<\/p>\n<p>        QMessageBox.information(self, &quot;Info&quot;, &quot;Preferences Saved&quot;)<\/p>\n<p>class MainWindow(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, 400, 300)<\/p>\n<p>        open_prefs_button = QPushButton(&quot;Open Preferences&quot;, self)<\/p>\n<p>        open_prefs_button.clicked.connect(self.show_preferences)<\/p>\n<p>        self.setCentralWidget(open_prefs_button)<\/p>\n<p>    def show_preferences(self):<\/p>\n<p>        prefs_dialog = PreferencesDialog()<\/p>\n<p>        prefs_dialog.exec_()<\/p>\n<p>app = QApplication(sys.argv)<\/p>\n<p>main_window = MainWindow()<\/p>\n<p>main_window.show()<\/p>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528PyQt\u521b\u5efa\u4e86\u4e00\u4e2a\u4e3b\u7a97\u53e3<code>MainWindow<\/code>\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6309\u94ae\u3002\u5f53\u70b9\u51fb\u6309\u94ae\u65f6\uff0c\u4f1a\u521b\u5efa\u5e76\u6253\u5f00\u4e00\u4e2a\u65b0\u7684\u5bf9\u8bdd\u7a97\u53e3<code>PreferencesDialog<\/code>\uff0c\u8fd9\u4e2a\u7a97\u53e3\u5305\u542b\u4e00\u4e9b\u6807\u7b7e\u3001\u8f93\u5165\u6846\u548c\u4e00\u4e2a\u4fdd\u5b58\u6309\u94ae\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528wxPython\u5e93<\/h3>\n<\/p>\n<p><p>wxPython\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684Python GUI\u5e93\uff0c\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528wxPython\u5e93\u6253\u5f00\u7a97\u53e3\u9996\u9009\u9879\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import wx<\/p>\n<p>class PreferencesDialog(wx.Dialog):<\/p>\n<p>    def __init__(self, parent):<\/p>\n<p>        super().__init__(parent, title=&quot;Preferences&quot;, size=(300, 200))<\/p>\n<p>        panel = wx.Panel(self)<\/p>\n<p>        vbox = wx.BoxSizer(wx.VERTICAL)<\/p>\n<p>        vbox.Add(wx.StaticText(panel, label=&quot;Preference 1:&quot;), flag=wx.ALL, border=10)<\/p>\n<p>        vbox.Add(wx.TextCtrl(panel), flag=wx.ALL | wx.EXPAND, border=10)<\/p>\n<p>        vbox.Add(wx.StaticText(panel, label=&quot;Preference 2:&quot;), flag=wx.ALL, border=10)<\/p>\n<p>        vbox.Add(wx.TextCtrl(panel), flag=wx.ALL | wx.EXPAND, border=10)<\/p>\n<p>        save_button = wx.Button(panel, label=&quot;Save&quot;)<\/p>\n<p>        save_button.Bind(wx.EVT_BUTTON, self.save_preferences)<\/p>\n<p>        vbox.Add(save_button, flag=wx.ALL | wx.CENTER, border=10)<\/p>\n<p>        panel.SetSizer(vbox)<\/p>\n<p>    def save_preferences(self, event):<\/p>\n<p>        wx.MessageBox(&quot;Preferences Saved&quot;, &quot;Info&quot;, wx.OK | wx.ICON_INFORMATION)<\/p>\n<p>class MainWindow(wx.Frame):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__(None, title=&quot;Main Window&quot;, size=(400, 300))<\/p>\n<p>        panel = wx.Panel(self)<\/p>\n<p>        vbox = wx.BoxSizer(wx.VERTICAL)<\/p>\n<p>        open_prefs_button = wx.Button(panel, label=&quot;Open Preferences&quot;)<\/p>\n<p>        open_prefs_button.Bind(wx.EVT_BUTTON, self.show_preferences)<\/p>\n<p>        vbox.Add(open_prefs_button, flag=wx.ALL | wx.CENTER, border=50)<\/p>\n<p>        panel.SetSizer(vbox)<\/p>\n<p>    def show_preferences(self, event):<\/p>\n<p>        prefs_dialog = PreferencesDialog(self)<\/p>\n<p>        prefs_dialog.ShowModal()<\/p>\n<p>        prefs_dialog.Destroy()<\/p>\n<p>app = wx.App(False)<\/p>\n<p>main_window = MainWindow()<\/p>\n<p>main_window.Show()<\/p>\n<p>app.MainLoop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528wxPython\u521b\u5efa\u4e86\u4e00\u4e2a\u4e3b\u7a97\u53e3<code>MainWindow<\/code>\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6309\u94ae\u3002\u5f53\u70b9\u51fb\u6309\u94ae\u65f6\uff0c\u4f1a\u521b\u5efa\u5e76\u6253\u5f00\u4e00\u4e2a\u65b0\u7684\u5bf9\u8bdd\u7a97\u53e3<code>PreferencesDialog<\/code>\uff0c\u8fd9\u4e2a\u7a97\u53e3\u5305\u542b\u4e00\u4e9b\u6807\u7b7e\u3001\u8f93\u5165\u6846\u548c\u4e00\u4e2a\u4fdd\u5b58\u6309\u94ae\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u4e09\u79cd\u4f7f\u7528Python\u6253\u5f00\u7a97\u53e3\u9996\u9009\u9879\u7684\u65b9\u6cd5\uff1aTkinter\u3001PyQt\u548cwxPython\u3002<strong>\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f<\/strong>\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u9879\u76ee\u9700\u6c42\u548c\u4e2a\u4eba\u504f\u597d\u3002<strong>Tkinter\u9002\u5408\u521d\u5b66\u8005\uff0cPyQt\u9002\u5408\u9700\u8981\u590d\u6742GUI\u7684\u9879\u76ee\uff0cwxPython\u5219\u63d0\u4f9b\u4e86\u7075\u6d3b\u4e14\u5f3a\u5927\u7684GUI\u5f00\u53d1\u80fd\u529b<\/strong>\u3002\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u5b9e\u73b0\u7a97\u53e3\u9996\u9009\u9879\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6253\u5f00\u7a97\u53e3\u9996\u9009\u9879\u8bbe\u7f6e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6253\u5f00\u7a97\u53e3\u9996\u9009\u9879\u901a\u5e38\u6d89\u53ca\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\uff0c\u5982Tkinter\u3001PyQt\u6216wxPython\u3002\u4ee5Tkinter\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u8bbe\u7f6e\u7a97\u53e3\uff0c\u4f7f\u7528<code>Toplevel<\/code>\u7c7b\u6765\u5b9e\u73b0\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u5bfc\u5165Tkinter\u5e93\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7a97\u53e3\u5b9e\u4f8b\uff0c\u5e76\u5728\u7a97\u53e3\u4e2d\u6dfb\u52a0\u6240\u9700\u7684\u8bbe\u7f6e\u9009\u9879\u3002<\/p>\n<p><strong>\u5728Python GUI\u4e2d\uff0c\u54ea\u4e9b\u5e93\u6700\u9002\u5408\u521b\u5efa\u7a97\u53e3\u9996\u9009\u9879\uff1f<\/strong><br \/>\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u7a97\u53e3\u9996\u9009\u9879\uff0c\u6700\u5e38\u7528\u7684\u5305\u62ecTkinter\u3001PyQt\u548cwxPython\u3002Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u6613\u4e8e\u4f7f\u7528\u4e14\u9002\u5408\u5c0f\u578b\u9879\u76ee\u3002PyQt\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u9700\u8981\u590d\u6742\u754c\u9762\u7684\u5e94\u7528\uff0c\u800cwxPython\u5219\u63d0\u4f9b\u4e86\u672c\u5730\u5316\u7684\u5916\u89c2\u548c\u611f\u89c9\uff0c\u9002\u5408\u9700\u8981\u8de8\u5e73\u53f0\u652f\u6301\u7684\u9879\u76ee\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4fdd\u5b58\u7528\u6237\u7684\u7a97\u53e3\u9996\u9009\u9879\uff1f<\/strong><br \/>\u5728Python\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4fdd\u5b58\u7528\u6237\u7a97\u53e3\u9996\u9009\u9879\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4f8b\u5982\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\uff08\u5982JSON\u6216INI\u6587\u4ef6\uff09\u6765\u5b58\u50a8\u7528\u6237\u7684\u8bbe\u7f6e\u3002\u7528\u6237\u5728\u9996\u9009\u9879\u7a97\u53e3\u4e2d\u8fdb\u884c\u66f4\u6539\u540e\uff0c\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u503c\u5199\u5165\u6587\u4ef6\u4e2d\uff0c\u5e76\u5728\u5e94\u7528\u542f\u52a8\u65f6\u8bfb\u53d6\u8fd9\u4e9b\u6587\u4ef6\u4ee5\u6062\u590d\u8bbe\u7f6e\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u5e93\u5b58\u50a8\u66f4\u590d\u6742\u7684\u9996\u9009\u9879\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6253\u5f00\u7a97\u53e3\u9996\u9009\u9879\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528tkinter\u5e93\u3001\u4f7f\u7528PyQt\u5e93\u3001\u4f7f\u7528wxPython\u5e93\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1118360,"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\/1118355"}],"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=1118355"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1118355\/revisions"}],"predecessor-version":[{"id":1118362,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1118355\/revisions\/1118362"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1118360"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1118355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1118355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1118355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}