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

新手上路,请多包涵

我正在尝试构建我的第一个 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()

非常感谢 :-)

原文由 T.Stimer 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
1 个回答

您可以使用可选参数 bgfg (请注意,您可能需要使用不同的选项,如 highlightbackground --- 我认为这是 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()

我将此添加为答案的唯一原因是因为我为 SO 上的某个人写的类似问题的 最后一个答案 只是因为他们使用的是 Mac 而不起作用。如果您使用的是 Windows 计算机,则没有问题。

原文由 P S Solanki 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进