Python &引用;至于;或;而";动态OLED滚动循环?

Python &引用;至于;或;而";动态OLED滚动循环?,python,for-loop,while-loop,scroll,Python,For Loop,While Loop,Scroll,我有一段代码(在下面)可以在我的OLED上滚动一个长字符串…它可以工作,但它很难看而且不是动态的。 我想我可以得到var\u read\u文件中的字符数,然后执行for循环或while循环,使字符串根据字符串的长度滚动 我的想法是这样的: my_str_count = (len(var_read_file)) loopcounter = 0 scrollindex = 1 while my_str_count > loopcounter: scrollindex = scrolli

我有一段代码(在下面)可以在我的OLED上滚动一个长字符串…它可以工作,但它很难看而且不是动态的。 我想我可以得到
var\u read\u文件中的字符数
,然后执行
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()


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

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

根据你的回答:

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)

我应该以“我是一个初学者”作为开场白,但我不知道如何更有效地完成。什么类型的显示器是
display
?它是如何定义的?ssd1306 OLED显示器您不理解。您正在使用名为
display
的变量。该变量是一个类的实例,具有
show
fill
text
方法,但我们不知道它是哪个类,也没有关于如何使用它的文档。如果你在屏幕上显示了一些东西,你可能正在使用像Qt或GTK这样的工具包,然后不应该尝试手动处理动画(知道它,就在那里)。那是你正在使用的图书馆吗?如果你在做嵌入式编程,那是另一种怪兽。你应该避免使用全局编程。请给我们一个完整的例子,我们可以运行,而不是突然的事情。