Unicorn HAT HD - Clearing the display on keyboard interrupt for *all* scripts


#1

I noticed when trying out the various example scripts, some of them clear the display when complete, and some do not. When inspecting the scripts, the ones that don’t are missing the lines:

except KeyboardInterrupt:
    unicorn.off()

But when I add these lines to a script that misses them, the script won’t even run. as though it doesn’t even recognise the command.

Is there any way of having the display clear at the end of every script upon keyboard interrupt, or are there simply some designs that it won’t work for?


#2

There may ne more to it than just adding the except function, there may be more code ahead of it you need to copy too. I think there needs to be a “try” statement or something somewhere? I’m no Python expert though.
https://wiki.python.org/moin/HandlingExceptions


#3

Thanks! You nailed it.

Made a copy of demo.py. After “step = 0”, adding the line “try:”, indenting everything after that, and finishing off with the unindented, code from my original post will properly run the script and clear the display when interrupted. Perfect!


#4

Good stuff, have fun.