Search code examples
python-2.7wxpythonword-wrap

How to set up word wrap for an stc.StyledTextCtrl() in wxPython


I was wondering about this, so I did quite a bit of google searches, and came up with the SetWrapMode(self, mode) function. However, it was never really detailed, and there was nothing that really said how to use it. I ended up figuring it out, so I thought I'd post a thread here and answer my own question for anyone else who is wondering how to make an stc.StyledTextCtrl() have word wrap.


Solution

  • Ok, so first you need to have your Styled Text Control already defined, of course. If you don't know how to do this, then go watch some tutorials on wxPython. I recommend a youtuber called sentdex http://youtube.com/sentdex, who has a complete series on wxPython, as well as Zach King, who has a 4 episode series on making a text editor. Anyways, my definition of my text control looks like this: self.control = stc.StyledTextCtrl(self, style=wx.TE_MULTILINE). Yours could look a little different, but the overall idea is the same.

    self.control = stc.StyledTextCtrl(self, style=wx.TE_MULTILINE)
    

    Many places will tell you that it will need to be SetWrapMode(self, mode), but if you have self.CONTROLNAME at the beginning like I do, you will get an error if you also put self as an argument because self. at the beginning counts as the argument. However, if your control is defined with self.CONTROLNAME and you don't put the self.CONTROLNAME at the beginning of your SetWordWrap()function, you'll also get an error, so be careful with that. Mode just has to be 0 or 1-3. So for example, mine looks like this: self.control.SetWrapMode(mode=1). Word wrap mode options:

    0: None | 1: Word Wrap | 2: Character Wrap | 3: White Space Wrap

    My final definition and word wrap setup looks like this:

    self.control = stc.StyledTextCtrl(self, style=wx.TE_MULTILINE)
    self.control.SetWrapMode(mode=1)
    

    And that's it! Hope this helped.

    Thanks to @Chris Beaulieu for correcting me on an issue with the mode options.