首页 > 解决方案 > 如何更改继承自 ttk.Frame 的 Frame 类中容器的背景颜色?

问题描述

我想更改容器的背景颜色,但我无法做到。我的代码只改变了 frame1 的背景颜色。更改容器的背景颜色需要什么代码?

import tkinter as tk
from tkinter import ttk


class MainFrame(ttk.Frame):
    def __init__(self, container):
        super().__init__(container)

        self.style2 = {'fg': 'black', 'bg': '#e95420', 'activebackground': 'coral', 'activeforeground': '#2f2f2f'}

        self.frame1 = ttk.Frame(self,  padding = (0, 0, 0, 0), style='My.TFrame')
        self.frame1.pack(fill="x", padx=0, pady=100, expand=False)

        s = ttk.Style()
        s.configure('My.TFrame', background='blue')

        self.exit = tk.Button(self.frame1, compound=tk.RIGHT, text='Exit', width=20, relief="solid", command=container.exit)
        self.exit.pack(expand=False, padx=0, pady=100, ipadx=10, ipady=10, side=tk.BOTTOM)
        self.exit.configure(self.style2)
        self.pack()


class StatusBar(tk.Frame):
    def __init__(self, container):
        super().__init__(container)

        self.variable = tk.StringVar()
        self.variable.set("add the current time here")
        self.label=tk.Label(self, bd=0, relief="solid", height="2", width="1500", textvariable=self.variable, foreground="white", background='#2f2f2f', font=('helvetica',9))
        self.label.pack()
        self.pack(fill="x", side="bottom", ipady=0, padx=0, pady=0)


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry("1500x1100")

    def exit(self):
        self.destroy()


if __name__ == "__main__":
    app = App()
    sb = StatusBar(app)
    mf = MainFrame(app)
    app.mainloop()

标签: python-3.xtkinter

解决方案


推荐阅读