I am building an interface that has a Treeview
to display a list of items.
I would like for some items to be displayed with italic font.
I understood that a Treeview
can apply a tag
to its items, tags have bound properties - including a font that will be applied to the items containing that tag.
First try
I have tried creating a tag with a new font, passing only the italic property to such font, but the results were awful:
treeview.tag_configure(
'content_modified', font=font.Font(slant='italic'))
I realized that I was creating a whole new font, not only an italic effect font, and the base font not necessarily is the same as the one being used by the Treeview
.
Second try
I got the desired result visually, but the code is bothering me.
The problem
- I would like to be able to query the
Treeview
for the font that it is using, and I could not find a way to do that. I would rather copy the queried font instead of a global named font expecting it to be the one that theTreeview
is using.
CodePudding user response:
As I mentioned in the comment section. In ttk are some differences due the style engine and some options are not longer in the widgets themselves.
The Tile widgets are mostly compatible with the corresponding Tk widgets. They support the same set of widget commands and the same essential options. However, most of the appearance-related options have been removed — more precisely, they’ve been moved — out of the widgets themselves and into the style system.
In another section of the same document it is stated that:
Layouts use the same hierarchical naming scheme as elements, falling back to the parent theme if no match is found. Styles also use hierarchical names, but with a different fallback chain. The search for maps and defaults proceeds along the name hierarchy, but terminates at the “root” style named “.”. The parent theme is not consulted. Theme- wide default settings like -background and -font can be set on the root style, overriding them if necessary on individual widget-specific styles
So if nothing is defined a ttk.widgets
grabs the parents value, since I'm not aware of any parent except the root style, I would consider this as the style that it grabs from. print(style.configure('.','font'))
prints TkDefaultFont
and when I configure this font, all items in the Treeview changes without any call to tree.tag_configure
, which leads me to the conclusion I have the right font.
But be aware that this font most likely is used in more style schemas than just the Treeview that you will configure with it too.
Example Code:
import tkinter as tk
from tkinter import ttk
from tkinter import font
root = tk.Tk()
widget = ttk.Treeview(root, style='my.Treeview')
widget['columns']=('value1')
widget.heading(column="#0", text='Home')
widget.column('#0', stretch = True, minwidth=80, width = 180)
widget.heading(column="#1", text="value1")
widget.column('#1', stretch = True, minwidth=80, width = 180)
widget.insert("", "end", iid=0, text="Insert Me", values="here",
tags='my_tag')
widget.insert(0, "end", iid=None, text="Insert Me2")
widget.pack()
style = ttk.Style()
italic_font = tk.font.nametofont(style.configure('.','font'))
italic_font.config(slant='italic',weight='bold')