Question

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?

Was it helpful?

Solution

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

Mac and Linux default to black, while Windows defaults to blue.

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()

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top