Raspberry PI multi-line上的LED矩阵?

问题描述 投票:0回答:1

我有一个python脚本,但我想添加更多行。我该怎么做呢?

#!/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("-t", "--text", help="The text to scroll on the RGB LED panel", default="I love you!")

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

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

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


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

我尝试添加另一个self.parser.add_argument,但是没有用。

#!/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("-t", "--text", help="The text to scroll on the RGB LED panel", default="I love you!")
self.parser.add_argument("-t", "--text", help="The text to scroll on the RGB LED panel", default="Will you marry me?")

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

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

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


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

这给了我一个错误输出:

Traceback (most recent call last):

argparse.ArgumentError: argument -t/--text: conflicting option string(s): -t, --text

我需要解决什么才能使其具有多行?我曾尝试使用Google搜索,但无法找到解决方案。

我正在使用带有128x32 LED面板的adafruit帽子引擎盖。

python argparse led raspberry-pi-zero
1个回答
0
投票

您只需要使用一个add_argument("-t", ...),然后您可以选择

1)全部发送为一个字符串

--text "line1;line2;line3"

后来使用split(";")将其分成几行

2)使用action="append"

add_argument("-t", "--text", action="append")

然后您可以多次使用--text,它将创建具有所有值的列表

--text "line1" --text "line2" --text "line3"

3)使用nargs="*"

add_argument("-t", "--text", nargs="*")

然后您可以将一个--text与很多行一起使用而没有;,但要使用空格

--text "line1" "line2" "line3"

import argparse

parser = argparse.ArgumentParser()

parser.add_argument('-t1')
parser.add_argument('-t2', action='append')
parser.add_argument('-t3', nargs='*')

args = parser.parse_args()

if args.t1:
    args.t1 = args.t1.split(';')
print(args)

使用

./main.py -t1 "a;b;c" -t2 "x" -t2 "y" -t2 "z" -t3 "1" "2" "3"

结果

Namespace(t1=['a', 'b', 'c'], t2=['x', 'y', 'z'], t3=['1', '2', '3'])
© www.soinside.com 2019 - 2024. All rights reserved.