简体   繁体   English

更改ttk笔记本的文字大小和颜色

[英]Change text size and color of ttk notebook

I created a window and use ttk notebook to view a tab menu.我创建了一个窗口并使用 ttk notebook 查看选项卡菜单。 I am not able to change the text size and font,background color etc for the tab menu.我无法更改选项卡菜单的文本大小和字体、背景颜色等。

from tkinter import *
from tkinter import ttk
import tkinter

window = Tk()
style = ttk.Style()
note = ttk.Notebook(window)

window.tab1 = ttk.Frame(note)
window.tab2 = ttk.Frame(note)
window.tab3 = ttk.Frame(note)
window.tab4 = ttk.Frame(note)

note.add(window.tab1, text = "Home ")
note.add(window.tab2, text = "Disconnected ")
note.add(window.tab3, text = "Alarm ")
note.add(window.tab4, text = "")

note.pack()
style.configure('TNotebook.Tab', foreground='red')
window.mainloop()

How could I change the size and color of the text like "home","Alarm" etc individually??我如何单独更改“home”、“Alarm”等文本的大小和颜色? style.configure('TNotebook.Tab', foreground='red') change the fore color of notebook and all the tab text color is changed. style.configure('TNotebook.Tab', foreground='red') 改变notebook的前景色,所有标签的文字颜色都改变了。 how could i change the color of text suppose "disconnected"????我怎么能改变文本的颜色假设“断开连接”????

Here is a code I found, I edited it of course but it might help you about some of your demands such as color, (still don't know about text if any one can help by the way):这是我找到的一段代码,我当然对其进行了编辑,但它可能会帮助您解决您的一些需求,例如颜色,(如果有人可以提供帮助,仍然不知道文本):

style = ttk.Style()
style.theme_create('Cloud', settings={
    ".": {
        "configure": {
            "background": '#aeb0ce', # All colors except for active tab-button
            "font": 'red'
        }
    },
    "TNotebook": {
        "configure": {
            "background":'black', # color behind the notebook
            "tabmargins": [5, 5, 0, 0], # [left margin, upper margin, right margin, margin beetwen tab and frames]
        }
    },
    "TNotebook.Tab": {
        "configure": {
            "background": 'dark blue', # Color of non selected tab-button
            "padding": [5, 2], # [space beetwen text and horizontal tab-button border, space between text and vertical tab_button border]
            "font":"white"
        },
        "map": {
            "background": [("selected", '#aeb0ce')], # Color of active tab
            "expand": [("selected", [1, 1, 1, 0])] # [expanse of text]
        }
    }
})
style.theme_use('Cloud')

EDIT: here is the link: https://www.programcreek.com/python/example/104109/tkinter.ttk.Notebook but note that i have explored all the example code and this example was the only one dealing with:编辑:这里是链接: https ://www.programcreek.com/python/example/104109/tkinter.ttk.Notebook 但请注意,我已经探索了所有示例代码,这个示例是唯一一个处理:

style.theme_create()

This function of ttk allow you to set up the whole theme of your app, what is done here is about going from parent class widget to child class and configure things such as background, foreground and so on. ttk 的这个功能允许你设置你的应用程序的整个主题,这里所做的是从父类小部件到子类并配置背景,前景等。 What i understood is that each of widget level have name, and an amount of paremeter that you can change and what you don't touch stay default.我所了解的是,每个小部件级别都有名称,以及您可以更改的参数量以及您不触摸的保持默认设置。 So here we are exploring the parent, then the TNotebook, then Tnotebook.Tab, then map wich is the lowest level.所以我们在这里探索父级,然后是 TNotebook,然后是 Tnotebook.Tab,然后是最低级别的地图。 I'm working on it and figured out how to change the font color, add this parameter in the TNotbook.Tab configuration as I show below:我正在研究它并想出如何更改字体颜色,在 TNotbook.Tab 配置中添加此参数,如下所示:

"TNotebook.Tab": {
        "configure": {"foreground":"white"}

EDIT: Another break through about the selected and disabled tab-button, about having two different colors here is how you can do, in "map" class of the upper code:编辑:关于选中和禁用的选项卡按钮的另一个突破,关于这里有两种不同的颜色,是你如何在上层代码的“地图”类中做的:

"map": {"foreground": [("selected", "black"),("!disabled", "white")] }

the " theme_settings " of tkinter doc is quite useful: https://docs.python.org/3/library/tkinter.ttk.html tkinter 文档的“theme_settings”非常有用: https ://docs.python.org/3/library/tkinter.ttk.html

Found how to change text font in this topic: How can I change the size of tab caption box and font of ttk notebook tabs?在本主题中找到如何更改文本字体: How can I change the size of tab caption box and font of ttk notebook tabs?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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