5

I got a problem with changing the height of the Treeview.heading. I have found some answers about the dimensions of Treeview.column, but when I access Treeview.heading in the documentation, there is not a single word about changing the height of the heading dynamically when the text doesn't fit (and wrapping it) or even just hard-coding height of the heading in pixels. I don't have to split the text to two rows, but when I just keep it that long the whole table (as it has many entries) takes up the whole screen. I want to keep it smaller, therefore I need to split longer entries.

Here is how it looks like:

As it can be seen, the text is not fully visible in Treeview heading

Sqoshu
  • 944
  • 2
  • 9
  • 16
  • Yeah that is what I thought too - if you can resize the width, why no height. But it seems that part is missing. Or I am just terribly blind. EDIT: The 'size' is the reference name of the column that you want to set the heading in (these names can be set in `Treeview.columns`; you can also use numbers) and the `text` is the string that should appear in it. So it's not it. – Sqoshu Jan 29 '20 at 15:31
  • 1
    Check out this post: [ttk.Treeview - Can't change row height](https://stackoverflow.com/a/26962663/7475225). It looks like a good start. – Mike - SMT Jan 29 '20 at 15:39
  • 1
    This allows changing the height of the rows, but not the heading, unfortunately. – Sqoshu Jan 29 '20 at 15:44
  • The more I dig in the more I find I can edit in the header but non of the editable style layout has a height field for header... It may not be possible in this case. My guess is the height is hard coded and does not have a variable we can change. – Mike - SMT Jan 29 '20 at 16:11

3 Answers3

2

I can't find any documentation to verify this but it looks like the height of the heading is determined by the heading in the first column.

Reproducing the problem

col_list = ('Name', 'Three\nLine\nHeader', 'Two\nline')
tree = Treeview(parent, columns=col_list[1:])
ix = -1
for col in col_list:
    ix += 1
    tree.heading(f'#{ix}', text=col)

The fix

col_list = ('Name\n\n', 'Three\nLine\nHeader', 'Two\nline')

or, if you want to make it look prettier

col_list = ('\nName\n', 'Three\nLine\nHeader', 'Two\nline')

The only problem is I haven't figured out how to centre the heading on a two line header

Edit

The newlines work if it is the top level window but not if it is a dialog. Another way of doing this is to set the style. I've got no idea why this works.

style = ttk.Style()
style.configure('Treeview.Heading', foreground='black')
j_4321
  • 15,431
  • 3
  • 34
  • 61
cup
  • 7,589
  • 4
  • 19
  • 42
  • 2
    This works (the toplevel version), but with a clarification: you didn't name your first column (by doing col_list[1:]). You can cheat by using all of your columns, then adding an extra line outside the for loop: `tree.heading('#0', text='\n\n')` – smp55 Apr 19 '21 at 18:19
0

you can use font size to increase the header height for sometimes;

enter image description here

style = ttk.Style()
style.configure('Treeview.Heading', foreground='black', background='white', font=('Arial',25),)
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Maktum
  • 56
  • 4
0

Scrolledtreeview1.heading("#0", text="\n\n",anchor="center")

use \n in heading #0 text as many lines you want.

Muddybeast
  • 31
  • 4