1

I try to set up a wx.dataview.DataViewListCtrl. I have multiple columns with text content. Some Entries of the first column are wider than the selected default width. They are cut off. I could set the column width by hand but I like to set it up automatically to the maximum content or header width. Is there an automated way to do so? If not, how can I calculate the ideal width. I'm on Ubuntu Linux. The used backend is gtk. A C++ answer using wxDataViewListCtrl could help, too. I'm able to translate it.

Dan A.S.
  • 654
  • 8
  • 24
Martin Fehrs
  • 792
  • 4
  • 13

2 Answers2

2

You can set the column width to wxCOL_WIDTH_AUTOSIZE (probably mapped to wx.COL_WIDTH_AUTOSIZE in Python) to make it fit its existing elements.

VZ.
  • 21,740
  • 3
  • 39
  • 42
  • Sadly this has no effect. – Martin Fehrs Jan 14 '20 at 14:51
  • 1
    Oops, I didn't notice you were under Ubuntu and so were using the native GTK version of the control. It indeed has some bugs with `wxCOL_WIDTH_AUTOSIZE` support due to GTK limitations and in this case computing the fitting width manually is indeed the only thing to do. – VZ. Jan 15 '20 at 11:35
1

You can determine the column size knowing which is the longest size of each text value stored in its cells programmatically and assign the widest value to this column width.

For this, you must create a font object:

# Example font:
font = wx.Font(pointSize = 10, family = wx.DEFAULT, style = wx.NORMAL, weight wx.NORMAL, faceName = 'Consolas')

# Get the screen display control and set the font:
dc = wx.ScreenDC()
dc.SetFont(font)

# Gets the width and height of that text with this font:
w,h = dc.GetTextExtent("hello world")

You must calculate this for each text you enter in the cell of that column, remembering the largest of all those values. After completing the ListView, you will already have a reference for the column size.

Dan A.S.
  • 654
  • 8
  • 24
  • Is there anything else to consider? For example spacing. – Martin Fehrs Jan 15 '20 at 12:06
  • Surely. Different fonts have different sizes. But if you always use the same font, doing some tests you can get a pattern to calculate the space that source occupies. Anyway, have you tried this (Windows)? https://stackoverflow.com/questions/35771863/how-to-calculate-length-of-string-in-pixels-for-specific-font-and-size (using ctypes) – Dan A.S. Jan 15 '20 at 15:00