wxPython StyledTextCtrl Hexadecimal formatting

730 views Asked by At

To whomever can lend a hand.

I'm building an app with BoaConstructor in Python which uses a wx.STC.StyledTextCtrl. In this styledtextctrl I am outputting hexadecimal data through Scapy's hexdump function. It adds the line numbers, hexadecimal dump and character transcode. Unfortunately, I cannot figure out how to format this text in the StyledTextCtrl so it displays like a regular hex editor would (see images here https://i.stack.imgur.com/xtGa3.jpg). Thanks!

1

There are 1 answers

4
acattle On BEST ANSWER

Since you said that the output looks OK in other editors, I'm guessing the issue is that the font uses variable pixel widths for characters (e.g. if a wide character like "w" is 15 pixels wide then a thinner character like "i" may be only 10 pixels). In a fixed-width font (sometimes called monowidth font or typewritter text font) all characters have the exact same width.

You can see evidence of this in your screen shot; there is a slight extra indent on the second row right before the ".(." part. It seems that the space character is much thinner than numerals, so all those missing pixels make the third "column" of the bottom row appear too far left (while extra width in the second row pushes it slightly right)

You need to find a font such that IsFixedWidth() returns True then set up your STC to use that font. Normally, you'd set this using the SetFont() method but I happen to know that STCs prefer to use there own methods. I found this StyleSetFont() method that is specific to STCs, so that's probably a better bet.

As for choosing your font, here is a good tutorial about how fonts work in wxpython. The author actually is an active member here. Specially, search that article for his "wx.FontEnumerator widget" example. It should allow you to find fixed-widths fonts and view them to see if you like them. For a quick-and-dirty solution, this forum post talks about the wx.TELETYPE flag that guarantees fixed-width and these code samples should give you some idea how to use it.

Good luck!

----EDIT----
I'm not very familiar with STCs but I remember from a previous answer I gave to a question involving STCs that you might need to call StyleClearAll() after setting your font. See the EDIT section of that answer for more information about why this may be required.