简体   繁体   中英

Can't change styles for classes inheriting from ttk.Frame

So I want to just change the background of my frames so I can layout them properly and I can't seem to change styles for my frames.

style.configure('TFrame', background='red')
style.configure('Blue.TFrame', background='blue')

main_window = MainWindow(root, style='Blue.TFrame')

The above code resuslts in a red background, while I need it to change to blue, and if I don't change TFrame background, there is just no backgound color at all. My MainWindow class does inherit from ttk.Frame, so I don't know if that is what's causing it...

Minimal Reproducible Example:

import tkinter as tk
from tkinter import ttk

class MainWindow(ttk.Frame):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent)
        self.parent = parent

        # Search_company shortcut
        self.search_comp = ttk.Entry(self)
        self.search_comp.grid(row=0, column=0)

def main():
    root = tk.Tk()
    root.state('zoomed')

    # Configuring styles
    style = ttk.Style()

    style.configure('TFrame', background='red')
    style.configure('Blue.TFrame', background='blue')

    main_window = MainWindow(root, style='Blue.TFrame')
    main_window.grid(row=0, column=1, sticky='nsew')

    root.mainloop()

if __name__ == "__main__":
    main()

You aren't passing the style option to the superclass. It needs to be this:

super().__init__(parent, *args, **kwargs)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM