Home > Back-end >  ttk.Labelframe color issue
ttk.Labelframe color issue

Time:12-25

I am coding a bit of a GUI using tkinter in Python, using ttk items such as the notebook, treeview, and labelframes (and a lot of other things too) everything I was doing looked great, until I changed the color from the "system color" of beige, to white.

Here's an (updated!) iteration of the code (minimum reproducible example, showing just the problem, and not much else!):

import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import font

window = Tk()

s=ttk.Style()

window.title("test window")
window.geometry("405x505 0 0")
section_1 = Frame(window, width = 405, height = 505)
section_1.place(x=0, y=0)

tabControl_1 = ttk.Notebook(section_1, height = 470, width = 395) #395
tab1 = ttk.Frame(tabControl_1, style='new.TFrame')
tab2 = ttk.Frame(tabControl_1)

tabControl_1.add(tab1, text='Devices')
tabControl_1.add(tab2, text='Tab 2')
tabControl_1.place(y=5,x=5)

label = ttk.Label(text="Detected devices will appear here", style="my.TLabel")

lf = ttk.LabelFrame(tab1, labelwidget=label, labelanchor='nw')
lf.pack()
lf.place(x=20,y=20)

treeview_frame = Frame(lf, width = 301, height = 201, background="white")
treeview_frame.grid(row=0, column=0, columnspan=3)
treeview_frame.grid_propagate(False)

s.configure('my.TLabel', background='white', font=('Segoe UI', 9))
s.configure('new.TFrame', background ='white')
window.resizable(False,False)
window.mainloop()

Here's an (updated) screenshot of what that looks like, visually: https://imgur.com/a/oMXBSoz

Essentially, my question is, what is the simplest and/or best way to get all of those patchy bits around the labelframe to be white?

If someone could explain what I'm missing here, logic wise, I would much appreciate it.

UPDATE:

Whilst we were kinda "getting there" slowly in the comments, it seems that Derek has gotten the simple non-destructive solution for this, with his answer.

The lesson here is that if your labelframe references a label, you can change styling for both the label and the labelframe, and this has an effect on the output.

Working from Derek's answer, I was able to get this result:

https://imgur.com/a/1vJqOUV

..Which is perfect, essentially.

Every time I come onto here I get made to feel like I am really stupid (entirely though my own fault, of course), thanks for the help to those that chipped in.

CodePudding user response:

Change background color of ttk.Labelframe like this.

s=ttk.Style()

s.theme_use("default")
s.configure("TLabelframe", background = "white")
  • Related