如何在Mac上的ttk/Tkinter笔记本中为框架的背景色匹配? [英] How can I match background colors for a Frame in a Notebook for ttk/Tkinter on a Mac?
问题描述
在Mac上基于Tkinter + ttk的GUI上工作时,我注意到背景颜色和Notebook小部件存在问题.将ttk.Frame
添加为ttk.Notebook
选项卡时,框架的显示背景与笔记本选项卡的插入"背景不匹配.
如何使ttk.Frame
与周围的背景颜色匹配,而无需硬编码对于非Mac用户来说看起来很奇怪的值?
我读了一些关于自定义样式的SO答案,但目前尚不清楚如何在这种情况下查询父窗口小部件的背景颜色.据我所知,它们都是一样的!
>>> 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'
上述屏幕截图的示例代码:
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()
重要细节:Python 2.7.8,Tcl/Tk 8.5.15中提供了此信息(Tk.eval('info patchlevel')
的报道).
我遇到了'systemWindowBody'
问题,只是使用sys.platform
检查了操作系统,并仅将颜色硬编码为OSX.
如果您有大量的小部件,则可以为OSX使用特定的主题. (这是我发现的一个很好的tkinter主题答案:更改标签标题").
我知道这可能并不理想,但这至少可以确保您不会弄乱其他平台上的外观. 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 How can I make the 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! Example code for above screenshot: Important details: This is present with Python 2.7.8, Tcl/Tk 8.5.15 (as reported by I ran into the 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. 这篇关于如何在Mac上的ttk/Tkinter笔记本中为框架的背景色匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!ttk.Frame
as a ttk.Notebook
tab, the displayed background of the frame does not match the 'inset' background for the notebook tab.ttk.Frame
match the surrounding background color, without hard-coding a value that will look weird for non-Mac users?>>> 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'
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()
Tk.eval('info patchlevel')
).'systemWindowBody'
problem and just checked the OS using sys.platform
and hardcoded the color for OSX only.