Невозможно изменить фон отображения вкладок tkinter - PullRequest
0 голосов
/ 22 апреля 2020

Я хочу сделать фон рамки с вкладками в этом коде белым, но по какой-то причине все, что я пробовал, просто не работает. Цвет фона в настоящее время серый, но фон отдельных виджетов белый. В идеале все они были бы белыми. Единственное решение, которое я нашел, которое изменило фон на белый, закончилось тем, что удалил границу кнопки.

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

1 Ответ

0 голосов
/ 22 апреля 2020

Для добавляемых виджетов необходимо указать аргумент "bg" ( bg для "background" ), поэтому, если вы создаете метку, вам нужно сделать это как

* 1004. *

То же самое относится и к вашим кадрам:

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

Вы также можете просто использовать «background» вместо «bg», если хотите. Также, если вы когда-нибудь захотите изменить этот цвет, я бы определенно рекомендовал сделать следующее для простоты

bgColour = #my chosen colour

Label(tab1, text="", bg=bgColour"
...