Hi everyone,
I am new to Python and wxPython, and I have been writing a series of
small text editors to help myself learn.
However, I have hit a major road block. I can't figure out a way to
make my toolbar buttons for "bold" and "italicize" apply to *only* the
currently highlighted/selected text in my StyledTextCtrl. I'm even
beginning to wonder if this is even possible.
I've spent the last several days looking at code on the web. I've also
looked at the wxPython demo.
Here is the code that I last tried, but it doesn't do anything:
import wx
import wx.stc as stc
import wx.py.dispatcher as dispatcher
class EditorFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(600, 500))
...
self.textCtrl = stc.StyledTextCtrl(self)
self.textCtrl.SetFocus()
self.textCtrl.StyleSetSpec(stc.STC_STYLE_DEFAULT, "face:
Courier New, size: 10, fore: #000000")
self.textCtrl.StyleClearAll()
self.textCtrl.StyleSetSpec(1, "face: Courier New, size: 10,
fore: #000000, back: #FFFFFF")
self.textCtrl.StyleSetSpec(2, "face: Courier New, size: 10,
bold, fore: #000000, back: #FFFFFF")
...
def OnBold(self, event):
frm, to = self.textCtrl.GetSelection()
# remove all styling
self.textCtrl.StartStyling(frm, 0xff)
self.textCtrl.SetStyling(len(self.textCtrl.GetSelectedText()),
stc.STC_STYLE_DEFAULT)
# set current selection to bold
self.textCtrl.StartStyling(frm, 0xff)
self.textCtrl.SetStyling(len(self.textCtrl.GetSelectedText()),
2) # style 2 is bold
...
app = wx.App()
EditorFrame(None, -1, 'wxPython Editor')
app.MainLoop()
-bw_77