Hi there. I started with wxPython (Under Linux) a little while ago and I'm enjoying very much, however, there doesn't seem to be a control for working with Rich text.

Iv found a few articles (mostly dated a few years ago) that suggest that such a control is on its way, but I haven't found it yet and the RTF Demo that came with the wxWidgets does not actually load or save RTF files.

So, is there one?

Regards

Max

Yes, RichTextCtrl is usable, I believe vegaseat has an example or two in the wx sticky thread on this forum.

Thanks for your replies:

evstevemd: The demo example looks very nice, but the rich text that is shown is created on the fly in code. The actual control cannot load or save RTF files (at least in Linux).

jlm699: Yes, I found this example (Its an excellent thread and I spent ages reading through it!) but its only for Windows users. I'm not using windows and I still need an RTF control.

This has become a real problem for me. If there is no RichTextCtrl for Python under Linux then I'll have to move to a different language, which will be a shame, because I rather like Python. I find it a bit hard to believe that wxPython does not have any method to deal with RTF type files (By this I mean I'm happy to have an editor that uses HTML, just so long as it loads and saves the files properly).

Its a shame, but Iv a feeling that Ill have to move away from Python rather soon. :(

Regards

Max

Not 'fly code', I mean on the fly, as in added to the control at startup using code (as opposed to loading a file).

Anyway, yes, Iv been using Google for this, but Iv come up with nothing that actually gives a working control that can load and save RTF.

Max

Not 'fly code', I mean on the fly, as in added to the control at startup using code (as opposed to loading a file).
Max

Max, you *can* load and save RTF files using wxRichTextCtrl, but they will be stored as XML files. Just fire up the wxPython demo, go to the wxRichTextCtrl specific demo, launch it, go to the File menu, select Save As, then select "XML files" for File type and save the document.

Now you can load it back in by going to the File menu and selecting the Open command. In the Open dialog select "XML files" for File type so you can find the saved document.

This must be working on all the platforms supported by wxPython.

Alexei

Max, you *can* load and save RTF files using wxRichTextCtrl, but they will be stored as XML files. Just fire up the wxPython demo, go to the wxRichTextCtrl specific demo, launch it, go to the File menu, select Save As, then select "XML files" for File type and save the document.

Now you can load it back in by going to the File menu and selecting the Open command. In the Open dialog select "XML files" for File type so you can find the saved document.

Ah, just for a moment there you gave me some hope!

Alas, it doesn't work! The file is certainly saved in XML, but when it comes to loading it back again all you get is the XML code, and no nice formatting!

Max

Ah, just for a moment there you gave me some hope!

Alas, it doesn't work! The file is certainly saved in XML, but when it comes to loading it back again all you get is the XML code, and no nice formatting!

Max

That's strange. I've successfully done it on Windows and I thought it should work on Linux too.

Anyway, I'm going to try it on Linux shortly, and will report back about what I've found out.

OK. I've tried saving and loading XML files produced by the RichTextCtrl demo on Linux, and everything worked without a hitch.

I'm running Linux Mint 5.0 (derived from Ubuntu 8.04), wxPython 2.8.9.1.

If you have an older version of wxPython, maybe that's why it doesn't work.

Please make sure that when loading the document back you select the correct filter (not txt, but xml).

Hmm, I'm using wxPython 2.8.9.1 and Ubuntu 7.10 but I can see a possible problem here:

When it comes to loading, there is no choice of filters, only "*" - this might well be the cause of the problem.

Iv had a look at the code for the demo, but I cant see how to change the filter on the load event. Ill have to try and work that out before I go any further because if this works my problem is solved - I don't care what format the text is saved in, just so long as it can be saved and loaded and look like RTF style text to the user.

Many thanks for your efforts, and if you have any tips about how to get that load filter set up on the demo Id be more than happy to try it out! ;)

Regards

Max

Okay, the problem has been sorted out now:

The demo saves nicely in XML (.xml or .ox) but when it comes to loading the code is not correct and the filter only shows *.*

To correct this, in the OnFileOpen routine, the following change must be made:

def OnFileOpen(self, evt):
        wildcard, types = rt.RichTextBuffer.GetExtWildcard(save=False)
        dlg = wx.FileDialog(self, "Choose a filename", 
                            #The following line was changed from "*.*"
                            wildcard=wildcard,
                            style=wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            if path:
                fileType = types[dlg.GetFilterIndex()]
                self.rtc.LoadFile(path, fileType)
        dlg.Destroy()

Having made this change the control will save as XML and load from XML and display as Rich Text! The problem is now fixed, so many thanks to everyone who tried to help out, but especially to alvinru for pointing in the right direction!

Regards

Max

This question has already been answered. Start a new discussion instead.