views:

77

answers:

2

hi everybody!

i have a problem with wxpython and his rich text control, when i try to insert unicode characters... \xb2 prints an apex '2', '\u2074' should print an apex '4'...
edit: i use windows vista... and i tried 'coding cp1252 ' and 'utf-8' but with the same result...
2edit: on vista it crashs, on xp it shows a strange square (i guess it's when the pc doesn't recognize the character...)

here is the source code:

from __future__ import unicode_literals

import wx
import wx.richtext as rt

class Trial(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Prova', 
                size=(400, 400))

        self.rtc = rt.RichTextCtrl(self, style=wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER)
        wx.CallAfter(self.rtc.SetFocus)
        #self.rtc.Freeze()
        self.rtc.BeginFontSize(14)
        self.rtc.WriteText('hei!\xb2') #alright
        self.rtc.WriteText('hi\u2074!')#crash
        self.rtc.EndFontSize()
        
       
if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = Trial()
    frame.Show()
    app.MainLoop()

but when i try to run it, it crashs...

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    frame = display.Trial()
  File "C:\Users\expert\Desktop\display.py", line 15, in __init__
    self.rtc.WriteText('hi\u2074!')
  File "C:\Python26\lib\site-packages\wx-2.8-msw-ansi\wx\richtext.py", line 2507, in WriteText
    return _richtext.RichTextCtrl_WriteText(*args, **kwargs)
  File "C:\Python26\lib\encodings\cp1252.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u2074' in position 4: character maps to <undefined>

so...what sould i do? i really need to show it...

thanks everybody!!

A: 

This can sometimes occur if you forget to set your encoding. Put this at the top of the code:

# -*- encoding: utf-8 -*-

before any code including comments but after the shebang (#!/usr/bin/python)

Thomas O
i've already tried, thanks :)
Ant
Wrong, forgetting the encoding line will lead to a `SyntaxError` instead of `UnicodeEncodeError`.
AndiDog
+2  A: 

If you want Unicode support then you should be using the unicode version of wxpython.

There are two versions of wxPython for each of the supported Python versions on Win32. They are nearly identical, except one of them has been compiled with support for the Unicode version of the platform APIs. Unless you've been told differently, you probably want to get the Unicode build of wxPython.

Most other platforms also have two versions.

It works fine if you pass the actual symbols e.g

self.rtc.WriteText("hei!²")
volting
oo...i didn't know.. i'll try right now! thank you :)
Ant
nothing...i re-installed wxpython 2.6 (unicode version) but it present exactly the same output... (i already had that version, but i didn't remember...)
Ant
Your right I just tested... Im not sure why that is but it works fine if use the actual symbols instead of the utf representation i.e `self.rtc.WriteText("hei!²")` works.
volting
@ant: The installation path contains "wx-2.8-msw-ansi" so I'm sure you *didn't* have the Unicode version. Make sure you uninstall the wxPython package completely and reinstall the Unicode build. More infos on http://wiki.wxpython.org/UnicodeBuild
AndiDog
Actually `'\xb2'` works but `'\u2074'` is shown as a square box.
volting
It's shown as square box because your font doesn't contain that glyph. U+2074 is "superscript 4" and not contained in many fonts, whereas ² is in most fonts. Try another non-Windows-1252 character such as U+04B0 (should work with Tahoma).
AndiDog
@andi but i've installed the unicode version...from here http://sourceforge.net/projects/wxpython/files/wxPython/2.8.11.0/wxPython2.8-win32-unicode-2.8.11.0-py26.exe/download and also i checked that the installation would become the standard one... indeed, "unicode" in wx.PlatformInfo returns True
Ant
@volting yeah, like to me ... @andi sorry i'm not sure i understood what you've written...
Ant
@ant: So now that you installed the Unicode version and get the same behavior as volting (square box instead of character), your problem is solved. Choose a system font like "DejaVu Sans" that supports the "superscript four" glyph in order to see the character.
AndiDog
@AndiDog: +1 I hadn't even thought of font support, Ill have to remember this for the next time I have unicode issues.
volting
thank you, great! :)
Ant
i cant' make +1 cause i haven't enough reputation...i'm sorry
Ant
@ant: Don't forget to accept volting's answer.
AndiDog