7

I have designed a GUI using python tkinter. And now I want to set style for Checkbutton and Labelframe, such as the font, the color .etc I have read some answers on the topics of tkinter style, and I have used the following method to set style for both Checkbutton and Labelframe. But they don't actually work.

Root = tkinter.Tk()
ttk.Style().configure('Font.TLabelframe', font="15", foreground = "red")
LabelFrame = ttk.Labelframe(Root, text = "Test", style = "Font.TLabelframe")
LabelFrame .pack( anchor = "w", ipadx = 10, ipady = 5, padx = 10, pady = 0, side = "top")

Can you tell me the reasons, or do you have some other valid methods? Thank you very much!

Mark Jie
  • 73
  • 1
  • 1
  • 3

3 Answers3

18

You need to configure the Label sub-component:

from tkinter import *
from tkinter import ttk

root = Tk()

s = ttk.Style()

s.configure('Red.TLabelframe.Label', font=('courier', 15, 'bold'))
s.configure('Red.TLabelframe.Label', foreground ='red')
s.configure('Red.TLabelframe.Label', background='blue')
lf = ttk.LabelFrame(root, text = "Test", style = "Red.TLabelframe")
lf.pack( anchor = "w", ipadx = 10, ipady = 5, padx = 10,
                  pady = 0, side = "top")
Frame(lf, width=100, height=100, bg='black').pack()
print(s.lookup('Red.TLabelframe.Label', 'font'))
root.mainloop()
Oblivion
  • 1,669
  • 14
  • 14
6

As the accepted answer didn't really help me when I wanted to do a simple changing of weight of a ttk.LabelFrame font (if you do it like recommended, you end up with a misplaced label), I'll provide what worked for me.

You have to use labelwidget option argument of ttk.LabelFrame first preparing a seperate ttk.Label that you style earlier accordingly. Important: using labelwidget means you don't use the usual text option argument for your ttk.LabelFrame (just do it in the label).

# changing a labelframe font's weight to bold
root = Tk()
style = ttk.Style()
style.configure("Bold.TLabel", font=("TkDefaultFont", 9, "bold"))
label = ttk.Label(text="Foo", style="Bold.TLabel")
lf = ttk.LabelFrame(root, labelwidget=label)
z33k
  • 3,280
  • 6
  • 24
  • 38
0

For completeness sake, I was looking how to change border color style (specifically color) of ttk.LabelFrame and finally found how it worked so wanted to post on the posts that I came across while searching.

I create global styles for my ttk widgets, there are ways to apply this to single widgets as well.

style = ttk.Style()
style.theme_create('style', parent='alt', 
                        settings = {'TLabelframe': {'configure': 
                                        {'background': 'black',
                                        'relief': 'solid', # has to be 'solid' to color 
                                        'bordercolor': 'orange',
                                        'borderwidth': 1}},
                                    'TLabelframe.Label': {'configure': 
                                        {'foreground': 'green',
                                        'background': 'black'}}})
style.theme_use('style')
Marro
  • 73
  • 8