Search code examples
python-2.7wxpython

Usage of ExportXML and ImportXML using RichTextXMLhandler in Wxpython 4.0


I have building simple application using the Richtextctrl in wxPython 4.0.0a1(Latest version) and using python 2.7.

I have tried to save the buffer content using richtextxmlhandler with savestrem method, but in the latest version wxpython savestrem methon is not available.

so. i used the ExportXML,but i am getting errors. please any example on usage of Exportxml in rich text is helpful.

Thanks in advance.


Solution

  • The RichTextCtrl had the SaveStream method removed in Phoenix. So now you need to use SaveFile:

    # wxPython Phoenix / Python 3 Version
    
    import wx
    import wx.richtext
    
    from io import BytesIO
    
    
    class MyFrame(wx.Frame):
    
        def __init__(self):
            wx.Frame.__init__(self, None, title='Richtext Test')
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            self.rt = wx.richtext.RichTextCtrl(self)
            self.rt.SetMinSize((300,200))
    
            save_button = wx.Button(self, label="Save")
            save_button.Bind(wx.EVT_BUTTON, self.on_save)
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(self.rt, 1, wx.EXPAND|wx.ALL, 6)
            sizer.Add(save_button, 0, wx.EXPAND|wx.ALL, 6)
    
            self.SetSizer(sizer)
            self.Show()
    
        def on_save(self, event):
            out = BytesIO()
            handler = wx.richtext.RichTextXMLHandler()
            rt_buffer = self.rt.GetBuffer()
            handler.SaveFile(rt_buffer, out)
            self.xml_content = out.getvalue()
            print(self.xml_content)
    
    
    if __name__ == "__main__":
        app = wx.App(False)
        frame = MyFrame()
        app.MainLoop()
    

    You can read more about SaveFile here:

    I also updated my blog article on this topic here: