Nico44044
Nico44044

Reputation: 411

Tkinter Labelframe strange behavior : border color disappear when applying text in label

My label frames can only handle the highlightbackground and highlightcolor options as long as I don't put some text in the text option.

Here is my code, try by youserself, if you add some text in the LabelFrame the border disappear ...

import tkinter as tk

def change_bg(widget):
    widget['highlightbackground'] = 'red'

if __name__ == '__main__':
    root = tk.Tk()

    frame = tk.LabelFrame(root, text="", height=100, width=100,
        highlightbackground="green", highlightcolor="green", highlightthickness=1, bd= 0)
    button = tk.Button(root, text="Paint")
    button['command'] = lambda wgt=frame : change_bg(wgt)

    frame.pack()
    button.pack()

    root.mainloop()

Upvotes: 1

Views: 495

Answers (2)

Nico44044
Nico44044

Reputation: 411

I finally managed to find a workaround on Mac using the clam theme :

This one below works for me so far and allows to color multiple Labelframe with different color including Label and bordercolor.

from tkinter import *
from tkinter import ttk


    root = Tk()
    
    edit_labelframe_color_style = ttk.Style()
    edit_labelframe_color_style.theme_use('clam') #only theme to handle bordercolor for labelframe
    
    edit_labelframe_color_style.configure('red.TLabelframe', bordercolor='red',background='white')
    edit_labelframe_color_style.configure('red.TLabelframe.Label', foreground='red',background='white')
    
    edit_labelframe_color_style.configure('green.TLabelframe', bordercolor='green',background='white')
    edit_labelframe_color_style.configure('green.TLabelframe.Label', foreground='green',background='white')
    
    labelframe = ttk.LabelFrame(root, text="Group", style='red.TLabelframe',height=200, width=400)
    labelframe.grid(padx=20, pady=20)
    labelframe.grid_propagate(0)
    
    labelframe1 = ttk.LabelFrame(root, text="Group", style='green.TLabelframe',height=200, width=400)
    labelframe1.grid(padx=20, pady=200)
    labelframe1.grid_propagate(0)
    
    
    
    root.mainloop()

Upvotes: 0

astqx
astqx

Reputation: 2096

I was able to reproduce this when tested in Mac, but it works just fine on Windows. I believe it's one of those things that Mac simply doesn't support. You can try nesting the LabelFrame inside a Frame with a border and change its background color, something like this

import tkinter as tk

def change_bg(widget):
    widget['bg'] = 'red'

if __name__ == '__main__':
    root = tk.Tk()

    base_frame=tk.Frame(root,bd=1,bg='green')
    base_frame.pack()
    frame = tk.LabelFrame(base_frame, text="asdas", height=100, width=100,bd= 0)
    button = tk.Button(root, text="Paint")
    button['command'] = lambda wgt=base_frame : change_bg(wgt)

    frame.pack()
    button.pack()

    root.mainloop()

Upvotes: 1

Related Questions