How can I match background colors for a Frame in a Notebook for ttk/Tkinter on a Mac?

懵懂的女人 提交于 2019-12-07 09:39:49

问题


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?

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')).


回答1:


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.



来源:https://stackoverflow.com/questions/26472565/how-can-i-match-background-colors-for-a-frame-in-a-notebook-for-ttk-tkinter-on-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!