-1

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!

  • It would help if you showed us a sample of your code so we can see how you're processing the data and what settings you have for your STC. Also, have you tried debugging, manually calling hexdump then stepping through your program and comparing them? Is there a difference in the number of spaces? Maybe the number of spaces is correct but they are too thin and you need a fixed width font. – acattle Aug 07 '12 at 01:44
  • Hey thanks for the reply. I don't think it's so much the code as it is my inexperience. I'm using unmodified Scapy code found here: [link](http://trac.secdev.org/scapy/browser/scapy/utils.py#L67). I then print this output to the StyledTextCtrl. If I copy and paste the hex dump into an editor like gedit, the spaces appear correctly and everything is nicely formatted. So what I guess I need to look for is how to set a fixed width font? – Stanley Switalski Aug 07 '12 at 13:13
  • @acattle This is the code for the StyledTextCtrl I am using. It was automatically generated by BoaConstructor. ' self.HexDumpTerminal = wx.stc.StyledTextCtrl(id=wxID_SENDPACKETSFRAMEHEXDUMPTERMINAL, name=u'HexDumpTerminal', parent=self, pos=wx.Point(16, 192), size=wx.Size(480, 256), style=0) self.HexDumpTerminal.SetMinSize(wx.Size(330, 120)) ' – Stanley Switalski Aug 07 '12 at 13:28
  • Thanks for the update. You pretty much confirmed my suspicion so I went a head and submitted an answer. In the future you can add information to your questions (and answers) using the "edit" link at the bottom of your post (you can also edit comments, but only within 5 minutes of posting). Welcome to Stack Overflow and I hope I see you around! – acattle Aug 07 '12 at 17:26

1 Answers1

0

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.

Community
  • 1
  • 1
acattle
  • 3,073
  • 1
  • 16
  • 21
  • Thank you so much! I've been a lurker for a while but the community has been so great in helping me learn python. I was trying to put the code block according to the hints below but I guess it just didn't work. – Stanley Switalski Aug 07 '12 at 18:03
  • The code I had used was: self.NameOfSTC.StyleSetFont(0, wx.Font(10, wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_LIGHT)) – Stanley Switalski Aug 07 '12 at 18:04
  • @StanleySwitalski Am I to understand you are still having trouble setting the font? I don't have much experience with STCs but I remember from [a previous answer I gave](http://stackoverflow.com/questions/10866078/wxpython-change-the-background-colour-of-a-styledtextctrl/10867334#10867334) that calling `StyleClearAll()` can sometimes help. Unfortunately, that is the limit of my STC knowledge, so I hope it works! :p – acattle Aug 07 '12 at 18:16
  • No I'm all good. You've been great and thanks for the reply! I was just posting the code that I used for others. I've been in a situation where I was looking at posts and couldn't get proper code from the ideas the author posted. Makes life easier for people like me who really don't know programming all too well. – Stanley Switalski Aug 07 '12 at 19:24