Python Forum

Full Version: help with scrolling text on RGB Matrix
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am able to scroll text using Runtext.py but would like to have the text that is displayed, get it from a separate document. This would make it simpler for the end user to change the text being displayed without getting into the actual code.
Would anyone have any solutions for this?
Thanks
What is runtext.py? A google search came up with multiple example programs with this name. Can you post your code?
(Apr-09-2024, 05:47 PM)deanhystad Wrote: [ -> ]What is runtext.py? A google search came up with multiple example programs with this name. Can you post your code?

sorry, I'm very new to this. Below is the runtext.py code:

!/usr/bin/env python
# Display a runtext with double-buffering.
from samplebase import SampleBase
from rgbmatrix import graphics
import time


class RunText(SampleBase):
    def __init__(self, *args, **kwargs):
        super(RunText, self).__init__(*args, **kwargs)
        self.parser.add_argument("-t1", "--text", help="The text to scroll on the RGB LED panel", default="Hello world!") 

    def run(self):
        offscreen_canvas = self.matrix.CreateFrameCanvas()
        font = graphics.Font()
        font.LoadFont("../../../fonts/texgyre-27.bdf")
        textColor = graphics.Color(255,0,0)
        pos = offscreen_canvas.width
        my_text = self.args.text

        while True:
            offscreen_canvas.Clear()
            len = graphics.DrawText(offscreen_canvas, font, pos, 25, textColor, my_text)
            pos -= 1
            if (pos + len < 0):
                pos = offscreen_canvas.width

            time.sleep(0.015)
            offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)


# Main function
if __name__ == "__main__":
    run_text = RunText()
    if (not run_text.process()):
        run_text.print_help()
Looks like this one:

https://github.com/hzeller/rpi-rgb-led-m...plebase.py

Your post was missing the last line. I corrected it.

The program already allows setting the message without any programming. Just supply the message in the command line.
Output:
python runtext.py -text "This is the message I want."
Changing the program to read the text from a file looks easy. You can reuse the "text" argument to be the name of a file that contains the text to display. You'll want to give text a default value that is a filename, not the text to display.
self.parser.add_argument("-t1", "--text", help="The text to scroll on the RGB LED panel", default=""message.txt") 
Only a few lines need to change in the run method.
    def run(self):
        offscreen_canvas = self.matrix.CreateFrameCanvas()
        font = graphics.Font()
        font.LoadFont("../../../fonts/texgyre-27.bdf")
        textColor = graphics.Color(255,0,0)
        pos = offscreen_canvas.width
        text_file = self.args.text

        # Read text from file
        with open(text_file, "r") as file:
            my_text = file.read()
 
        while True:
            offscreen_canvas.Clear()
            len = graphics.DrawText(offscreen_canvas, font, pos, 25, textColor, my_text)
            pos -= 1
            if (pos + len < 0):
                pos = offscreen_canvas.width
                # Each time we scrolled the entire message, read the text from the file.
                # To change text, modify the file, and when the current message is done,
                # the new message will be displayed.
                with open(text_file, "r") as file:
                    my_text = file.read()
 
            time.sleep(0.015)
            offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)
I don't have a device for testing this code, so there may be bugs.