Reputation: 321
In pygame I am trying to print the text '10 x 10'. The 10 is stored as an integer in sizeOfGrid variable. My current code is:
text = font.render((str(sizeOfGrid),'x',str(sizeOfGrid)), 1, (10, 10, 10))
textpos = (32,210)
screenDisplay.blit
When I run the program it returns the error exceptions.TypeError: text must be string or unicode
. I don't understand this error or what is wrong as I have converted the 10 into a string.
Upvotes: 1
Views: 3023
Reputation: 322
The first argument of font.render
must be of the datatype string or unicode. You probably just need to change that. Instead of (str(sizeOfGrid),'x',str(sizeOfGrid)
you may write something like
"%d x %d" % tuple([sizeOfGrid]*2)
.
The "%d x %d"
part is the string format, where we ask for two digit arguments. With an tuple sample (4, 4)
this would give us the string "4 x 4"
.
To add arguments to the string, we use modulo followed by the arguments, that is % tuple([sizeOfGrid]*2)
where [sizeofGrid]*2
is [10, 10]
and tuple([sizeOfGrid]*2)
is (10, 10)
, which happen to be our two digit arguments. You can use it like this:
text = font.render("%d x %d" % tuple([sizeOfGrid]*2), 1, (10, 10, 10))
Upvotes: 1
Reputation: 967
I would use something like this
screenDisplay.blit(font.render("{0}x{0}".format(sizeOfGrid), True, (10, 10, 10)), (32, 210))
font.render
expects first arguement a string, you are providing a tuple.
note:I used {0}x{0}
since both values are sizeOfGrid.
Upvotes: 1