我想将此代码中的选项卡式框架的背景设为白色,但由于某些原因,我尝试过的所有方法都无法正常工作。背景颜色当前为灰色,但各个小部件的背景为白色。理想情况下,它们都是白色的。我发现唯一将背景更改为白色的解决方案最终删除了Button的边框。
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()
对于添加的小部件,您需要提供一个“ bg”参数(bg代表“背景”),因此,如果创建标签,则需要这样做
lbl_spacer = Label(tab1, text = "", bg="white")
同样适用于您的框架:
tab_1 = ttk.Frame(tab_control, bg="white")
如果愿意,您也可以只使用“ background”代替“ bg”。另外,如果您想更改该颜色,我绝对会建议您轻松进行以下操作]
bgColour = #my chosen colour
Label(tab1, text="", bg=bgColour"