如何使用 tkinter.Label 更改文本颜色

T.S*_*mer 25 tkinter python-3.x

我正在尝试构建我的第一个 GUI 程序,并且想知道由谁来更改标签文本颜色?例如,将其更改为“红色”

import tkinter as tk

root = tk.Tk()

label = tk.Label(root, text="what's my favorite video?", pady=10, padx=10, font=10,)
label.pack()
click_here = tk.Button(root, text="click here to find out", padx = 10, pady = 5)
click_here.pack()

root.mainloop()
Run Code Online (Sandbox Code Playgroud)

多谢 :-)

P S*_*nki 17

您可以使用可选参数bgfg(请注意,您可能需要使用不同的选项,如在本答案中highlightbackground所述的 MacOS 系统上) - 我认为这是MacOS 上的一个已知问题。tk.Button

import tkinter as tk

root = tk.Tk()

# bg is to change background, fg is to change foreground (technically the text color)
label = tk.Label(root, text="what's my favorite video?",
                 bg='#fff', fg='#f00', pady=10, padx=10, font=10) # You can use use color names instead of color codes.
label.pack()
click_here = tk.Button(root, text="click here to find out",
                       bg='#000', fg='#ff0', padx = 10, pady = 5)
click_here.pack()

root.mainloop()
Run Code Online (Sandbox Code Playgroud)

我添加这个作为答案的唯一原因是因为我在 SO 上为某人写的类似问题的最后一个答案并不能仅仅因为他们使用的是 Mac。如果您使用的是 Windows 计算机,那就没问题。