4

While working on a Tkinter + ttk based GUI on my Mac, I noticed a problem with background colors and the Notebook widget. When adding a ttk.Frame as a ttk.Notebook tab, the displayed background of the frame does not match the 'inset' background for the notebook tab.

How can I make the ttk.Frame match the surrounding background color, without hard-coding a value that will look weird for non-Mac users?

example of frame background mismatch

I read a few SO answers suggesting custom styles, but it's unclear how to query for the parent widget's background color in this situation. As far as I can tell, they're all the same!

>>> ttk.Style().lookup("TFrame", "background")
'systemWindowBody'
>>> ttk.Style().lookup("Notebook", "background")
'systemWindowBody'
>>> ttk.Style().lookup("Notebook.client", "background")
'systemWindowBody'
>>> ttk.Style().lookup("Notebook.tab", "background")
'systemWindowBody'

Example code for above screenshot:

import Tkinter
import ttk

class GUI(object):
    def __init__(self,root):
        self.root = root
        self.root.title(u"Frame Mismatch Example")
        self.mainframe = ttk.Frame(self.root, padding=(6, 6, 12, 12))
        self.mainframe.grid(sticky='nwse')

        self.notebook = ttk.Notebook(self.mainframe)
        self.tab1 = ttk.Frame(self.notebook)
        ttk.Button(self.tab1, text='Exit', command=self.root.destroy).pack(padx=100, pady=100)
        self.notebook.add(self.tab1, text="Tab 1")
        self.notebook.pack()

def main():
    root = Tkinter.Tk()
    root.columnconfigure(0, weight=1)
    root.rowconfigure(0, weight=1)
    makeGUI = GUI(root)
    root.mainloop()

if __name__ == '__main__':
    main()

Important details: This is present with Python 2.7.8, Tcl/Tk 8.5.15 (as reported by Tk.eval('info patchlevel')).

josePhoenix
  • 538
  • 1
  • 5
  • 14

1 Answers1

2

I ran into the 'systemWindowBody' problem and just checked the OS using sys.platform and hardcoded the color for OSX only.

If you have a large amount of widgets, you could use a specific theme for OSX. (This was a good tkinter theme answer I found: Change color of "tab header" in ttk.Notebook)

I know this may not be ideal, but it will at least make sure you don't mess up the look on other platforms.

Community
  • 1
  • 1
Jeff M.
  • 1,037
  • 10
  • 7
  • Thanks Jeff. The folks in #Tcl on FreeNode figured it was a problem in Tcl/Tk itself, so the best I can hope for is a workaround. It remains to be seen what these widgets do in OS X Yosemite, of course :) – josePhoenix Oct 21 '14 at 00:42
  • Still the same problem in El Capitan – Grezzo Sep 30 '16 at 20:46