I'm using wxPropertyGrid through wxPython for a program I'm working on and
have been having heaps of trouble getting it to work with unicode.
After a lot of head scratching and learning more about unicode than I ever
wanted to know I think I've managed to work out what's happening though I'm
clueless as to why! When I create a PGProperty and set its value, passing it
a unicode object, it seems to be encoding it to bytes using the utf-8 codec,
and then decoding it to unicode using latin-1 or something similar... This
doesn't cause problems for code points below 127 but anything higher gets
mangled. If my understanding is correct then this would be roughly
appropriate behaviour (except for the choice of 'latin-1' codec) if I were
passing it a string not a unicode object, but it is definitely unicode!
At this point I would love to provide you with minimal sample code
demonstrating this behaviour, but the code below which is meant to do just
that seems to work fine! If I just copy the same string into my program it
behaves as described above though!
I'm using Python 2.7.2 and wxPython 2.9.2.4 on OSX. I’m running it through
PyDev on eclipse and the default encoding is set to utf-8. I’ve also set
wx.SetDefaultPyEncoding and wx.Font.SetDefaultEncoding to utf-8 to no avail.
I appreciate that the information I’ve provided probably isn’t massively
helpful but if anyone has any thoughts about possible causes I’d be grateful
for any help you can give me. In the meantime I'll keep banging my head
against it in the hope that something useful falls out!
Cheers,
John
Sample code, requires python default encoding to be utf-8:
# coding=utf-8
import wx
import wx.propgrid as pg
app = wx.App(False)
wx.SetDefaultPyEncoding('utf-8')
dlg = wx.Dialog(None)
grid = pg.PropertyGrid(dlg)
value = u'§˚¨†ßƒ∂^'
# This should print the correct value
print value
sprop = pg.StringProperty('String Property', 'sprop', value=value)
# This should be wrong; specifically it should be '§Ë¨â ÃÆâ^'
print sprop.GetValue()
# But doing this should get you back to the original string
# value = bytes(value)
# value = value.encode('latin-1')
# value = value.decode('utf-8')
# print value # gives -> '§˚¨†ßƒ∂^'
def handler(event):
property = grid.GetSelection()
# This should be correct
print property.GetValue()
grid.Bind(pg.EVT_PG_CHANGED, handler)
grid.Append(sprop)
dlg.ShowModal()
···
--
View this message in context: http://wxpython-users.1045709.n5.nabble.com/Unicode-troubles-with-wxPropertyGrid-tp5713345.html
Sent from the wxPython-users mailing list archive at Nabble.com.