UnicodeEncodeError with pygame.display.set_caption()

Issue #315 open
Tjibbe
created an issue

If I put an Unicode character in the caption, pygame stops. If I do a pygame.display.set_caption("issinghoff - nöring"), pygame gives an error. But with fnt=pygame.font.Font('freesansbold.ttf', 30) imge = fnt.render("issinghoff - nöring"), there is no trouble at all, and I can blit the imge on the screen. I guess the caption is not drawn with Font.render() (??????)

Comments (9)

  1. Thomas Kluyver

    Can you give some more details:

    • What error do you get? Show the whole traceback.
    • Where did you get pygame from/how did you install it? There are various versions available built from different versions of the code.
    • Are you using Python 3 or 2?
  2. Tjibbe reporter

    After upgrading pygame from 1.9.1 to 1.9.2b1, I still get the same error. When you run my test program, do you also get the same error ?

  3. Thomas Kluyver

    Your test programme appears to be empty, but running pygame.display.set_caption("issinghoff - nöring") does not give me an error. I'm on Linux, though, so that may be doing something different. Let's have a look at the code...

  4. Thomas Kluyver

    Ah, if I use pygame.display.set_caption(u"issinghoff - nöring") it breaks on Python 2. I guess you're using from __future__ import unicode_literals?

    That method is expecting a str, and on Python 2 it will attempt to automatically convert unicode by encoding it as ascii. You can avoid that by manually encoding the text first:

    # I'm guessing the encoding to use here - CP1252 is the default on Windows in Western Europe.
    pygame.display.set_caption(u"issinghoff - nöring".encode('cp1252'))
    
  5. Thomas Kluyver

    In fact, looking at the docs of SDL_WM_SetCaption, which the data is passed on to, the text should be UTF-8 encoded:

    pygame.display.set_caption(u"issinghoff - nöring".encode('utf-8'))
    

    I'm leaving this issue open as a request for the pygame function to accept unicode objects on Python 2 and utf-8 encode them automatically.

  6. Log in to comment