wx.lib.fancytext.StaticFancyText does not support Unicode text

I want to use wx.lib.fancytext.StaticFancyText control in my wxPython application and make my script run under WinXP and Linux.

I found this control does not support unicode text.

Here is my testing code:

···

#-----------------------------------------------------------------------------------------

#!/usr/bin/python

-- coding: utf-8 --

import wx
import wx.lib.fancytext as fancytext

class MyFrame(wx.Frame):
def init(self, *args, **kwargs):
wx.Frame.init(self, *args, **kwargs)

    toppanel = wx.Panel(self)
    topsizer = wx.BoxSizer(wx.HORIZONTAL)

    # NOTE: create a unicode text

    stf = fancytext.StaticFancyText(toppanel, -1, u'<font color="red">测试</font>')
    topsizer.Add(stf)

    toppanel.SetSizer(topsizer)
    topsizer.SetSizeHints(self)

if name == ‘main’:
app = wx.App(redirect=False)

frame = MyFrame(parent=None)
frame.Show()

app.MainLoop()

#-----------------------------------------------------------------------------------------

Save it as a utf-8 script file.

This script file can run under Linux But failed on WinXP.

When I ran it under WinXP, I got below error:

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

E:\Desktop>test.py
Traceback (most recent call last):
File “E:\Desktop\test.py”, line 24, in
frame = MyFrame(parent=None)
File “E:\Desktop\test.py”, line 14, in init
stf = fancytext.StaticFancyText(toppanel, -1, u’娴?璇?</fo

')
File “D:\System\Python26\Lib\site-packages\wx-2.8-msw-unicode\wx\lib\fancytext
.py”, line 395, in init
bmp = RenderToBitmap(text, background)
File “D:\System\Python26\Lib\site-packages\wx-2.8-msw-unicode\wx\lib\fancytext
.py”, line 356, in RenderToBitmap
width, height, dy = GetFullExtent(str, dc, enclose)
File “D:\System\Python26\Lib\site-packages\wx-2.8-msw-unicode\wx\lib\fancytext
.py”, line 346, in GetFullExtent
RenderToRenderer(str, renderer, enclose)
File “D:\System\Python26\Lib\site-packages\wx-2.8-msw-unicode\wx\lib\fancytext
.py”, line 329, in RenderToRenderer
p.Parse(str, 1)
UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 50-51: ord
inal not in range(128)

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Does anyone know how to fix this bug? I want to use StaticFancyText for both WinXP and Linux.

Shao Hao wrote:

I want to use wx.lib.fancytext.StaticFancyText control in my wxPython
application and make my script run under WinXP and Linux.

I found this control does not support unicode text.

The fancytext module is very old and has been basically untouched since
it was created, so that's not too surprising. However...

"D:\System\Python26\Lib\site-packages\wx-2.8-msw-unicode\wx\lib\fancytext
.py", line 329, in RenderToRenderer
    p.Parse(str, 1)
UnicodeEncodeError: 'ascii' codec can't encode characters in position
50-51: ord
inal not in range(128)

If you take a look at the code where the exception happens you'll see
that p is the parser created from xml.parsers.expat.ParserCreate(), and
a quick test shows:

>>> import xml.parsers.expat
>>> p = xml.parsers.expat.ParserCreate()
>>> p.Parse(u'\u1234')
Traceback (most recent call last):
   File "<input>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u1234' in
position 0: ordinal not in range(128)
>>>

So the problem is not really that fancytext doesn't support unicode,
it's that it's using expat which doesn't and is apparently attempting to
implicitly convert the parameter to a string object.

Possible fixes might be for fancytext to convert to utf-8 before parsing
and convert back to Unicode when drawing the text, or maybe just
switching to use a different parser. Patches are welcome.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!