Discussion:
problem with pygame.font and symbol °
Spina Giovanni Vittorio
2014-05-14 16:08:08 UTC
Permalink
Hi,
I have a little problem
I must render a digit value in my application using font
the code is:

/def ReadValue():
a= ...
...
return int(a)

myfont = pygame.font.Font(None, 20)
angle = ReadValue()
LabelSurface = myfont.render("angle: "+str(angle)+"°", True, [255,255,255])
screen.blit(LabelSurface, [0,0])/

the problem is that (assuming angle is 30) I can't see
/angle: 30°/
but
/angle: 30°/

How can I do to render only /°/ withouth/Â/ ?
Thanks
Vittorio
Dominik George
2014-05-14 16:33:06 UTC
Permalink
Moin,
Post by Spina Giovanni Vittorio
the problem is that (assuming angle is 30) I can't see
/angle: 30°/
but
/angle: 30°/
that looks pretty much like an encoding issue. Apparently, Font.render expects
latin1 encoding.

You could use the str.encode funktion to work around that, finding out how to
do that is left as an exercise to you ;).

Cheers,
Nik
Spina Giovanni Vittorio
2014-05-14 16:45:43 UTC
Permalink
Have I to do same like
/MainFont.render(str(angle)+"°".encode("utf-8"), True, color)/
??
the line above give me an error :( !
PSL!!!!
Vittorio
Post by Dominik George
Moin,
Post by Spina Giovanni Vittorio
the problem is that (assuming angle is 30) I can't see
/angle: 30°/
but
/angle: 30°/
that looks pretty much like an encoding issue. Apparently, Font.render expects
latin1 encoding.
You could use the str.encode funktion to work around that, finding out how to
do that is left as an exercise to you ;).
Cheers,
Nik
/
/
Dominik George
2014-05-14 16:49:56 UTC
Permalink
Hi,
Post by Spina Giovanni Vittorio
Have I to do same like
/MainFont.render(str(angle)+"°".encode("utf-8"), True, color)/
??
the line above give me an error :( !
More like "^".decode("utf-8").encode("latin1")

-nik
Spina Giovanni Vittorio
2014-05-14 16:51:51 UTC
Permalink
Thank YOU!
it works
Post by Dominik George
Hi,
Post by Spina Giovanni Vittorio
Have I to do same like
/MainFont.render(str(angle)+"°".encode("utf-8"), True, color)/
??
the line above give me an error :( !
More like "^".decode("utf-8").encode("latin1")
-nik
Russell Jones
2014-05-26 06:52:27 UTC
Permalink
Post by Spina Giovanni Vittorio
str(angle)+"°"
Or for Python 2.7 and 3.3+ compatibility
(str(angle)+u"°").encode("latin1")

Note that in 2.7 adding the normal string to the unicode one produces a
unicode string. It might seem better to use unicode(), but this doesn't
exist in 3.
Similarly u"".join(['a', 'b', 'c'])

Alternatively, use
from __future__ import unicode_literals
(str(angle)+"°").encode("latin1")

Russell
Russell Jones
2014-05-30 05:27:26 UTC
Permalink
The other thing you might consider is adding as the first or second line

# -*- coding: utf8 -*-
or
# vim: set fileencoding=utf8 :
(see PEP 0263)

This is the encoding of the source file, in py2 specifying the specific set
of bytes will be in a string for a particular character, in py3 and in py2
unicode strings (u"") specifying how those bytes should be translated to a
sequence of unicode code points in a string.

(I've not used python on windows much, so you might need to specify utf16le
or something else, mbcs?)

Russell
Post by Russell Jones
Post by Spina Giovanni Vittorio
str(angle)+"°"
Or for Python 2.7 and 3.3+ compatibility
(str(angle)+u"°").encode("latin1")
Note that in 2.7 adding the normal string to the unicode one produces a
unicode string. It might seem better to use unicode(), but this doesn't
exist in 3.
Similarly u"".join(['a', 'b', 'c'])
Alternatively, use
from __future__ import unicode_literals
(str(angle)+"°").encode("latin1")
Russell
Loading...