Vitozz_RDX Vitozz_RDX - 4 months ago 14
Python Question

How to draw str(non-latin-word) in Pygame?

I'm only learning to program , so my question can be trivial . Sorry for that !

I want a programm to draw a word (in russian) on screen using Pygame . And that is ok, everything is working :

# coding=utf-8
import pygame,sys

pygame.init()

screen = pygame.display.set_mode((640,480))

some_font=pygame.font.Font(None,30)

word_to_print=some_font.render(u'привет',True,(255,255,255),(0, 0, 0))

while 1:
screen.blit(word_to_print,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
pygame.display.update()


But when i try to draw the same word, but in this form :

some_font.render(str('привет'),True,(255,255,255),(0, 0, 0))


=> output is strange looking rubbish (something like that ÐÐµÑ Ñа).

I got two questions :

1) how to get my program to drow str(any word in russian) ?

2) why is it working like that ?

Answer

1) how to get my program to draw str(any word in russian) ?

Strictly speaking, you can't. If you want to display arbitrary Russian text, you should store that text in a unicode() object, not a str() object:

unicode_string = u'привет'
some_font.render(unicode_string, ...)

If you don't have access to the original unicode() string, but only have the encoded str() string, you can decode it, if you know the encoding:

str_string = 'привет'
unicode_string = str_string.decode('utf-8')
some_font.render(unicode_string, ...)

If the object to which you apply str() is not already a str() (see your comments below), then you can call unicode() instead:

unicode_string = unicode(my_object)
some_font.render(unicode_string, ...

Finally, if the object to which you apply str() does not provide .__unicode__() method, but does return an encoded str() string, you can decode that string if you know the encoding:

str_string = str(my_object)
unicode_string = str_string.decode('utf-8')
some_font.render(unicode_string, ...

2) why is it working like that ?

From the pygame docs, "Both Unicode and char (byte) strings are accepted. For char strings a LATIN1 encoding is assumed." You have passed a utf-8-encoded string to a method that requires a latin1-encoded string.