简体   繁体   中英

How to change tkinter ttk Notebook background colour (Python)?

I am trying to change the background color of the Notebook widget in my python application so that it matches the blue color instead of being white.

I've tried searching for hours but to no avail. Is it not working because I'm working on a mac?

tabs_notebook = ttk.Notebook(self)
tabs_notebook.configure(bg=DARKBLUE)
tabs_notebook.pack()

view_tab = ttk.Frame(tabs_notebook)
new_tab = ttk.Frame(tabs_notebook)
tabs_notebook.add(view_tab, text="View Patient Details")
tabs_notebook.add(new_tab, text="Add New Patient")

问题截图

One of the major complaints levelled at Tk was that it was ugly. This was mostly because it never fit the native look and feel of the hosting environment and because the amount of configuration options provided meant application developers tended to roll their own style which never fit well with any other applications. The other problem was that on Windows and MacOS Tk used naive rendering APIs to draw buttons and other UI elements but failed to keep up when these platforms introduced theming. As a result even if the Tk application looked ok on default Windows XP, it would look out of place where a user selected an alternate theme.

So in developing ttk (themed Tk) one of the primary design goals was to ensure that the default theme fit the current platform. On Windows and MacOS this means that UI elements are drawn using the native theming APIs. On Linux - it just has to make a best guess.

In your specific case the notebook elements are being drawn using MacOS themeing API calls and you cannot change the colors for this theme from Tk. However, what you can do is define an entire Tk theme. There are some examples in Tk code doing this and these were imported to Python by someone. Those should yield enough to generate a new theme using Tk drawing APIs and possibly images for some elements to allow for a fully custom theme should you require that amount of customization.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM