【问题标题】:Unable to change the background of a tkinter tabbed display无法更改 tkinter 选项卡式显示的背景
【发布时间】:2020-08-04 20:28:24
【问题描述】:

我想将此代码中的选项卡式框架的背景设置为白色,但由于某种原因,我尝试的所有方法都不起作用。背景颜色当前为灰色,但各个小部件的背景为白色。理想情况下,它们都是白色的。我发现将背景更改为白色的唯一解决方案是移除按钮的边框。

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

【问题讨论】:

  • 将所有Label 更改为ttk.Label 是否可以解决问题?

标签: python tkinter ttk


【解决方案1】:

对于您添加的小部件,您需要给出一个“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"

【讨论】:

  • 当我执行tab_1 = ttk.Frame(tab_control, bg="white") 时,我实际上得到了一个错误。它说“_tkinter.TclError:未知选项“-bg””
  • ttk.Frame 不支持bgbackground 选项。
  • 做到了!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-19
  • 2016-11-30
  • 2017-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多