Change color of “tab header” in ttk.Notebook

前端 未结 2 812
长发绾君心
长发绾君心 2020-12-06 18:23

Gretings!

I want to change the color displayed in a tab header, created using ttk.Notebook. After search for a while I\'ve found that to change the style of ttk wid

相关标签:
2条回答
  • 2020-12-06 19:02

    You can try creating a custom theme.

    import tkinter as tk
    from tkinter import ttk
    
    root = tk.Tk()
    
    mygreen = "#d2ffd2"
    myred = "#dd0202"
    
    style = ttk.Style()
    
    style.theme_create( "yummy", parent="alt", settings={
            "TNotebook": {"configure": {"tabmargins": [2, 5, 2, 0] } },
            "TNotebook.Tab": {
                "configure": {"padding": [5, 1], "background": mygreen },
                "map":       {"background": [("selected", myred)],
                              "expand": [("selected", [1, 1, 1, 0])] } } } )
    
    style.theme_use("yummy")
    
    note = ttk.Notebook(root)
    f1 = ttk.Frame(note, width=300, height=200)
    note.add(f1, text = 'First')
    f2 = ttk.Frame(note, width=300, height=200)
    note.add(f2, text = 'Second')
    note.pack(expand=1, fill='both', padx=5, pady=5)
    
    tk.Button(root, text='yummy!').pack(fill='x')
    
    root.mainloop()
    

    EDIT

    The most detailed ttk documentation is from the tcl/tk world

    eg.

    http://www.tcl.tk/man/tcl/TkCmd/ttk_notebook.htm

    For some useful python-based examples, you can grab the pyttk-samples package from http://code.google.com/p/python-ttk/

    0 讨论(0)
  • 2020-12-06 19:04

    I had been using Oblivion's answer for some time, but I encountered an issue where the open/save dialog button outlines disappeared and Checkbuttons in Text widgets never appeared to be checked (even when they were checked). So, I translated the theme code into some style configuration and such to solve the problem (it solved it). This will let you change the tab bar color, the tab background/foreground and the active tab background/foreground. Plus, it won't cause issues with the rest of your chosen theme. It's essentially the same code from the theme translated over. So, really, Oblivion deserves most of the credit.

    Style().configure("TNotebook", background=myTabBarColor);
    Style().map("TNotebook.Tab", background=[("selected", myActiveTabBackgroundColor)], foreground=[("selected", myActiveTabForegroundColor)]);
    Style().configure("TNotebook.Tab", background=myTabBackgroundColor, foreground=myTabForegroundColor);
    

    Edit: Apparently, this solution doesn't work in Windows. I tested it in Linux (a number of versions of Xubuntu).

    0 讨论(0)
提交回复
热议问题