How do I change ttk.LabelFrame's blue header label to black in python's tkinter 8.5

三世轮回 提交于 2019-12-10 21:31:00

问题


I'm using tkinter 8.5 and python 3.3 on a Windows 7 machine.

The code below renders the Labelframe's header in a blue font color.

from tkinter import *
from tkinter import ttk

root = Tk()

lf = ttk.LabelFrame(root, text="Why is this blue?")
lf.pack()

label = ttk.Label(lf, text="label")
label.pack()

I tried fixing this by adding a ttk.Style(), but got an unexpected display:

from tkinter import *
from tkinter import ttk

root = Tk()

s = ttk.Style()
s.configure('TLabelframe.Label', font='arial 14 bold')

lf = ttk.LabelFrame(root, text="Now it's black, but w/ a bizarre display"
                    " and no etched frame.", style='TLabelframe.Label')
lf.pack()

label = ttk.Label(lf, text="label")
label.pack()

Is there a way to get a ttk.LabelFrame header to appear black in color w/o weird side effects?


回答1:


It appears that Windows defaults ttk.Labelframe headers to this blue color. Not sure why.

I found a solution by creating a ttk.Label and passing that as ttk.Labelframe's labelwidget argument. This might be more of a workaround, though. In any event, the code below displays the header text in black on my Windows 7 machine.

from tkinter import *
from tkinter import ttk

root = Tk()

l = ttk.Label(text="Not blue anymore")

lf = ttk.Labelframe(root, labelwidget=l)
lf.pack()

label = ttk.Label(lf, text="label")
label.pack()

root.mainloop()



回答2:


This can also be changed using the foreground property tk.LabelFrame(window, foreground='red')




回答3:


You just need to remove the style='TLabelframe.Label' from the ttk.LabelFrame options.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

s = ttk.Style()
s.configure('TLabelframe.Label', font='arial 14 bold')

lf = ttk.LabelFrame(root, text="Now it's black, but w/ a bizarre display"
                    " and no etched frame.")
lf.pack()

label = ttk.Label(lf, text="label")
label.pack()
root.mainloop()

I am using Python 3.7.3 and tkinter 8.6.



来源:https://stackoverflow.com/questions/25002373/how-do-i-change-ttk-labelframes-blue-header-label-to-black-in-pythons-tkinter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!