Search code examples
pythonwxpythonstrikethroughrichtextctrl

wxPython RichTextCtrl with strikethrough


I want to set text with strike-through in RichTextCtrl of wxPython. But could not find any method like BeginStrikethrough or SetStrikethrough.

Is it possible to apply strike-through in RichTextCtrl? How?

EDIT 1:

Font with strike-through flag used with BeginFont and EndFont not giving strike-through effect

import wx
import wx.richtext as rt

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, 1, 'Testing strike-through')
        rtc = rt.RichTextCtrl(self, -1)
        rtc.WriteText("normal text")
        rtc.Newline()

        font = wx.FFont(12, wx.FONTFAMILY_DEFAULT, face='Tahoma', flags=wx.FONTFLAG_STRIKETHROUGH)
        rtc.BeginFont(font)
        rtc.WriteText("This is strike-through")
        rtc.EndFont()

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

Output:

No strike-through in output


Solution

  • enter image description here

    1. You can use font.SetStrikethrough(True) with wxpython2.9.4 or higher, refer This Link

    2. Otherwise: use font.SetNativeFontInfoFromString(str) to set the flag with Native info description.

    Please check the following string to see the difference, only tested in windows:

    desc without Strikethrough: 0;-16;0;0;0;400;0;0;0;1;0;0;2;32;Tahoma

    desc with Strikethrough: 0;-16;0;0;0;400;0;0;1;1;0;0;2;32;Tahoma

    Code:

    import wx
    import wx.richtext as rt
    
    class MyFrame(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, 1, 'Testing strike-through')
            rtc = rt.RichTextCtrl(self, -1)
            rtc.WriteText("normal text")
            rtc.Newline()
    
            font = wx.FFont(12, wx.FONTFAMILY_DEFAULT, face='Tahoma', flags=wx.FONTFLAG_STRIKETHROUGH)
    
            info = font.GetNativeFontInfoDesc()
            info = self.setFontInfoStrikethrough(info)
            font.SetNativeFontInfoFromString(info)
    
            rtc.BeginFont(font)
            rtc.WriteText("This is strike-through")
            rtc.EndFont()
    
        def setFontInfoStrikethrough(self, info):
            print "orig:", info
            info = info.split(";")
            info[8] = r'1'
            info = ";".join(info)
            print "new :",info
            return info
    
    if __name__ == '__main__':
        app = wx.PySimpleApp()
        frame = MyFrame()
        frame.Show()
        app.MainLoop()