【发布时间】:2022-12-08 08:39:34
【问题描述】:
我正在尝试更新文本标签的背景颜色。为此,我使用了 tkinter 的 ttk 模块。
出于某种原因,它不想执行config.xx(style="xx.TLabel。
from tkinter import *
from tkinter import ttk
win = Tk()
win.geometry("1200x800") #1024*600
s = ttk.Style(win)
s.configure("CustomGrey.TLabel", background="#4D4D4D", foreground="white")
s.configure("CustomGreen.TLabel", background="#97D077", foreground="white")
s.configure("CustomYellow.TLabel", background="#FFD966", foreground="white")
s.configure("CustomRed.TLabel", background="#FF6666", foreground="white")
s.configure("CustomRed.TLabel", background="#FF6666", foreground="white", font=('Time New Roman', 60), anchor= "c")
def updateLabelColor(color):
if color == "Green": battery_lab.config(style="CustomGreen.TLabel")
elif color == "Yellow": battery_lab.config(style="CustomYellow.TLabel")
elif color == "Red": battery_lab.config(style="CustomRed.TLabel")
updateLabelColor("Green")
目标是文本可以在程序中改变颜色。通过 tk 或 ttk 标签完成并不重要。
有谁知道该怎么办?
【问题讨论】:
-
如果我为 minimal reproducible example 添加必要的代码,它就可以正常工作。您是否有可能发现自己陷入了使用通配符导入的困境tkinter.标签代替ttk.标签?
-
@Thingamabobs。我们谈论 ttk 不要
-
@约斯特。如果颜色==“绿色”:battery_lab.config(style =“CustomGreen.TLabel”)NameError:名称'battery_lab'未定义
-
@toyotaSupra op 在他们的问题中谈论了这两个问题,最重要的是 OP 缺少包含标签。所以你要么在你的陈述中猜测要么希望,但它与这里所说的相去甚远。
-
@toyotaSupra 您只需将标签包含为 ttk.Label 即可。不需要调试,只缺少两行代码。 OP 需要澄清他们实际需要帮助的内容。
标签: tkinter ttk tkinter-text ttkwidgets tkinter-label