Search code examples
pythontkinterttk

Unable to change the background of a tkinter tabbed display


I want to make the background of the tabbed frames in this code white but for some reason, everything I have tried just is not working. The background color is currently gray but the background of the individual widgets are white. Ideally, they would all be white. The only solution I found that changed the background to white ended up removing the border of the Button.

enter image description here

from tkinter import *
from tkinter import ttk

window = Tk()

width = 850
height = 450

window.title("Sheltering the Homeless During a Pandemic")
window.geometry(str(width) + "x" + str(height))

# style = ttk.Style()
# style.theme_create('st', settings={
#     ".": {
#         "configure": {
#             "background": "white",
#         }
#     },
#     "TNotebook": {
#         "configure": {
#             "tabmargins": [2, 5, 0, 0],
#         }
#     },
#     "TNotebook.Tab": {
#         "configure": {
#             "padding": [10, 2]
#         },
#         "map": {
#             "background": [("selected", "#D3D3D3")],
#             "expand": [("selected", [1, 1, 1, 0])]
#         }
#     },
# })
# style.theme_use('st')

#Create Tab Control
tab_control = ttk.Notebook(window)
#Tab1
tab1 = ttk.Frame(tab_control)
tab_control.add(tab1, text='National Level')
#Tab2
tab2 = ttk.Frame(tab_control)
tab_control.add(tab2, text='State Level')
tab_control.pack(expand=1, fill="both")

# Define the Input Labels
lbl_people_per_room = Label(tab1, text="People Per Room: ", pady=20)
lbl_people_per_room.grid(row=0, column=0)

lbl_num_employees_per_10_rooms = Label(tab1, text="Employees Per 10 Rooms: ")
lbl_num_employees_per_10_rooms.grid(row=0, column=2)

lbl_min_wage_inflation_percentage = Label(tab1, text="Miniumum Wage Inflation Percentage: ")
lbl_min_wage_inflation_percentage.grid(row=1, column=0)

lbl_nightly_compensation = Label(tab1, text="Nightly Hotel Compensation")
lbl_nightly_compensation.grid(row=1, column=2)

# Define the Entries
txt_people_per_room = DoubleVar(value=2)
entry_ppr = Entry(tab1, textvariable=txt_people_per_room)
entry_ppr.grid(row=0, column=1)

txt_num_employees_per_10_rooms = DoubleVar(value=1)
entry_ep10 = Entry(tab1, textvariable=txt_num_employees_per_10_rooms)
entry_ep10.grid(row=0, column=3)

txt_min_wage_inflation_percentage = DoubleVar(value=50)
entry_mwi = Entry(tab1, textvariable=txt_min_wage_inflation_percentage)
entry_mwi.grid(row=1, column=1)

txt_nightly_compensation = DoubleVar(value=72.05)
entry_nc = Entry(tab1, textvariable=txt_nightly_compensation)
entry_nc.grid(row=1, column=3)

# spacer
lbl_spacer = Label(tab1, text="")
lbl_spacer.grid(row=2, column=0, columnspan=4)

style1 = ttk.Style()
btn_calculate = ttk.Button(tab1, text="Calculate", width=15)
btn_calculate.grid(row=3, column=0, columnspan=4)

# spacer
lbl_spacer = Label(tab1, text="")
lbl_spacer.grid(row=4, column=0, columnspan=4)


window.mainloop()

Solution

  • For the widgets you add, you need to give a "bg" argument (bg for "background"), so if you create a label, you need to do it as

    lbl_spacer = Label(tab1, text = "", bg="white")
    
    

    same goes for your frames:

    tab_1 = ttk.Frame(tab_control, bg="white")
    

    you can also just use "background" in place of "bg" if you prefer. Also if you ever want to change that colour i would definetely recommend doing the following for ease

    bgColour = #my chosen colour
    
    Label(tab1, text="", bg=bgColour"