{"id":1188147,"date":"2025-01-15T20:13:52","date_gmt":"2025-01-15T12:13:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1188147.html"},"modified":"2025-01-15T20:13:54","modified_gmt":"2025-01-15T12:13:54","slug":"python%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%aelabel%e7%9a%84%e9%a2%9c%e8%89%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1188147.html","title":{"rendered":"Python\u5982\u4f55\u8bbe\u7f6elabel\u7684\u989c\u8272"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25140548\/1e6cdeb8-bade-4ac3-8a08-95d543d09023.webp\" alt=\"Python\u5982\u4f55\u8bbe\u7f6elabel\u7684\u989c\u8272\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8bbe\u7f6elabel\u7684\u989c\u8272\uff0c\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\u3001Matplotlib\u5e93\u3001\u6216\u5176\u4ed6\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\u6765\u5b8c\u6210\u3002<\/strong> \u4f8b\u5982\uff0c\u4f7f\u7528Tkinter\u5e93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>Label<\/code>\u63a7\u4ef6\u7684<code>fg<\/code>\u548c<code>bg<\/code>\u5c5e\u6027\u6765\u8bbe\u7f6e\u524d\u666f\u8272\u548c\u80cc\u666f\u8272\u3002\u4f7f\u7528Matplotlib\u5e93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>set_color<\/code>\u65b9\u6cd5\u6765\u8bbe\u7f6e\u6807\u7b7e\u989c\u8272\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5728\u4e0d\u540c\u7684\u5e93\u4e2d\u8bbe\u7f6elabel\u7684\u989c\u8272\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Tkinter\u5e93\u4e2d\u7684Label\u989c\u8272\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u7a97\u53e3\u5e94\u7528\u7a0b\u5e8f\u3002\u5728Tkinter\u4e2d\uff0c<code>Label<\/code>\u63a7\u4ef6\u7528\u4e8e\u663e\u793a\u6587\u672c\u6216\u56fe\u50cf\u3002\u53ef\u4ee5\u901a\u8fc7<code>fg<\/code>\u548c<code>bg<\/code>\u5c5e\u6027\u8bbe\u7f6e\u6807\u7b7e\u7684\u524d\u666f\u8272\u548c\u80cc\u666f\u8272\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efaTkinter\u7a97\u53e3\u548cLabel\u63a7\u4ef6<\/h4>\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 Label Color Example&quot;)<\/p>\n<h2><strong>\u521b\u5efaLabel\u63a7\u4ef6<\/strong><\/h2>\n<p>label = tk.Label(root, text=&quot;Hello, Tkinter!&quot;, font=(&quot;Helvetica&quot;, 16))<\/p>\n<h2><strong>\u8bbe\u7f6eLabel\u63a7\u4ef6\u7684\u524d\u666f\u8272\u548c\u80cc\u666f\u8272<\/strong><\/h2>\n<p>label.config(fg=&quot;red&quot;, bg=&quot;yellow&quot;)<\/p>\n<h2><strong>\u5c06Label\u63a7\u4ef6\u653e\u7f6e\u5728\u7a97\u53e3\u4e2d<\/strong><\/h2>\n<p>label.pack(pady=20)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u5faa\u73af<\/strong><\/h2>\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<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aTkinter\u7a97\u53e3\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6807\u7b7e\u3002\u901a\u8fc7<code>config<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u5c06\u6807\u7b7e\u7684\u524d\u666f\u8272\u8bbe\u7f6e\u4e3a\u7ea2\u8272\uff08<code>fg=&quot;red&quot;<\/code>\uff09\uff0c\u80cc\u666f\u8272\u8bbe\u7f6e\u4e3a\u9ec4\u8272\uff08<code>bg=&quot;yellow&quot;<\/code>\uff09\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u989c\u8272\u4ee3\u7801\u8bbe\u7f6eLabel\u989c\u8272<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u989c\u8272\u540d\u79f0\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u989c\u8272\u4ee3\u7801\u6765\u8bbe\u7f6e\u6807\u7b7e\u7684\u989c\u8272\u3002\u4f8b\u5982\uff0c\u5c06\u524d\u666f\u8272\u8bbe\u7f6e\u4e3a\u84dd\u8272\uff0c\u80cc\u666f\u8272\u8bbe\u7f6e\u4e3a\u7eff\u8272\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">label.config(fg=&quot;#0000FF&quot;, bg=&quot;#00FF00&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001Matplotlib\u5e93\u4e2d\u7684Label\u989c\u8272\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u5404\u79cd\u56fe\u5f62\u3002\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>set_color<\/code>\u65b9\u6cd5\u6765\u8bbe\u7f6e\u6807\u7b7e\u7684\u989c\u8272\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efaMatplotlib\u56fe\u5f62\u548c\u6807\u7b7e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u548c\u5b50\u56fe<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u6570\u636e<\/strong><\/h2>\n<p>x = [0, 1, 2, 3, 4]<\/p>\n<p>y = [0, 1, 4, 9, 16]<\/p>\n<p>ax.plot(x, y)<\/p>\n<h2><strong>\u8bbe\u7f6ex\u8f74\u548cy\u8f74\u6807\u7b7e\u7684\u989c\u8272<\/strong><\/h2>\n<p>ax.set_xlabel(&#39;X Axis&#39;, color=&#39;blue&#39;, fontsize=14)<\/p>\n<p>ax.set_ylabel(&#39;Y Axis&#39;, color=&#39;green&#39;, fontsize=14)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6807\u9898\u7684\u989c\u8272<\/strong><\/h2>\n<p>ax.set_title(&#39;Matplotlib Label Color Example&#39;, color=&#39;purple&#39;, fontsize=16)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aMatplotlib\u56fe\u5f62\uff0c\u5e76\u7ed8\u5236\u4e86\u4e00\u6761\u66f2\u7ebf\u3002\u901a\u8fc7\u5728<code>set_xlabel<\/code>\u3001<code>set_ylabel<\/code>\u548c<code>set_title<\/code>\u65b9\u6cd5\u4e2d\u6307\u5b9a<code>color<\/code>\u53c2\u6570\uff0c\u6211\u4eec\u5c06x\u8f74\u6807\u7b7e\u3001y\u8f74\u6807\u7b7e\u548c\u6807\u9898\u7684\u989c\u8272\u5206\u522b\u8bbe\u7f6e\u4e3a\u84dd\u8272\u3001\u7eff\u8272\u548c\u7d2b\u8272\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528RGB\u989c\u8272\u8bbe\u7f6eLabel\u989c\u8272<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u989c\u8272\u540d\u79f0\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528RGB\u989c\u8272\u4ee3\u7801\u6765\u8bbe\u7f6e\u6807\u7b7e\u7684\u989c\u8272\u3002\u4f8b\u5982\uff0c\u5c06x\u8f74\u6807\u7b7e\u989c\u8272\u8bbe\u7f6e\u4e3a\u7ea2\u8272\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.set_xlabel(&#39;X Axis&#39;, color=(1, 0, 0), fontsize=14)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5176\u4ed6GUI\u5e93\u4e2d\u7684Label\u989c\u8272\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Tkinter\u548cMatplotlib\uff0c\u8fd8\u6709\u5176\u4ed6GUI\u5e93\u53ef\u4ee5\u7528\u6765\u8bbe\u7f6e\u6807\u7b7e\u989c\u8272\uff0c\u4f8b\u5982PyQt\u548cKivy\u3002\u4e0b\u9762\u662f\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u8bbe\u7f6e\u6807\u7b7e\u989c\u8272\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528PyQt\u8bbe\u7f6eLabel\u989c\u8272<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget<\/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<\/strong><\/h2>\n<p>window = QWidget()<\/p>\n<p>window.setWindowTitle(&#39;PyQt Label Color Example&#39;)<\/p>\n<h2><strong>\u521b\u5efaLabel\u63a7\u4ef6<\/strong><\/h2>\n<p>label = QLabel(&#39;Hello, PyQt!&#39;)<\/p>\n<p>label.setStyleSheet(&#39;color: blue; background-color: yellow; font-size: 16px;&#39;)<\/p>\n<h2><strong>\u5e03\u5c40\u7ba1\u7406<\/strong><\/h2>\n<p>layout = QVBoxLayout()<\/p>\n<p>layout.addWidget(label)<\/p>\n<p>window.setLayout(layout)<\/p>\n<h2><strong>\u663e\u793a\u7a97\u53e3<\/strong><\/h2>\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>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aPyQt\u7a97\u53e3\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6807\u7b7e\u3002\u901a\u8fc7<code>setStyleSheet<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u5c06\u6807\u7b7e\u7684\u524d\u666f\u8272\u8bbe\u7f6e\u4e3a\u84dd\u8272\uff0c\u80cc\u666f\u8272\u8bbe\u7f6e\u4e3a\u9ec4\u8272\uff0c\u5b57\u4f53\u5927\u5c0f\u8bbe\u7f6e\u4e3a16\u50cf\u7d20\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528Kivy\u8bbe\u7f6eLabel\u989c\u8272<\/h4>\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.boxlayout import BoxLayout<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        layout = BoxLayout(padding=10)<\/p>\n<p>        # \u521b\u5efaLabel\u63a7\u4ef6<\/p>\n<p>        label = Label(text=&#39;Hello, Kivy!&#39;,<\/p>\n<p>                      color=(0, 0, 1, 1),  # \u524d\u666f\u8272\u4e3a\u84dd\u8272<\/p>\n<p>                      font_size=&#39;20sp&#39;)<\/p>\n<p>        layout.add_widget(label)<\/p>\n<p>        return layout<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aKivy\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6807\u7b7e\u3002\u901a\u8fc7\u8bbe\u7f6e<code>color<\/code>\u5c5e\u6027\uff0c\u6211\u4eec\u5c06\u6807\u7b7e\u7684\u524d\u666f\u8272\u8bbe\u7f6e\u4e3a\u84dd\u8272\uff0c\u5b57\u4f53\u5927\u5c0f\u8bbe\u7f6e\u4e3a20sp\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8bbe\u7f6e\u6807\u7b7e\u7684\u989c\u8272\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4f7f\u7528\u7684GUI\u5e93\u3002<strong>\u5728Tkinter\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>fg<\/code>\u548c<code>bg<\/code>\u5c5e\u6027\u8bbe\u7f6e\u524d\u666f\u8272\u548c\u80cc\u666f\u8272<\/strong>\uff1b<strong>\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>set_color<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u6807\u7b7e\u989c\u8272<\/strong>\uff1b<strong>\u5728PyQt\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>setStyleSheet<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u6807\u7b7e\u989c\u8272<\/strong>\uff1b<strong>\u5728Kivy\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>color<\/code>\u5c5e\u6027\u8bbe\u7f6e\u6807\u7b7e\u989c\u8272<\/strong>\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u6240\u9700\u7684\u754c\u9762\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u81ea\u5b9a\u4e49\u6807\u7b7e\u7684\u80cc\u666f\u548c\u524d\u666f\u8272\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\u6765\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0c\u5e76\u81ea\u5b9a\u4e49\u6807\u7b7e\u7684\u80cc\u666f\u548c\u524d\u666f\u8272\u3002\u901a\u8fc7\u8bbe\u7f6e<code>bg<\/code>\uff08\u80cc\u666f\u8272\uff09\u548c<code>fg<\/code>\uff08\u524d\u666f\u8272\uff09\u53c2\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u66f4\u6539\u6807\u7b7e\u7684\u989c\u8272\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import tkinter as tk\n\nroot = tk.Tk()\nlabel = tk.Label(root, text=&quot;Hello, World!&quot;, bg=&quot;blue&quot;, fg=&quot;white&quot;)\nlabel.pack()\nroot.mainloop()\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6807\u7b7e\u7684\u80cc\u666f\u8272\u88ab\u8bbe\u7f6e\u4e3a\u84dd\u8272\uff0c\u524d\u666f\u8272\u5219\u4e3a\u767d\u8272\u3002<\/p>\n<p><strong>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u6539\u53d8\u6807\u7b7e\u7684\u989c\u8272\uff1f<\/strong><br \/>\u9664\u4e86Tkinter\uff0cPython\u8fd8\u6709\u591a\u4e2a\u5e93\u53ef\u7528\u4e8e\u521b\u5efa\u7528\u6237\u754c\u9762\u5e76\u8bbe\u7f6e\u6807\u7b7e\u989c\u8272\u3002\u4f8b\u5982\uff0c\u4f7f\u7528PyQt\u6216Kivy\u7b49\u5e93\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6837\u5f0f\u8868\u6216\u5c5e\u6027\u8f7b\u677e\u6539\u53d8\u6807\u7b7e\u7684\u989c\u8272\u3002\u5728PyQt\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>setStyleSheet<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\">label.setStyleSheet(&quot;background-color: blue; color: white;&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u7684\u65b9\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6837\u5f0f\u8bbe\u7f6e\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u6807\u7b7e\u4e2d\u4f7f\u7528\u6e10\u53d8\u8272\uff1f<\/strong><br \/>\u867d\u7136Tkinter\u548cPyQt\u7b49\u5e93\u672c\u8eab\u4e0d\u76f4\u63a5\u652f\u6301\u6e10\u53d8\u8272\uff0c\u4f46\u60a8\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u591a\u4e2a\u6807\u7b7e\u5e76\u901a\u8fc7\u5e03\u5c40\u5c06\u5b83\u4eec\u5e76\u6392\u653e\u7f6e\uff0c\u6216\u8005\u4f7f\u7528\u56fe\u50cf\u4f5c\u4e3a\u6807\u7b7e\u80cc\u666f\u6765\u5b9e\u73b0\u6e10\u53d8\u6548\u679c\u3002\u5728Kivy\u4e2d\uff0c\u4f7f\u7528<code>Canvas<\/code>\u53ef\u4ee5\u8f7b\u677e\u7ed8\u5236\u6e10\u53d8\u80cc\u666f\uff0c\u8fd9\u6837\u53ef\u4ee5\u5e26\u6765\u66f4\u70ab\u9177\u7684\u89c6\u89c9\u6548\u679c\u3002\u901a\u8fc7\u7ed8\u5236\u77e9\u5f62\u5e76\u4e3a\u5176\u8bbe\u7f6e\u4e0d\u540c\u989c\u8272\uff0c\u53ef\u4ee5\u8fbe\u5230\u6e10\u53d8\u6548\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u6807\u7b7e\u4e2d\u52a8\u6001\u6539\u53d8\u989c\u8272\uff1f<\/strong><br \/>\u8981\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u6539\u53d8\u6807\u7b7e\u7684\u989c\u8272\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4e8b\u4ef6\u7ed1\u5b9a\u6216\u5b9a\u65f6\u5668\u3002\u4f8b\u5982\uff0c\u5728Tkinter\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>after<\/code>\u65b9\u6cd5\u6765\u5b9a\u65f6\u66f4\u6539\u6807\u7b7e\u7684\u989c\u8272\uff1a<\/p>\n<pre><code class=\"language-python\">def change_color():\n    label.config(bg=&quot;red&quot;)\n    root.after(1000, lambda: label.config(bg=&quot;blue&quot;))\n\nlabel = tk.Label(root, text=&quot;Hello, World!&quot;, bg=&quot;blue&quot;)\nlabel.pack()\nchange_color()\nroot.mainloop()\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6807\u7b7e\u7684\u80cc\u666f\u989c\u8272\u4f1a\u5728\u6bcf\u79d2\u5185\u5207\u6362\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8bbe\u7f6elabel\u7684\u989c\u8272\uff0c\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\u3001Matplotlib\u5e93\u3001\u6216\u5176\u4ed6\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08G [&hellip;]","protected":false},"author":3,"featured_media":1188156,"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\/1188147"}],"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=1188147"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1188147\/revisions"}],"predecessor-version":[{"id":1188161,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1188147\/revisions\/1188161"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1188156"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1188147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1188147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1188147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}