MrObvious
Posts: 13
Joined: Sat Jun 04, 2016 11:20 am

Help with Python code and SenseHAT

Sat Jun 04, 2016 9:40 pm

Hello

This is my first post and I am new to python (so please go easy on me :D).

I have just recieved SenseHAT for my birthday and I am busy playing with it and I have created a scrolling text countdown timer that will count down the days to any given event. The event and date are set via user input so they write the event and give the date of the event and the code will display the event and number of days until it.

I want to include the ability for the user to select the text colour and background colour of the scrolling text, but I am stuck with the code.

This is my current code:

Code: Select all

from sense_hat import SenseHat
import datetime
import time
from datetime import timedelta


sense = SenseHat()

Red =(255, 0, 0)
Orange = (255, 128, 0)
Yellow = (255, 255, 0)
Green = (0, 255, 0)
Blue = (0, 0, 255)
Purple =(255, 0, 255)

event = input("Enter an event: ")

def ObtainDate():
    while True:
        userIn = input("Type Date: dd/mm/yy: ")
        try:
            return datetime.datetime.strptime(userIn, "%d/%m/%y")
        except ValueError:
            print ("Invalid Format!\n")

t1 = ObtainDate()

colour = input("Choose a text colour: Enter Red, Orange, Yellow, Green, Blue, Purple: ")

#First message
while True:
	text = event + ' = ' #String to be displayed on the first round of text
	sense.show_message(text, scroll_speed=0.05, text_colour = colour)
	time.sleep(0.1)

#Second message
	currentDay = datetime.datetime.today()
	difference = (t1 - currentDay).days
	display_text = str(difference)
	sense.show_message(display_text + "days", scroll_speed = 0.05, text_colour = colour, back_colour = (0,0,255)) 


This work in that the user can type an event and give a day, but then it fails.

In my limited understanding it is because in the following line: sense.show_message(text, scroll_speed=0.05, text_colour = colour) have text_colour set to colour (the user input variable) is not allowed.

I am unsure of how to rectify this. If someone could help me out that would be great.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Help with Python code and SenseHAT

Sun Jun 05, 2016 8:09 am

MrObvious wrote:
In my limited understanding it is because in the following line: sense.show_message(text, scroll_speed=0.05, text_colour = colour) have text_colour set to colour (the user input variable) is not allowed.

I am unsure of how to rectify this. If someone could help me out that would be great.
Congratulations on your birthday and your great present, and welcome to the forums.

Have a look at https://pythonhosted.org/sense-hat/api/ and scroll down to show_message.

Note carefully how the list of 3 RGB elements is written: your version is different.

MrObvious
Posts: 13
Joined: Sat Jun 04, 2016 11:20 am

Re: Help with Python code and SenseHAT

Sun Jun 05, 2016 10:53 am

B.Goode wrote:
MrObvious wrote:
In my limited understanding it is because in the following line: sense.show_message(text, scroll_speed=0.05, text_colour = colour) have text_colour set to colour (the user input variable) is not allowed.

I am unsure of how to rectify this. If someone could help me out that would be great.
Congratulations on your birthday and your great present, and welcome to the forums.

Have a look at https://pythonhosted.org/sense-hat/api/ and scroll down to show_message.

Note carefully how the list of 3 RGB elements is written: your version is different.

Thanks for the reply, however, I understand that the correct line is:

sense.show_message(text, scroll_speed=0.05, text_colour = (255,0,0)

for Red text for example. What I am struggling with is linking it to a user input. Currently my user can enter Event and date, but how do I link the RGB value of user input to the text_colour parameter within the sense.show_message function.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Help with Python code and SenseHAT

Sun Jun 05, 2016 11:03 am

MrObvious wrote: I understand that the correct line is:

sense.show_message(text, scroll_speed=0.05, text_colour = (255,0,0)

for Red text for example.
No, I'm afraid you misunderstand. Look very closely at the shape of the brackets - that is significant. Also, the number of opening and closing brackets must match.
What I am struggling with is linking it to a user input. Currently my user can enter Event and date, but how do I link the RGB value of user input to the text_colour parameter within the sense.show_message function.
You are quite right that you have identified a 'challenge': the input from the user is a string that (might) contain the name of a colour. [Q: what happens if the user enters 'dinosaur' instead?] You need to associate this with the named variable that contains the rgb definition of that colour. Doing some python reading around 'lists' or 'dictionaries' will help you get to a solution. Maybe asking for a number that represents the colour would be easier to handle?

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Help with Python code and SenseHAT

Sun Jun 05, 2016 11:04 am

You could use a dictionary to link the 'human' names to the RGB colours.

Code: Select all

colours = {'Red':(255,0,0),'Green':(0,255,0)}
Then lookup the RGB value after the user has typed the colour name.

Code: Select all

rgb = colours[user_input]
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

MrObvious
Posts: 13
Joined: Sat Jun 04, 2016 11:20 am

Re: Help with Python code and SenseHAT

Sun Jun 05, 2016 1:24 pm

Ok so the correct code for the show message is:

sense.show_message(text, scroll_speed=0.05, text_colour = [255,0,0])

and to link it with user input is possible with a dictionary potentially.

Looks like as B. Good says reading up on dictionaries is going to be the way forward.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Help with Python code and SenseHAT

Sun Jun 05, 2016 1:37 pm

Yes, but @scotty101 has given you a great example of how you might do it.

MrObvious
Posts: 13
Joined: Sat Jun 04, 2016 11:20 am

Re: Help with Python code and SenseHAT

Sun Jun 05, 2016 8:46 pm

Thanks for all your help. I have managed to get my script working and I have added an option to select a background colour too. Also I have added an if loop so if the user enters 'dinosaur' it won't work.

Now to the challenge to to build this into a GUI using Tkinter.

Here is the code if you are interested, comments and pointers welcome.

Code: Select all

from sense_hat import SenseHat
import datetime
import time
from datetime import timedelta


sense = SenseHat()

colours = {"Red":(255, 0, 0),
"Orange": (255, 128, 0),
"Yellow":(255, 255, 0),
"Green":(0, 255, 0),
"Blue":(0, 0, 255),
"Purple": (255, 0, 255)}


event = input("Enter an event: ")

def ObtainDate():
    while True:
        userIn = input("Type Date: dd/mm/yy: ")
        try:
            return datetime.datetime.strptime(userIn, "%d/%m/%y")
        except ValueError:
            print ("Invalid Format!\n")

t1 = ObtainDate()

while True:
    colour_text = input("Choose a text colour: Enter Red, Orange, Yellow, Green, Blue, Purple: ")
    if colour_text in colours:
        rgb = colours[colour_text]
        break
    else:
        print ("Not a colour option please choose again")
    
while True:
    colour_background = input("Choose a background colour: Enter Red, Orange, Yellow, Green, Blue, Purple: ")
    if colour_background in colours:
        rgb2 = colours[colour_background]
        break
    else:
        print ("Not a colour option please choose again")    

#First message
while True:
	text = event + ' is in ' #String to be displayed on the first round of text
	sense.show_message(text, scroll_speed=0.05, text_colour = rgb, back_colour = rgb2)
	time.sleep(0.1)

#Second message
	currentDay = datetime.datetime.today()
	difference = (t1 - currentDay).days
	display_text = str(difference)
	sense.show_message(display_text + " days", scroll_speed = 0.05, text_colour = rgb, back_colour = rgb2)

User avatar
bensimmo
Posts: 4622
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Help with Python code and SenseHAT

Wed Jun 15, 2016 8:08 pm

You could always get the user to scroll through the list, displaying the list on the SenseHat as they scroll up or down, and using the 'joystick' to do the scroll and select.
As an alternative challenge.

Return to “Python”