5

I wrote a small app recently that needs to be cross-platform. I used Python and Tkinter for the GUI.

It works great but recently I got a new laptop with a hiDPI screen and it seems to mess up the TreeView (see image below). The text height is too big compared to the height of the rows.

The picture is taken from a virtual machine running Windows 7 as a guest. I had to set the text to a larger size (125%) to be able to read on the screen (every other program I've tried work fine with this setting).

Is there any way to set the height of a row in Tkinter TreeView? I haven't seen anything like that in the docs.

enter image description here

Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75

2 Answers2

12
s = ttk.Style()
s.configure('Treeview', rowheight=40) # repace 40 with whatever you need
Novel
  • 13,406
  • 2
  • 25
  • 41
  • 1
    I like answers simple but If you don't could please add working example as well? – Nae Jan 24 '18 at 22:42
  • Thanks a lot, sorry for not including a MCVE, I thought the picture would speak for itself. – Jacques Gaudin Jan 24 '18 at 22:59
  • 1
    @JacquesGaudin in coding a single character mistake can be the difference between working and not, so we like to be able to test our solutions before we post them. By not including a MCVE, you are putting the burden on us to write some boilerplate code to do the testing. In this case I had an answer already since it's not the first time I've been asked this question. If I hadn't had a ready answer I would not have bothered researching your problem without a way to test solutions. – Novel Jan 24 '18 at 23:11
  • @Novel I have a project with 3 different treeviews, and I have tried to use : `s= ttk.Style()` `s.configure('Treeview', rowheight=40)` But then it changes the row height for all treeviews , not for the treeview that I wanted. I have tried to change the `root` but it didnt work, can you please help me with this? – Aleksandar Beat Jul 03 '18 at 21:49
  • @AleksandarBeat Read up on styles. I modified the global style. To do what you want you need to make a style with a new name like `s.configure('Aleksandar.Treeview', rowheight=40)` which you can then use by passing the style argument like `t = ttk.Treeview(self, style='Aleksandar.Treeview')`. – Novel Jul 03 '18 at 23:41
  • @Novel i have tryed to do what you told me but I didnt succeed. Please check out this question, tell me if you can help me with it: [link](https://stackoverflow.com/questions/51169603/changing-row-height-for-different-treeviews-tkitner) – Aleksandar Beat Jul 04 '18 at 09:31
6

I already have a variable setup for font size and would like to avoid setting up a variable for row height. So my code looks like this:

style = ttk.Style()
style.configure("Treeview.Heading", font=(None, LARGE_FONT), \
                rowheight=int(LARGE_FONT*2.5))
style.configure("Treeview", font=(None, MON_FONTSIZE), \
                rowheight=int(MON_FONTSIZE*2.5))

When LARGE_FONT is set to 14, the row height is set to 35. When MON_FONTSIZE is set to 12, the row height is calculated as 30.

The end result has the correct spacing (IMO) for the system font. YMMV for other font families though:

wman gnome gsettings.png

WinEunuuchs2Unix
  • 1,801
  • 1
  • 17
  • 34