动态OLED滚动的“for”或“while”循环?

2024-09-27 07:29:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一段代码(在下面)可以在我的OLED上滚动一个长字符串…它可以工作,但它很难看而且不是动态的。 我想我可以得到var_read_file中的字符数,然后执行for循环或while循环,使字符串根据字符串的长度滚动

我的想法是这样的:

my_str_count = (len(var_read_file))
loopcounter = 0
scrollindex = 1
while my_str_count > loopcounter:
    scrollindex = scrollindex + 5
    display.fill(0)
    display.text(var_read_file, scrollindex, 15, 1)
    display.show()
    time.sleep(0.1)
    loopcounter = loopcounter + 1

以下是当前的丑陋代码:

def myoutboundscroll():
    display.fill(0)
    display.text(var_read_file, 0, 15, 1)
    display.show()
    display.fill(0)
    display.text(var_read_file, -5, 15, 1)
    display.show()
    display.fill(0)
    display.text(var_read_file, -10, 15, 1)
    display.show()
    display.fill(0)
    display.text(var_read_file, -15, 15, 1)
    display.show()
    display.fill(0)
    display.text(var_read_file, -20, 15, 1)
    display.show()
    display.fill(0)
    display.text(var_read_file, -25, 15, 1)
    display.show()
    display.fill(0)
    display.text(var_read_file, -30, 15, 1)
    display.show()
    display.fill(0)
    display.text(var_read_file, -35, 15, 1)
    display.show()
    display.fill(0)
    display.text(var_read_file, -40, 15, 1)
    display.show()
    display.fill(0)
    display.text(var_read_file, -45, 15, 1)
    display.show()
    display.fill(0)
    display.text(var_read_file, -50, 15, 1)
    display.show()
    display.fill(0)
    display.text(var_read_file, -55, 15, 1)
    display.show()
    display.fill(0)
    display.text(var_read_file, -60, 15, 1)
    display.show()
    display.fill(0)
    display.text(var_read_file, -65, 15, 1)
    display.show()
    display.fill(0)
    display.text(var_read_file, -70, 15, 1)
    display.show()


Tags: 字符串代码textreadvarmyshowcount
2条回答

根据你的回答:

def myoutboundscroll(text, duration_in_sec):
    shift = -5  # text position increment
    text_len = len(text)
    time_incr = duration_in_sec / text_len
    for scrollindex in range(0, text_len * shift, shift):
        display.fill(0)
        display.text(text, scrollindex, 15, 1)
        display.show()
        time.sleep(time_incr)

通过一些小的调整,我的想法奏效了:

def myoutboundscroll():
    global my_str_out_count
    loopcounter = 0
    scrollindex = 0
    while my_str_out_count > loopcounter:
        scrollindex = scrollindex - 5
        display.fill(0)
        display.text(var_read_file, scrollindex, 15, 1)
        display.show()
        time.sleep(0.1)
        loopcounter = loopcounter + 1

相关问题 更多 >

    热门问题