DenGor
DenGor

Reputation: 205

Customising ttk.Frame widget

I want to be able to adjust the color of a ttk.Frame() widget. I know this question has been asked numerous times here. A very useful answer was given here: Change color of "tab header" in ttk.Notebook. One can do this using style.theme_create().

I want to make a similar adjustment using ttk.Style() but cannot figure out how. Could someone explain to me how I can do this?

The reason I am not using the method proposed in the above link is because there are other widgets in my code that I want to (and can) customize using ttk.Style().

import tkinter as tk
from tkinter import ttk

class Window():
    def __init__(self, parent):
        self.parent = parent
        self.parent.minsize(width=600, height=400)
        self.widgets()

    def widgets(self):   
        s1 = ttk.Style()
        s1.configure('My.TNotebook.Tab', padding=5, background='red')
        s1.map('My.TNotebook.Tab', background=[('selected', 'green')])

        self.nb1 = ttk.Notebook(self.parent, style='My.TNotebook.Tab')
        self.tab1 = ttk.Frame(self.nb1)        
        self.tab2 = ttk.Frame(self.nb1)
        self.tab3 = ttk.Frame(self.nb1)   
        self.nb1.add(self.tab1, text='Tab1')
        self.nb1.add(self.tab2, text='Tab2')
        self.nb1.add(self.tab3, text='Tab3')
        self.nb1.place(relx=0.1, rely=0.1, width=500, height=200)
        self.b1 = ttk.Button(self.parent, text='Quit', command=self.quit)
        self.b1.place(relx=0.4, rely=0.7, height=70, width=150)

    def quit(self):
        self.parent.destroy()

root = tk.Tk()
app = Window(root)
root.mainloop()

Upvotes: 0

Views: 1533

Answers (1)

DenGor
DenGor

Reputation: 205

I searched more and found an interesting and simple solution here: http://page.sourceforge.net/html/themes.html. The key to solving the problem seems to be the need to tell Python which theme to use (I am running Python on Windows). In my answer, I use classic but winnative, clam, alt, and default will do too. Using vista or xpnative makes no effect on the tabs' colors.

import tkinter as tk
from tkinter import ttk

class Window():
    def __init__(self, parent):
        self.parent = parent
        self.parent.minsize(width=600, height=400)
        self.widgets()

    def widgets(self):   
        s1 = ttk.Style()
        s1.theme_use('classic')
        s1.configure('TNotebook.Tab', background='navajo white')
        s1.map('TNotebook.Tab', background=[('selected', 'goldenrod'), ('active', 'goldenrod')])

        self.nb1 = ttk.Notebook(self.parent)
        self.nb1.place(relx=0.1, rely=0.1, width=500, height=200)
        self.tab1 = ttk.Frame(self.nb1)        
        self.tab2 = ttk.Frame(self.nb1)
        self.tab3 = ttk.Frame(self.nb1)
        self.nb1.add(self.tab1, text='Tab1')
        self.nb1.add(self.tab2, text='Tab2')
        self.nb1.add(self.tab3, text='Tab3')
        self.b1 = ttk.Button(self.parent, text='Quit', command=self.quit)
        self.b1.place(relx=0.4, rely=0.7, height=70, width=150)

    def quit(self):
        self.parent.destroy()

root = tk.Tk()
app = Window(root)
root.mainloop()

Upvotes: 1

Related Questions