Uncategorized

sense hat assignment notes

Asked 
Active today
Viewed 16 times
1

I’ve working on an assignment with the Raspberry Pi. I want to change the color of a word in a string or message in an if statement. I tried doing this. enter image description here

It came up with an error saying “TypeError: cannot concatenate ‘str’ and ‘tuple’ objects”. Does anyone know how to do what I’m trying to do? Thanks

 New contributor
  • What makes you think this is possible? Have you found a command to set the text colour? Surely setting a colour depends on the medium displaying the text? – joan 2 hours ago
  • I’m making it display the text on a raspberry pi. To change the text colour, you need to write text_colour = (the variable with the rgb values). I just don’t know how to change the colour of a word out of a sentence. – roger.dude 2 hours ago
  • Please add the error message to your question. – joan 2 hours ago
  • Ah, let me see. So you want to change colour of Rpi SenseHat LED matrix message. The colour setting part of your program is OK. What causes the problem is you try to concatenate a string to a tuple. In the first IF statement, “It is time for” is a string, but “(a, red)” is not a string. The API to change colour of a dot of the matrix is the API: set_pixels: Sense Hat doc’s API set_pixels pythonhosted.org/sense-hat/api. / to continue, … – tlfong01 30 mins ago   
  • For your assignment, there are two functions you can use: (1) set_pixels: to set ALL 64 pixels in the matrix in one go. Example: sense.set_pixels(question_mark) to display the question mark in the LED matrix. Of course you can display the letter “H” instead of a question mark. (2) set_pixel (no “s” at the end) to set ONLY ONE of the 64 pixels. It is this set only one pixel that you can use to individual pixel’s colour. / to continue, …– tlfong01 20 mins ago   
  • To summarize, you can (1) use first command to display one letter of 64 pixels, but you need to decide what are the colour of each pixel before hand, and “build” the letter, or (2) You display the letter first, then use the second command to change the colour pixel by pixel, one pixel at a time. / to continue, … – tlfong01 16 mins ago   
  • 1
    You said that ‘a’ is not a string but I’ve also written a=”Break” – roger.dude 14 mins ago
  • If your teacher’s assignment is open ended, you can experiment with both methods, then write one function, with many parameters, including (1) a list of characters as the message string, (2) a matrix of 8 x 8 elements, each of which is for the colour of each pixel of the character, / to continue, … – tlfong01 9 mins ago   
  • I am just suggesting one of many ways you can explore, of course you can think of better ways. Have a nice assignment and good luck. Cheers. – tlfong01 7 mins ago   
  • Some clarifications (1) in your statement [a = “xyz“], “xyz” is a string, but [a] is the name of a variable for the string, (2) a string can only be concatenate to another string, NOT a tuple, which is not a string type, therefore type error. I know it is confusing, so you need to study more python to understand what I am talking about. Nice python learning, bye. – tlfong01 1 min ago   Edit   
  • Ok. Thanks. I’ll try to understand it a bit more – roger.dude 45 secs ago

2 Answers

1

This question is unclear and not Pi specific (and this answer does not address the python coding errors).

The following python code fragment is an example of how to display coloured text on a console using ANSI escape codes. This works on ANY platform, it may or may not work on a SenseHat.

RED   = "\033[1;31m"
RESET = "\033[0;0m"

def pRed(s):
    print("%s%s%s" % (RED, s, RESET), file=sys.stderr)
def perr(l):
    pRed("%s - Error on line %d:" % (l, lineno))

  • I’m sorry. I meant for the question to be Pi specific and not Raspberry Pi Desktop. – roger.dude 1 hour ago

1

Try using the ANSI escape code:

"\033[1;32;40m Bright Green"

The above ANSI escape code will set the text colour to bright green. The format is: \033[ Escape code, this is always the same 1 = Style, 1 for normal. 32 = Text colour, 32 for bright green. 40m = Background colour, 40 is for black.

You can get the color codes here

 New contributor

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: