{"id":1150984,"date":"2025-01-13T17:08:00","date_gmt":"2025-01-13T09:08:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1150984.html"},"modified":"2025-01-13T17:08:02","modified_gmt":"2025-01-13T09:08:02","slug":"python%e5%a6%82%e4%bd%95%e5%bf%ab%e9%80%9f%e5%bc%80%e5%8f%91%e7%95%8c%e9%9d%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1150984.html","title":{"rendered":"python\u5982\u4f55\u5feb\u901f\u5f00\u53d1\u754c\u9762"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25181326\/2a1a1bc0-37b7-477f-bb2c-25b51b7624f3.webp\" alt=\"python\u5982\u4f55\u5feb\u901f\u5f00\u53d1\u754c\u9762\" \/><\/p>\n<p><p> <strong>Python\u5feb\u901f\u5f00\u53d1\u754c\u9762\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528GUI\u5e93\u3001\u4f7f\u7528\u62d6\u653e\u754c\u9762\u8bbe\u8ba1\u5de5\u5177\u3001\u638c\u63e1\u57fa\u672c\u7684\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b\u3001\u9009\u62e9\u5408\u9002\u7684\u6846\u67b6\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528GUI\u5e93\u662f\u5feb\u901f\u5f00\u53d1Python\u754c\u9762\u7684\u5173\u952e\u3002\u5e38\u7528\u7684GUI\u5e93\u6709Tkinter\u3001PyQt\u3001Kivy\u7b49\u3002\u672c\u6587\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u5feb\u901f\u5f00\u53d1Python\u754c\u9762\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001GUI\u5e93\u4ecb\u7ecd<\/h3>\n<\/p>\n<p><h4>1\u3001Tkinter<\/h4>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u5b83\u968fPython\u5b89\u88c5\u5305\u4e00\u8d77\u5b89\u88c5\u3002Tkinter\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u5e93\uff0c\u9002\u5408\u5f00\u53d1\u7b80\u5355\u7684\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b89\u88c5\u4e0e\u5165\u95e8<\/strong><\/li>\n<\/ul>\n<p><p>Tkinter\u65e0\u9700\u989d\u5916\u5b89\u88c5\uff0c\u53ea\u8981\u4f60\u5b89\u88c5\u4e86Python\uff0c\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528Tkinter\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Tkinter\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def on_button_click():<\/p>\n<p>    label.config(text=&quot;Hello, Tkinter!&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;Welcome to Tkinter&quot;)<\/p>\n<p>label.pack()<\/p>\n<p>button = tk.Button(root, text=&quot;Click Me&quot;, command=on_button_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<ul>\n<li><strong>\u7ec4\u4ef6\u4ecb\u7ecd<\/strong><\/li>\n<\/ul>\n<p><p>Tkinter\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ec4\u4ef6\uff0c\u5982Label\u3001Button\u3001Entry\u3001Text\u3001Canvas\u7b49\uff0c\u53ef\u4ee5\u6ee1\u8db3\u57fa\u672c\u7684\u754c\u9762\u5f00\u53d1\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>2\u3001PyQt<\/h4>\n<\/p>\n<p><p>PyQt\u662fPython\u7ed1\u5b9a\u7684Qt\u5e93\uff0c\u5b83\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684GUI\u5e93\uff0c\u9002\u5408\u5f00\u53d1\u590d\u6742\u7684\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002PyQt\u652f\u6301\u8de8\u5e73\u53f0\u5f00\u53d1\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ec4\u4ef6\u548c\u529f\u80fd\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b89\u88c5\u4e0e\u5165\u95e8<\/strong><\/li>\n<\/ul>\n<p><p>\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5PyQt\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyQt5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684PyQt\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QLabel, QPushButton, QVBoxLayout, QWidget<\/p>\n<p>def on_button_click():<\/p>\n<p>    label.setText(&quot;Hello, PyQt!&quot;)<\/p>\n<p>app = QApplication(sys.argv)<\/p>\n<p>window = QWidget()<\/p>\n<p>window.setWindowTitle(&#39;PyQt Example&#39;)<\/p>\n<p>layout = QVBoxLayout()<\/p>\n<p>label = QLabel(&#39;Welcome to PyQt&#39;)<\/p>\n<p>layout.addWidget(label)<\/p>\n<p>button = QPushButton(&#39;Click Me&#39;)<\/p>\n<p>button.clicked.connect(on_button_click)<\/p>\n<p>layout.addWidget(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<ul>\n<li><strong>\u7ec4\u4ef6\u4ecb\u7ecd<\/strong><\/li>\n<\/ul>\n<p><p>PyQt\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ec4\u4ef6\uff0c\u5982QLabel\u3001QPushButton\u3001QLineEdit\u3001QTextEdit\u3001QTableWidget\u7b49\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u590d\u6742\u7684\u754c\u9762\u5f00\u53d1\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>3\u3001Kivy<\/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\u5feb\u901f\u5f00\u53d1\u3002\u5b83\u652f\u6301\u8de8\u5e73\u53f0\u5f00\u53d1\uff0c\u9002\u5408\u5f00\u53d1\u79fb\u52a8\u5e94\u7528\u7a0b\u5e8f\u548c\u591a\u70b9\u89e6\u63a7\u754c\u9762\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b89\u88c5\u4e0e\u5165\u95e8<\/strong><\/li>\n<\/ul>\n<p><p>\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5Kivy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install kivy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Kivy\u793a\u4f8b\uff1a<\/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>from kivy.uix.button import Button<\/p>\n<p>from kivy.uix.boxlayout import BoxLayout<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        layout = BoxLayout(orientation=&#39;vertical&#39;)<\/p>\n<p>        self.label = Label(text=&#39;Welcome to Kivy&#39;)<\/p>\n<p>        layout.add_widget(self.label)<\/p>\n<p>        button = Button(text=&#39;Click Me&#39;)<\/p>\n<p>        button.bind(on_press=self.on_button_click)<\/p>\n<p>        layout.add_widget(button)<\/p>\n<p>        return layout<\/p>\n<p>    def on_button_click(self, instance):<\/p>\n<p>        self.label.text = &#39;Hello, Kivy!&#39;<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u7ec4\u4ef6\u4ecb\u7ecd<\/strong><\/li>\n<\/ul>\n<p><p>Kivy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ec4\u4ef6\uff0c\u5982Label\u3001Button\u3001TextInput\u3001Canvas\u7b49\uff0c\u9002\u5408\u5f00\u53d1\u89e6\u63a7\u53cb\u597d\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u62d6\u653e\u754c\u9762\u8bbe\u8ba1\u5de5\u5177<\/h3>\n<\/p>\n<p><h4>1\u3001Qt Designer<\/h4>\n<\/p>\n<p><p>Qt Designer\u662f\u4e00\u4e2a\u7528\u4e8e\u8bbe\u8ba1Qt\u754c\u9762\u7684\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u53ef\u4ee5\u751f\u6210PyQt\u4ee3\u7801\u3002\u4f7f\u7528Qt Designer\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u754c\u9762\u8bbe\u8ba1\u8fc7\u7a0b\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b89\u88c5\u4e0e\u4f7f\u7528<\/strong><\/li>\n<\/ul>\n<p><p>Qt Designer\u901a\u5e38\u968fPyQt\u4e00\u8d77\u5b89\u88c5\uff0c\u53ef\u4ee5\u5728PyQt\u5b89\u88c5\u76ee\u5f55\u4e2d\u627e\u5230\u3002\u4f7f\u7528Qt Designer\u521b\u5efa\u754c\u9762\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pyuic5<\/code>\u5de5\u5177\u5c06\u8bbe\u8ba1\u6587\u4ef6\u8f6c\u6362\u4e3aPython\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyuic5 -o output.py input.ui<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001Glade<\/h4>\n<\/p>\n<p><p>Glade\u662f\u4e00\u4e2a\u7528\u4e8e\u8bbe\u8ba1GTK+\u754c\u9762\u7684\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u53ef\u4ee5\u751f\u6210Gtk\u4ee3\u7801\u3002\u4f7f\u7528Glade\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u754c\u9762\u8bbe\u8ba1\u8fc7\u7a0b\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b89\u88c5\u4e0e\u4f7f\u7528<\/strong><\/li>\n<\/ul>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u5b89\u88c5Glade\uff0c\u4f8b\u5982\u5728Ubuntu\u4e0a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install glade<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528Glade\u521b\u5efa\u754c\u9762\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Gtk.Builder<\/code>\u52a0\u8f7d\u8bbe\u8ba1\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gi<\/p>\n<p>gi.require_version(&#39;Gtk&#39;, &#39;3.0&#39;)<\/p>\n<p>from gi.repository import Gtk<\/p>\n<p>class MyWindow(Gtk.Window):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        Gtk.Window.__init__(self, title=&quot;Glade Example&quot;)<\/p>\n<p>        builder = Gtk.Builder()<\/p>\n<p>        builder.add_from_file(&quot;interface.glade&quot;)<\/p>\n<p>        self.label = builder.get_object(&quot;label&quot;)<\/p>\n<p>        button = builder.get_object(&quot;button&quot;)<\/p>\n<p>        button.connect(&quot;clicked&quot;, self.on_button_click)<\/p>\n<p>        self.add(builder.get_object(&quot;main_container&quot;))<\/p>\n<p>    def on_button_click(self, widget):<\/p>\n<p>        self.label.set_text(&quot;Hello, Glade!&quot;)<\/p>\n<p>win = MyWindow()<\/p>\n<p>win.connect(&quot;destroy&quot;, Gtk.main_quit)<\/p>\n<p>win.show_all()<\/p>\n<p>Gtk.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b\u662fGUI\u5f00\u53d1\u7684\u57fa\u7840\uff0c\u7406\u89e3\u4e8b\u4ef6\u548c\u56de\u8c03\u51fd\u6570\u7684\u6982\u5ff5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5f00\u53d1\u4ea4\u4e92\u5f0f\u754c\u9762\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4e8b\u4ef6\u4e0e\u56de\u8c03\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728GUI\u7f16\u7a0b\u4e2d\uff0c\u4e8b\u4ef6\u662f\u7528\u6237\u4ea4\u4e92\uff08\u5982\u70b9\u51fb\u6309\u94ae\u3001\u8f93\u5165\u6587\u672c\u7b49\uff09\u89e6\u53d1\u7684\u52a8\u4f5c\uff0c\u56de\u8c03\u51fd\u6570\u662f\u5904\u7406\u8fd9\u4e9b\u4e8b\u4ef6\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5728Tkinter\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>command<\/code>\u53c2\u6570\u6307\u5b9a\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">button = tk.Button(root, text=&quot;Click Me&quot;, command=on_button_click)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728PyQt\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>connect<\/code>\u65b9\u6cd5\u5c06\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u8fde\u63a5\u5230\u56de\u8c03\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">button.clicked.connect(on_button_click)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5e38\u89c1\u4e8b\u4ef6\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5904\u7406\u5e38\u89c1\u4e8b\u4ef6\uff08\u5982\u70b9\u51fb\u3001\u8f93\u5165\u3001\u9009\u62e9\u7b49\uff09\u662fGUI\u7f16\u7a0b\u7684\u57fa\u672c\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u5728Kivy\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>bind<\/code>\u65b9\u6cd5\u5c06\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u8fde\u63a5\u5230\u56de\u8c03\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">button.bind(on_press=self.on_button_click)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5904\u7406\u8f93\u5165\u4e8b\u4ef6\u53ef\u4ee5\u4f7f\u7528\u76f8\u5e94\u7684\u8f93\u5165\u7ec4\u4ef6\uff0c\u5982Tkinter\u7684<code>Entry<\/code>\u3001PyQt\u7684<code>QLineEdit<\/code>\u3001Kivy\u7684<code>TextInput<\/code>\u7b49\uff0c\u5e76\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u83b7\u53d6\u8f93\u5165\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">entry = tk.Entry(root)<\/p>\n<p>entry.pack()<\/p>\n<p>input_value = entry.get()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u9009\u62e9\u5408\u9002\u7684\u6846\u67b6<\/h3>\n<\/p>\n<p><h4>1\u3001\u6839\u636e\u9700\u6c42\u9009\u62e9\u6846\u67b6<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684GUI\u6846\u67b6\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u4e0d\u540c\u7684\u6846\u67b6\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>Tkinter<\/strong>\uff1a\u9002\u5408\u5f00\u53d1\u7b80\u5355\u7684\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\uff0c\u8f7b\u91cf\u7ea7\uff0c\u6613\u4e8e\u5b66\u4e60\u3002<\/li>\n<li><strong>PyQt<\/strong>\uff1a\u9002\u5408\u5f00\u53d1\u590d\u6742\u7684\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\uff0c\u529f\u80fd\u5f3a\u5927\uff0c\u7ec4\u4ef6\u4e30\u5bcc\u3002<\/li>\n<li><strong>Kivy<\/strong>\uff1a\u9002\u5408\u5f00\u53d1\u79fb\u52a8\u5e94\u7528\u7a0b\u5e8f\u548c\u591a\u70b9\u89e6\u63a7\u754c\u9762\uff0c\u652f\u6301\u8de8\u5e73\u53f0\u5f00\u53d1\u3002<\/li>\n<\/ul>\n<p><h4>2\u3001\u6027\u80fd\u4e0e\u6269\u5c55\u6027<\/h4>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u6846\u67b6\u65f6\uff0c\u8fd8\u9700\u8981\u8003\u8651\u6027\u80fd\u4e0e\u6269\u5c55\u6027\u3002PyQt\u548cKivy\u5728\u6027\u80fd\u548c\u6269\u5c55\u6027\u65b9\u9762\u8868\u73b0\u4f18\u5f02\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u548c\u9ad8\u6027\u80fd\u9700\u6c42\u7684\u5e94\u7528\u7a0b\u5e8f\u3002Tkinter\u867d\u7136\u8f7b\u91cf\uff0c\u4f46\u5728\u5904\u7406\u590d\u6742\u754c\u9762\u548c\u9ad8\u6027\u80fd\u9700\u6c42\u65f6\u53ef\u80fd\u8868\u73b0\u4e0d\u8db3\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f18\u5316\u548c\u8c03\u8bd5<\/h3>\n<\/p>\n<p><h4>1\u3001\u4ee3\u7801\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u53d1GUI\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u4ee3\u7801\u4f18\u5316\u662f\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u4f18\u5316\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97<\/strong>\uff1a\u907f\u514d\u5728\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4e2d\u8fdb\u884c\u590d\u6742\u8ba1\u7b97\uff0c\u53ef\u4ee5\u5c06\u8ba1\u7b97\u903b\u8f91\u653e\u5728\u540e\u53f0\u7ebf\u7a0b\u4e2d\u3002<\/li>\n<li><strong>\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784<\/strong>\uff1a\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u5b58\u50a8\u548c\u5904\u7406\u6570\u636e\uff0c\u63d0\u9ad8\u8bbf\u95ee\u548c\u64cd\u4f5c\u6548\u7387\u3002<\/li>\n<li><strong>\u51cf\u5c11\u754c\u9762\u5237\u65b0\u9891\u7387<\/strong>\uff1a\u907f\u514d\u9891\u7e41\u5237\u65b0\u754c\u9762\uff0c\u53ef\u4ee5\u901a\u8fc7\u5224\u65ad\u662f\u5426\u9700\u8981\u66f4\u65b0\u6765\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u5237\u65b0\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><h4>2\u3001\u8c03\u8bd5\u6280\u5de7<\/h4>\n<\/p>\n<p><p>\u8c03\u8bd5\u662f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4e0d\u53ef\u907f\u514d\u7684\u4e00\u90e8\u5206\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u8c03\u8bd5\u6280\u5de7\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u65e5\u5fd7<\/strong>\uff1a\u901a\u8fc7\u65e5\u5fd7\u8bb0\u5f55\u5173\u952e\u6b65\u9aa4\u548c\u53d8\u91cf\u503c\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5b9a\u4f4d\u548c\u89e3\u51b3\u95ee\u9898\u3002<\/li>\n<li><strong>\u65ad\u70b9\u8c03\u8bd5<\/strong>\uff1a\u4f7f\u7528IDE\uff08\u5982PyCharm\u3001VSCode\uff09\u63d0\u4f9b\u7684\u65ad\u70b9\u8c03\u8bd5\u529f\u80fd\uff0c\u53ef\u4ee5\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u68c0\u67e5\u53d8\u91cf\u503c\u548c\u7a0b\u5e8f\u72b6\u6001\u3002<\/li>\n<li><strong>\u5355\u5143\u6d4b\u8bd5<\/strong>\uff1a\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u53ef\u4ee5\u5e2e\u52a9\u9a8c\u8bc1\u4ee3\u7801\u903b\u8f91\u7684\u6b63\u786e\u6027\uff0c\u53ca\u65f6\u53d1\u73b0\u548c\u4fee\u590d\u95ee\u9898\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u6848\u4f8b\u5206\u6790<\/h3>\n<\/p>\n<p><h4>1\u3001\u5f00\u53d1\u7b80\u5355\u8bb0\u4e8b\u672c\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u5c06\u4f7f\u7528Tkinter\u5f00\u53d1\u4e00\u4e2a\u7b80\u5355\u7684\u8bb0\u4e8b\u672c\u5e94\u7528\uff0c\u5305\u542b\u6587\u672c\u7f16\u8f91\u3001\u4fdd\u5b58\u3001\u6253\u5f00\u7b49\u57fa\u672c\u529f\u80fd\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u521b\u5efa\u57fa\u672c\u754c\u9762<\/strong><\/li>\n<\/ul>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Tkinter\u754c\u9762\uff0c\u5305\u542b\u6587\u672c\u7f16\u8f91\u533a\u57df\u548c\u83dc\u5355\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import filedialog<\/p>\n<p>def new_file():<\/p>\n<p>    text.delete(&#39;1.0&#39;, tk.END)<\/p>\n<p>def open_file():<\/p>\n<p>    file_path = filedialog.askopenfilename()<\/p>\n<p>    if file_path:<\/p>\n<p>        with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>            content = file.read()<\/p>\n<p>            text.delete(&#39;1.0&#39;, tk.END)<\/p>\n<p>            text.insert(&#39;1.0&#39;, content)<\/p>\n<p>def save_file():<\/p>\n<p>    file_path = filedialog.asksaveasfilename()<\/p>\n<p>    if file_path:<\/p>\n<p>        with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>            content = text.get(&#39;1.0&#39;, tk.END)<\/p>\n<p>            file.write(content)<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Simple Notepad&quot;)<\/p>\n<p>menu = tk.Menu(root)<\/p>\n<p>root.config(menu=menu)<\/p>\n<p>file_menu = tk.Menu(menu)<\/p>\n<p>menu.add_cascade(label=&quot;File&quot;, menu=file_menu)<\/p>\n<p>file_menu.add_command(label=&quot;New&quot;, command=new_file)<\/p>\n<p>file_menu.add_command(label=&quot;Open&quot;, command=open_file)<\/p>\n<p>file_menu.add_command(label=&quot;Save&quot;, command=save_file)<\/p>\n<p>file_menu.add_separator()<\/p>\n<p>file_menu.add_command(label=&quot;Exit&quot;, command=root.quit)<\/p>\n<p>text = tk.Text(root)<\/p>\n<p>text.pack(expand=True, fill=tk.BOTH)<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u529f\u80fd\u5b9e\u73b0<\/strong><\/li>\n<\/ul>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5b9e\u73b0\u4e86\u65b0\u5efa\u3001\u6253\u5f00\u3001\u4fdd\u5b58\u6587\u4ef6\u7684\u57fa\u672c\u529f\u80fd\u3002\u53ef\u4ee5\u901a\u8fc7\u83dc\u5355\u9879\u89e6\u53d1\u76f8\u5e94\u7684\u64cd\u4f5c\uff0c\u4f7f\u7528<code>filedialog<\/code>\u6a21\u5757\u9009\u62e9\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5f00\u53d1\u56fe\u7247\u6d4f\u89c8\u5668<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u5c06\u4f7f\u7528PyQt\u5f00\u53d1\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u7247\u6d4f\u89c8\u5668\u5e94\u7528\uff0c\u5305\u542b\u56fe\u7247\u5c55\u793a\u3001\u4e0a\u4e00\u5f20\u3001\u4e0b\u4e00\u5f20\u7b49\u529f\u80fd\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u521b\u5efa\u57fa\u672c\u754c\u9762<\/strong><\/li>\n<\/ul>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684PyQt\u754c\u9762\uff0c\u5305\u542b\u56fe\u7247\u5c55\u793a\u533a\u57df\u548c\u63a7\u5236\u6309\u94ae\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import os<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QLabel, QPushButton, QVBoxLayout, QWidget, QFileDialog<\/p>\n<p>from PyQt5.QtGui import QPixmap<\/p>\n<p>class ImageBrowser(QWidget):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&#39;Image Browser&#39;)<\/p>\n<p>        self.layout = QVBoxLayout()<\/p>\n<p>        self.setLayout(self.layout)<\/p>\n<p>        self.label = QLabel(&#39;No image loaded&#39;)<\/p>\n<p>        self.layout.addWidget(self.label)<\/p>\n<p>        self.prev_button = QPushButton(&#39;Previous&#39;)<\/p>\n<p>        self.prev_button.clicked.connect(self.show_prev_image)<\/p>\n<p>        self.layout.addWidget(self.prev_button)<\/p>\n<p>        self.next_button = QPushButton(&#39;Next&#39;)<\/p>\n<p>        self.next_button.clicked.connect(self.show_next_image)<\/p>\n<p>        self.layout.addWidget(self.next_button)<\/p>\n<p>        self.images = []<\/p>\n<p>        self.current_index = -1<\/p>\n<p>    def load_images(self, folder):<\/p>\n<p>        self.images = [os.path.join(folder, file) for file in os.listdir(folder) if file.endswith((&#39;png&#39;, &#39;jpg&#39;, &#39;jpeg&#39;, &#39;bmp&#39;))]<\/p>\n<p>        self.current_index = 0 if self.images else -1<\/p>\n<p>        self.show_image()<\/p>\n<p>    def show_image(self):<\/p>\n<p>        if 0 &lt;= self.current_index &lt; len(self.images):<\/p>\n<p>            pixmap = QPixmap(self.images[self.current_index])<\/p>\n<p>            self.label.setPixmap(pixmap)<\/p>\n<p>        else:<\/p>\n<p>            self.label.setText(&#39;No image loaded&#39;)<\/p>\n<p>    def show_prev_image(self):<\/p>\n<p>        if self.current_index &gt; 0:<\/p>\n<p>            self.current_index -= 1<\/p>\n<p>            self.show_image()<\/p>\n<p>    def show_next_image(self):<\/p>\n<p>        if self.current_index &lt; len(self.images) - 1:<\/p>\n<p>            self.current_index += 1<\/p>\n<p>            self.show_image()<\/p>\n<p>def main():<\/p>\n<p>    app = QApplication(sys.argv)<\/p>\n<p>    browser = ImageBrowser()<\/p>\n<p>    folder = QFileDialog.getExistingDirectory(None, &#39;Select Image Folder&#39;)<\/p>\n<p>    if folder:<\/p>\n<p>        browser.load_images(folder)<\/p>\n<p>        browser.show()<\/p>\n<p>    sys.exit(app.exec_())<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u529f\u80fd\u5b9e\u73b0<\/strong><\/li>\n<\/ul>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5b9e\u73b0\u4e86\u56fe\u7247\u5c55\u793a\u3001\u4e0a\u4e00\u5f20\u3001\u4e0b\u4e00\u5f20\u7684\u57fa\u672c\u529f\u80fd\u3002\u901a\u8fc7<code>QFileDialog<\/code>\u9009\u62e9\u56fe\u7247\u6587\u4ef6\u5939\uff0c\u5e76\u52a0\u8f7d\u56fe\u7247\u5217\u8868\u3002\u4f7f\u7528<code>QPixmap<\/code>\u663e\u793a\u5f53\u524d\u56fe\u7247\uff0c\u5e76\u901a\u8fc7\u6309\u94ae\u63a7\u5236\u6d4f\u89c8\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684GUI\u5e93\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u5f00\u53d1\u754c\u9762\u5e94\u7528\u3002\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684GUI\u5e93\u3001\u4f7f\u7528\u62d6\u653e\u754c\u9762\u8bbe\u8ba1\u5de5\u5177\u3001\u638c\u63e1\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b\u3001\u4f18\u5316\u4ee3\u7801\u548c\u8c03\u8bd5\u6280\u5de7\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u3002\u672c\u6587\u4ecb\u7ecd\u4e86Tkinter\u3001PyQt\u3001Kivy\u7b49\u5e38\u7528GUI\u5e93\u7684\u57fa\u672c\u7528\u6cd5\uff0c\u5e76\u901a\u8fc7\u6848\u4f8b\u5206\u6790\u5c55\u793a\u4e86\u5b9e\u9645\u5e94\u7528\u5f00\u53d1\u8fc7\u7a0b\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u754c\u9762\u5f00\u53d1\u7684\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5feb\u901f\u5f00\u53d1\u7528\u6237\u754c\u9762\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u5feb\u901f\u5f00\u53d1\u7528\u6237\u754c\u9762\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u5305\u62ecTkinter\u3001PyQt\u548cKivy\u3002Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u9002\u5408\u7b80\u5355\u7684\u5e94\u7528\u7a0b\u5e8f\uff1bPyQt\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5e93\uff0c\u9002\u5408\u5f00\u53d1\u590d\u6742\u7684\u684c\u9762\u5e94\u7528\uff1b\u800cKivy\u5219\u66f4\u9002\u5408\u79fb\u52a8\u8bbe\u5907\u548c\u591a\u70b9\u89e6\u63a7\u5e94\u7528\u3002\u9009\u62e9\u9002\u5408\u4f60\u9879\u76ee\u9700\u6c42\u7684\u5e93\uff0c\u53ef\u4ee5\u5927\u5e45\u63d0\u5347\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<p><strong>Python\u5f00\u53d1\u754c\u9762\u9700\u8981\u54ea\u4e9b\u57fa\u672c\u77e5\u8bc6\uff1f<\/strong><br \/>\u5728\u5f00\u59cb\u4f7f\u7528Python\u5f00\u53d1\u7528\u6237\u754c\u9762\u4e4b\u524d\uff0c\u638c\u63e1Python\u8bed\u8a00\u7684\u57fa\u7840\u77e5\u8bc6\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u6b64\u5916\uff0c\u5bf9\u4e8e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u7406\u89e3\u3001\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b\u7684\u6982\u5ff5\u4ee5\u53caGUI\u8bbe\u8ba1\u7684\u57fa\u672c\u539f\u5219\u90fd\u5c06\u6709\u52a9\u4e8e\u4f60\u66f4\u6709\u6548\u5730\u5f00\u53d1\u754c\u9762\u3002\u5b66\u4e60\u5982\u4f55\u4f7f\u7528\u6240\u9009\u5e93\u7684\u6587\u6863\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u53ef\u4ee5\u52a0\u901fPython\u754c\u9762\u5f00\u53d1\u7684\u5de5\u5177\u6216\u6846\u67b6\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u8bb8\u591a\u5de5\u5177\u548c\u6846\u67b6\u53ef\u4ee5\u52a0\u901fPython\u754c\u9762\u7684\u5f00\u53d1\u3002\u4f8b\u5982\uff0c\u4f7f\u7528PyInstaller\u6216cx_Freeze\u53ef\u4ee5\u5c06Python\u5e94\u7528\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u65b9\u4fbf\u5206\u53d1\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u50cfFlask\u6216Django\u8fd9\u6837\u7684Web\u6846\u67b6\uff0c\u7ed3\u5408\u524d\u7aef\u6280\u672f\uff0c\u80fd\u591f\u521b\u5efa\u5177\u6709\u73b0\u4ee3\u5316\u7528\u6237\u754c\u9762\u7684Web\u5e94\u7528\u7a0b\u5e8f\u3002\u8fd9\u4e9b\u5de5\u5177\u548c\u6846\u67b6\u4e0d\u4ec5\u63d0\u5347\u4e86\u5f00\u53d1\u6548\u7387\uff0c\u8fd8\u53ef\u4ee5\u589e\u5f3a\u5e94\u7528\u7684\u529f\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5feb\u901f\u5f00\u53d1\u754c\u9762\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528GUI\u5e93\u3001\u4f7f\u7528\u62d6\u653e\u754c\u9762\u8bbe\u8ba1\u5de5\u5177\u3001\u638c\u63e1\u57fa\u672c\u7684\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b\u3001\u9009\u62e9\u5408\u9002\u7684\u6846\u67b6\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1150989,"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\/1150984"}],"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=1150984"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1150984\/revisions"}],"predecessor-version":[{"id":1150990,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1150984\/revisions\/1150990"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1150989"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1150984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1150984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1150984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}