{"id":1040769,"date":"2024-12-31T12:43:39","date_gmt":"2024-12-31T04:43:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1040769.html"},"modified":"2024-12-31T12:43:41","modified_gmt":"2024-12-31T04:43:41","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%9c%a8gui%e9%87%8c%e5%a1%ab%e5%9b%be%e5%bd%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1040769.html","title":{"rendered":"Python\u4e2d\u5982\u4f55\u5728gui\u91cc\u586b\u56fe\u5f62"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/f2bb378a-478d-44e0-91de-6d9abf63647f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u4e2d\u5982\u4f55\u5728gui\u91cc\u586b\u56fe\u5f62\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728GUI\uff08\u56fe\u5f62\u7528\u6237\u754c\u9762\uff09\u91cc\u586b\u5145\u56fe\u5f62\uff0c\u5305\u62ec\u4f7f\u7528Tkinter\u3001PyQt\u3001Kivy\u7b49\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528Tkinter\u662f\u6700\u5e38\u89c1\u548c\u57fa\u7840\u7684\u65b9\u6cd5\u3002\u901a\u8fc7Tkinter\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u56fe\u5f62\u754c\u9762\u5e76\u5728\u5176\u4e2d\u7ed8\u5236\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Tkinter\u5728GUI\u4e2d\u586b\u5145\u56fe\u5f62\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p><strong>Tkinter\u7684\u753b\u5e03\u7ec4\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>Tkinter\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2aGUI\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3aCanvas\u7684\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\uff0c\u5982\u7ebf\u6761\u3001\u77e9\u5f62\u3001\u692d\u5706\u3001\u591a\u8fb9\u5f62\u7b49\u3002\u4f7f\u7528Canvas\u7ec4\u4ef6\uff0c\u60a8\u53ef\u4ee5\u5728Tkinter\u7a97\u53e3\u4e2d\u521b\u5efa\u548c\u7ba1\u7406\u56fe\u5f62\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6838\u5fc3\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><p><strong>1\u3001\u5b89\u88c5\u548c\u5bfc\u5165Tkinter<\/strong><\/p>\n<p>Tkinter\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u56e0\u6b64\u901a\u5e38\u4e0d\u9700\u8981\u5355\u72ec\u5b89\u88c5\u3002\u5982\u679cTkinter\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install python3-tk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165Tkinter\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2\u3001\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2aTkinter\u4e3b\u7a97\u53e3\uff0c\u8fd9\u662f\u6240\u6709Tkinter\u5e94\u7528\u7a0b\u5e8f\u7684\u57fa\u7840\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">root = tk.Tk()<\/p>\n<p>root.title(&quot;Tkinter Canvas Example&quot;)<\/p>\n<p>root.geometry(&quot;500x500&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3\u3001\u521b\u5efaCanvas\u7ec4\u4ef6<\/strong><\/p>\n<p>\u5728\u4e3b\u7a97\u53e3\u4e2d\u521b\u5efa\u4e00\u4e2aCanvas\u7ec4\u4ef6\uff0c\u7528\u4e8e\u7ed8\u5236\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">canvas = tk.Canvas(root, width=400, height=400, bg=&#39;white&#39;)<\/p>\n<p>canvas.pack()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4\u3001\u7ed8\u5236\u56fe\u5f62<\/strong><\/p>\n<p>\u4f7f\u7528Canvas\u7ec4\u4ef6\u63d0\u4f9b\u7684\u5404\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Canvas\u4e0a\u7ed8\u5236\u4e0d\u540c\u7c7b\u578b\u7684\u56fe\u5f62\u3002\u4f8b\u5982\uff0c\u7ed8\u5236\u77e9\u5f62\u3001\u692d\u5706\u3001\u7ebf\u6761\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ed8\u5236\u77e9\u5f62<\/p>\n<p>canvas.create_rectangle(50, 50, 150, 150, fill=&#39;blue&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u692d\u5706<\/strong><\/h2>\n<p>canvas.create_oval(200, 50, 300, 150, fill=&#39;red&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u7ebf\u6761<\/strong><\/h2>\n<p>canvas.create_line(50, 200, 150, 300, fill=&#39;green&#39;, width=3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>5\u3001\u8fd0\u884c\u4e3b\u5faa\u73af<\/strong><\/p>\n<p>\u6700\u540e\uff0c\u8fd0\u884cTkinter\u7684\u4e3b\u5faa\u73af\uff0c\u4f7f\u7a97\u53e3\u4fdd\u6301\u6253\u5f00\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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><p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Tkinter\u5728GUI\u4e2d\u586b\u5145\u56fe\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Tkinter Canvas Example&quot;)<\/p>\n<p>root.geometry(&quot;500x500&quot;)<\/p>\n<h2><strong>\u521b\u5efaCanvas\u7ec4\u4ef6<\/strong><\/h2>\n<p>canvas = tk.Canvas(root, width=400, height=400, bg=&#39;white&#39;)<\/p>\n<p>canvas.pack()<\/p>\n<h2><strong>\u7ed8\u5236\u77e9\u5f62<\/strong><\/h2>\n<p>canvas.create_rectangle(50, 50, 150, 150, fill=&#39;blue&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u692d\u5706<\/strong><\/h2>\n<p>canvas.create_oval(200, 50, 300, 150, fill=&#39;red&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u7ebf\u6761<\/strong><\/h2>\n<p>canvas.create_line(50, 200, 150, 300, fill=&#39;green&#39;, width=3)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>PyQt\u7684QGraphicsView<\/strong><\/p>\n<\/p>\n<p><p>PyQt\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684Python GUI\u5e93\uff0c\u5b83\u57fa\u4e8eQt\u6846\u67b6\u3002\u4f7f\u7528PyQt\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u590d\u6742\u548c\u7f8e\u89c2\u7684\u56fe\u5f62\u754c\u9762\u3002PyQt\u63d0\u4f9b\u4e86QGraphicsView\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u548c\u7ba1\u7406\u56fe\u5f62\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PyQt\u5728GUI\u4e2d\u586b\u5145\u56fe\u5f62\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><p><strong>1\u3001\u5b89\u88c5\u548c\u5bfc\u5165PyQt<\/strong><\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5PyQt\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyQt5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165PyQt\u76f8\u5173\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsView, QGraphicsScene, QGraphicsRectItem, QGraphicsEllipseItem, QGraphicsLineItem<\/p>\n<p>from PyQt5.QtGui import QPen, QColor<\/p>\n<p>from PyQt5.QtCore import Qt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2\u3001\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2aPyQt\u4e3b\u7a97\u53e3\uff0c\u5e76\u8bbe\u7f6e\u7a97\u53e3\u6807\u9898\u548c\u5c3a\u5bf8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MainWindow(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&quot;PyQt GraphicsView Example&quot;)<\/p>\n<p>        self.setGeometry(100, 100, 500, 500)<\/p>\n<p>        # \u521b\u5efaQGraphicsView\u548cQGraphicsScene<\/p>\n<p>        self.view = QGraphicsView(self)<\/p>\n<p>        self.view.setGeometry(50, 50, 400, 400)<\/p>\n<p>        self.scene = QGraphicsScene()<\/p>\n<p>        self.view.setScene(self.scene)<\/p>\n<p>        # \u6dfb\u52a0\u56fe\u5f62\u5bf9\u8c61\u5230\u573a\u666f<\/p>\n<p>        self.add_graphics()<\/p>\n<p>    def add_graphics(self):<\/p>\n<p>        # \u7ed8\u5236\u77e9\u5f62<\/p>\n<p>        rect = QGraphicsRectItem(50, 50, 100, 100)<\/p>\n<p>        rect.setBrush(QColor(&quot;blue&quot;))<\/p>\n<p>        self.scene.addItem(rect)<\/p>\n<p>        # \u7ed8\u5236\u692d\u5706<\/p>\n<p>        ellipse = QGraphicsEllipseItem(200, 50, 100, 100)<\/p>\n<p>        ellipse.setBrush(QColor(&quot;red&quot;))<\/p>\n<p>        self.scene.addItem(ellipse)<\/p>\n<p>        # \u7ed8\u5236\u7ebf\u6761<\/p>\n<p>        pen = QPen(QColor(&quot;green&quot;))<\/p>\n<p>        pen.setWidth(3)<\/p>\n<p>        line = QGraphicsLineItem(50, 200, 150, 300)<\/p>\n<p>        line.setPen(pen)<\/p>\n<p>        self.scene.addItem(line)<\/p>\n<h2><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u5b9e\u4f8b<\/strong><\/h2>\n<p>app = QApplication([])<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3\u5b9e\u4f8b<\/strong><\/h2>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<h2><strong>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>app.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528PyQt\u5728GUI\u4e2d\u586b\u5145\u56fe\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsView, QGraphicsScene, QGraphicsRectItem, QGraphicsEllipseItem, QGraphicsLineItem<\/p>\n<p>from PyQt5.QtGui import QPen, QColor<\/p>\n<p>from PyQt5.QtCore import Qt<\/p>\n<p>class MainWindow(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&quot;PyQt GraphicsView Example&quot;)<\/p>\n<p>        self.setGeometry(100, 100, 500, 500)<\/p>\n<p>        # \u521b\u5efaQGraphicsView\u548cQGraphicsScene<\/p>\n<p>        self.view = QGraphicsView(self)<\/p>\n<p>        self.view.setGeometry(50, 50, 400, 400)<\/p>\n<p>        self.scene = QGraphicsScene()<\/p>\n<p>        self.view.setScene(self.scene)<\/p>\n<p>        # \u6dfb\u52a0\u56fe\u5f62\u5bf9\u8c61\u5230\u573a\u666f<\/p>\n<p>        self.add_graphics()<\/p>\n<p>    def add_graphics(self):<\/p>\n<p>        # \u7ed8\u5236\u77e9\u5f62<\/p>\n<p>        rect = QGraphicsRectItem(50, 50, 100, 100)<\/p>\n<p>        rect.setBrush(QColor(&quot;blue&quot;))<\/p>\n<p>        self.scene.addItem(rect)<\/p>\n<p>        # \u7ed8\u5236\u692d\u5706<\/p>\n<p>        ellipse = QGraphicsEllipseItem(200, 50, 100, 100)<\/p>\n<p>        ellipse.setBrush(QColor(&quot;red&quot;))<\/p>\n<p>        self.scene.addItem(ellipse)<\/p>\n<p>        # \u7ed8\u5236\u7ebf\u6761<\/p>\n<p>        pen = QPen(QColor(&quot;green&quot;))<\/p>\n<p>        pen.setWidth(3)<\/p>\n<p>        line = QGraphicsLineItem(50, 200, 150, 300)<\/p>\n<p>        line.setPen(pen)<\/p>\n<p>        self.scene.addItem(line)<\/p>\n<h2><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u5b9e\u4f8b<\/strong><\/h2>\n<p>app = QApplication([])<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3\u5b9e\u4f8b<\/strong><\/h2>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<h2><strong>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>app.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>Kivy\u7684Canvas<\/strong><\/p>\n<\/p>\n<p><p>Kivy\u662f\u4e00\u4e2a\u5f00\u6e90\u7684Python\u5e93\uff0c\u7528\u4e8e\u5feb\u901f\u5f00\u53d1\u8de8\u5e73\u53f0\u7684\u5e94\u7528\u7a0b\u5e8f\u3002Kivy\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684Canvas\u7c7b\uff0c\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u548c\u7ba1\u7406\u56fe\u5f62\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Kivy\u5728GUI\u4e2d\u586b\u5145\u56fe\u5f62\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><p><strong>1\u3001\u5b89\u88c5\u548c\u5bfc\u5165Kivy<\/strong><\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5Kivy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install kivy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165Kivy\u76f8\u5173\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.widget import Widget<\/p>\n<p>from kivy.graphics import Rectangle, Ellipse, Line, Color<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2\u3001\u521b\u5efa\u81ea\u5b9a\u4e49\u5c0f\u90e8\u4ef6<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5c0f\u90e8\u4ef6\u7c7b\uff0c\u5e76\u5728\u5176\u4e2d\u4f7f\u7528Canvas\u7ed8\u5236\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyWidget(Widget):<\/p>\n<p>    def __init__(self, kwargs):<\/p>\n<p>        super().__init__(kwargs)<\/p>\n<p>        with self.canvas:<\/p>\n<p>            # \u7ed8\u5236\u77e9\u5f62<\/p>\n<p>            Color(0, 0, 1, 1)  # \u8bbe\u7f6e\u989c\u8272\u4e3a\u84dd\u8272<\/p>\n<p>            Rectangle(pos=(50, 50), size=(100, 100))<\/p>\n<p>            # \u7ed8\u5236\u692d\u5706<\/p>\n<p>            Color(1, 0, 0, 1)  # \u8bbe\u7f6e\u989c\u8272\u4e3a\u7ea2\u8272<\/p>\n<p>            Ellipse(pos=(200, 50), size=(100, 100))<\/p>\n<p>            # \u7ed8\u5236\u7ebf\u6761<\/p>\n<p>            Color(0, 1, 0, 1)  # \u8bbe\u7f6e\u989c\u8272\u4e3a\u7eff\u8272<\/p>\n<p>            Line(points=[50, 200, 150, 300], width=3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3\u3001\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u7c7b<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2aKivy\u5e94\u7528\u7a0b\u5e8f\u7c7b\uff0c\u5e76\u5728build\u65b9\u6cd5\u4e2d\u8fd4\u56de\u81ea\u5b9a\u4e49\u5c0f\u90e8\u4ef6\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        return MyWidget()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4\u3001\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f<\/strong><\/p>\n<p>\u6700\u540e\uff0c\u8fd0\u884cKivy\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><p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Kivy\u5728GUI\u4e2d\u586b\u5145\u56fe\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.widget import Widget<\/p>\n<p>from kivy.graphics import Rectangle, Ellipse, Line, Color<\/p>\n<p>class MyWidget(Widget):<\/p>\n<p>    def __init__(self, kwargs):<\/p>\n<p>        super().__init__(kwargs)<\/p>\n<p>        with self.canvas:<\/p>\n<p>            # \u7ed8\u5236\u77e9\u5f62<\/p>\n<p>            Color(0, 0, 1, 1)  # \u8bbe\u7f6e\u989c\u8272\u4e3a\u84dd\u8272<\/p>\n<p>            Rectangle(pos=(50, 50), size=(100, 100))<\/p>\n<p>            # \u7ed8\u5236\u692d\u5706<\/p>\n<p>            Color(1, 0, 0, 1)  # \u8bbe\u7f6e\u989c\u8272\u4e3a\u7ea2\u8272<\/p>\n<p>            Ellipse(pos=(200, 50), size=(100, 100))<\/p>\n<p>            # \u7ed8\u5236\u7ebf\u6761<\/p>\n<p>            Color(0, 1, 0, 1)  # \u8bbe\u7f6e\u989c\u8272\u4e3a\u7eff\u8272<\/p>\n<p>            Line(points=[50, 200, 150, 300], width=3)<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        return MyWidget()<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u5728GUI\u4e2d\u586b\u5145\u56fe\u5f62\uff0c\u5305\u62ecTkinter\u3001PyQt\u548cKivy\u3002\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u529f\u80fd\u548c\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>Tkinter<\/strong>\uff1aPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u6613\u4e8e\u5b66\u4e60\u548c\u4f7f\u7528\uff0c\u9002\u5408\u7b80\u5355\u7684\u56fe\u5f62\u548c\u754c\u9762\u3002<\/li>\n<li><strong>PyQt<\/strong>\uff1a\u57fa\u4e8eQt\u6846\u67b6\uff0c\u63d0\u4f9b\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7f8e\u89c2\u7684\u754c\u9762\uff0c\u9002\u5408\u590d\u6742\u548c\u4e13\u4e1a\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<li><strong>Kivy<\/strong>\uff1a\u5f00\u6e90\u3001\u8de8\u5e73\u53f0\uff0c\u9002\u5408\u5feb\u901f\u5f00\u53d1\u591a\u5e73\u53f0\u5e94\u7528\u7a0b\u5e8f\uff0c\u7279\u522b\u9002\u7528\u4e8e\u89e6\u6478\u5c4f\u8bbe\u5907\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\u4ee3\u7801\uff0c\u60a8\u53ef\u4ee5\u4e86\u89e3\u5982\u4f55\u5728\u4e0d\u540c\u7684Python GUI\u5e93\u4e2d\u7ed8\u5236\u56fe\u5f62\uff0c\u5e76\u6839\u636e\u60a8\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u8fdb\u884c\u5f00\u53d1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7684GUI\u4e2d\u7ed8\u5236\u57fa\u672c\u5f62\u72b6\uff1f<\/strong><br \/>\u5728Python\u7684GUI\u4e2d\uff0c\u4f7f\u7528\u5e93\u5982Tkinter\u6216PyQt\u53ef\u4ee5\u8f7b\u677e\u7ed8\u5236\u57fa\u672c\u5f62\u72b6\u3002\u5bf9\u4e8eTkinter\uff0c\u53ef\u4ee5\u4f7f\u7528Canvas\u7ec4\u4ef6\uff0c\u5229\u7528\u5176\u63d0\u4f9b\u7684\u7ed8\u56fe\u65b9\u6cd5\uff0c\u5982create_rectangle()\u3001create_oval()\u7b49\uff0c\u6765\u7ed8\u5236\u77e9\u5f62\u3001\u5706\u5f62\u7b49\u56fe\u5f62\u3002\u5bf9\u4e8ePyQt\uff0cQPainter\u7c7b\u5219\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u7a97\u53e3\u4e2d\u7ed8\u5236\u56fe\u5f62\uff0c\u901a\u8fc7\u91cd\u5199paintEvent()\u65b9\u6cd5\u5b9e\u73b0\u56fe\u5f62\u7684\u81ea\u5b9a\u4e49\u7ed8\u5236\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u5728Python\u4e2d\u5b9e\u73b0\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\uff1f<\/strong><br \/>Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0c\u5176\u4e2dTkinter\u662f\u5185\u7f6e\u7684\uff0c\u9002\u5408\u5feb\u901f\u5f00\u53d1\u5c0f\u578b\u5e94\u7528\uff1bPyQt\u548cPySide\u63d0\u4f9b\u66f4\u591a\u529f\u80fd\u548c\u66f4\u590d\u6742\u7684\u754c\u9762\u8bbe\u8ba1\uff0c\u9002\u5408\u6784\u5efa\u5927\u578b\u5e94\u7528\uff1bKivy\u5219\u662f\u4e00\u4e2a\u5f00\u6e90\u5e93\uff0c\u9002\u7528\u4e8e\u591a\u5e73\u53f0\u7684\u5e94\u7528\u5f00\u53d1\uff0c\u5c24\u5176\u662f\u79fb\u52a8\u8bbe\u5907\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53d6\u51b3\u4e8e\u9879\u76ee\u9700\u6c42\u548c\u5f00\u53d1\u7ecf\u9a8c\u3002<\/p>\n<p><strong>\u5728Python GUI\u4e2d\u5982\u4f55\u5b9e\u73b0\u52a8\u6001\u66f4\u65b0\u7684\u56fe\u5f62\uff1f<\/strong><br \/>\u52a8\u6001\u66f4\u65b0\u56fe\u5f62\u53ef\u4ee5\u901a\u8fc7\u5b9a\u65f6\u5668\u6216\u4e8b\u4ef6\u9a71\u52a8\u7684\u65b9\u6cd5\u5b9e\u73b0\u3002\u5728Tkinter\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528after()\u65b9\u6cd5\u5b9a\u671f\u8c03\u7528\u66f4\u65b0\u51fd\u6570\uff1b\u800c\u5728PyQt\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528QTimer\u7c7b\u6765\u5b9e\u73b0\u5b9a\u65f6\u5668\u529f\u80fd\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5141\u8bb8\u5f00\u53d1\u8005\u6839\u636e\u7528\u6237\u8f93\u5165\u6216\u5176\u4ed6\u6761\u4ef6\u5b9e\u65f6\u66f4\u65b0\u56fe\u5f62\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u4e3a\u4e92\u52a8\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728GUI\uff08\u56fe\u5f62\u7528\u6237\u754c\u9762\uff09\u91cc\u586b\u5145\u56fe\u5f62\uff0c\u5305\u62ec\u4f7f\u7528Tkinter\u3001PyQt\u3001Kiv [&hellip;]","protected":false},"author":3,"featured_media":1040779,"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\/1040769"}],"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=1040769"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1040769\/revisions"}],"predecessor-version":[{"id":1040781,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1040769\/revisions\/1040781"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1040779"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1040769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1040769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1040769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}