设置单个 Tkinter Notebook 选项卡的样式

问题描述 投票:0回答:1

我想将一个笔记本选项卡的前景文本颜色更改为不同于下面创建的样式定义的其余选项卡。如前所述,我本来希望为选项卡 B 设置红色字体,但我不确定要设置什么样式才能设置这个选项卡的样式。有人可以告诉我我应该更改什么属性吗?非常感谢。

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()
root.geometry('%dx%d+0+0' %(200,200))
# Style  # /68485915/
style = ttk.Style()
style.theme_use("default")

# Notebook Style
style.configure('TNotebook', background='white')
style.configure('TNotebook.Tab', background='white', foreground='black')
style.map('TNotebook.Tab',background=[("selected",'green')])
style.configure('Red.TNotebook.Tab', foreground = 'red')

# Create Notebook and Frames
Book = ttk.Notebook(root)

aFrame = ttk.Frame(Book)

# I apply the Red tab style to this frame, but the tab does not change from the style created above
# I am not sure what the relationship between the frame and the tab which is part of the notebook
bFrame = ttk.Frame(Book, style='Red.TNotebook.Tab') #/18855943/

# Pack
aFrame.pack(fill = tk.BOTH, expand = True)
bFrame.pack(fill = tk.BOTH, expand = True)
Book.pack(fill = tk.BOTH, expand = True)

# Add Tabs
Book.add(aFrame, text = 'A')
Book.add(bFrame, text = 'B')

root.mainloop()

我查看了以下问题,但似乎没有一个能回答我的问题:

python tkinter tabs styles
1个回答
0
投票

我想更改一个笔记本标签的前景文本颜色 与样式定义的其余选项卡不同 在下方创建

  • 使用
    yummy
    代替
    default
  • 为绿色和红色添加两个常量。
  • 添加
    style.theme_create
  • 使用
    style.theme_use("yummy")
  • 评论出来
    style.configure

Snippet 修改了你的脚本:

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()
root.geometry('%dx%d+0+0' %(200,200))
# Style  # 68485915
style = ttk.Style()
 
COLOR_GREEN = "#26d663"
COLOR_RED = "#dd0202"

style.theme_create("yummy", parent="alt", settings={
    "TNotebook": {"configure": {"tabmargins": [2, 5, 2, 0] } },
    "TNotebook.Tab": {
    "configure": {"padding": [5, 1], "background": COLOR_GREEN},
    "map":       {"background": [("selected", COLOR_RED)],
    "expand": [("selected", [1, 1, 1, 0])] } } } )

style.theme_use("yummy")

 
# Create Notebook and Frames
Book = ttk.Notebook(root)

aFrame = ttk.Frame(Book)
Book.add(aFrame, text = 'A')
bFrame = ttk.Frame(Book) #/18855943/
Book.add(bFrame, text = 'B')
Book.pack(fill = tk.BOTH, expand = True)
  

root.mainloop()

截图:

© www.soinside.com 2019 - 2024. All rights reserved.