My problem is that in the example below, the text in the highlighted row is positioned at the top and is not vertically centered.
I would like to achieve the same effect as with the CSS attribute “vertical-align: middle”.
import tkinter as tk
from tkinter import ttk, font
class SampleTreeView:
def __init__(self, parent):
columns = ('ord', 'char')
font_size = 16
view_font = font.Font(family = 'Times', size = font_size, slant = font.ROMAN)
style = ttk.Style()
style.configure('Sample.Treeview', font = view_font, rowheight = round(font_size * 1.5))
#style.configure('Sample.Treeview.field', anchor = tk.CENTER)
self._tree = ttk.Treeview(parent, style = 'Sample.Treeview', columns = columns, selectmode = tk.BROWSE, show = '', padding = (0, 0, 0, 0))
self._tree.column('#0', width = 0, stretch = tk.NO)
col_widths = [0] * 2
for i in range(4):
c = chr(i + 97)
t = (str(ord(c)), c)
for j in range(2):
col_widths[j] = max(col_widths[j], view_font.measure(t[j]))
self._tree.insert(parent = '', index = tk.END, iid = i, text = '', values = t)
for j in range(2):
w = round(col_widths[j] * 1.5)
self._tree.column(columns[j], anchor = tk.CENTER, minwidth = w, width = w, stretch = tk.NO)
self._tree.pack(side = tk.LEFT, expand = True, fill = tk.Y)
self._tree.selection_set(3)
root = tk.Tk()
tv = SampleTreeView(root)
root.mainloop()
Version tkinter: 8.6.14
SampleTreeView