0

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()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
DenGor
  • 205
  • 7
  • 18
  • 1
    This [answer](https://stackoverflow.com/a/29572789/355230) might help you. – martineau Mar 08 '18 at 18:03
  • If you need to change the color of a frame, why not use the standard tkinter `Frame`? It's very easy to change the color of it. – Bryan Oakley Mar 08 '18 at 22:55
  • @martineau you are right, although it looks a little too complicated. I was really hoping there's a chance to simply use `ttk.Style.configure()`. I'll look into that answer. – DenGor Mar 09 '18 at 12:46
  • @Bryan Oakley I guess I didn't express myself properly. What I need is to be able to change the style of `ttk.Notebook` tabs. My initial guess was to do something to `ttk.Frame` since this is how tabs are constructed. – DenGor Mar 09 '18 at 12:48
  • Duplicate of [ttk styling “TNotebook.Tab” background and borderwidth not working](https://stackoverflow.com/q/22389198/291641) possibly given the above comment. – patthoyts Mar 24 '18 at 16:33

1 Answers1

1

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()
DenGor
  • 205
  • 7
  • 18