中断控制台界面中滚动文本的一般方法

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

我正在尝试在控制台中制作游戏,并希望滚动文字。我希望能够按一个键/类型回车,并跳过滚动并打印其余部分。到目前为止,我尝试使用pygame(由于必须具有活动的显示表面而在图片外),sys.stdin.read(1)使用asyncio(阻止了cmd的运行,并且没有在基于异步ide的用户中查询用户)。

这是我对此的最新尝试。

import asyncio,time,sys

global skip 
immutablesleep = 0.04
mutablesleep = [immutablesleep]

async def aprintl(string,sep="",end="\n",sleep=mutablesleep):
    global skip
    for letter in string+end:
        print(letter+sep,end="",flush=True)
        await asyncio.sleep(sleep[0])
    skip = True

async def break_print():
    global skip
    while not skip:
        ch = sys.stdin.read(1)
        if len(ch)>0:
            mutablesleep[0]=0
            skip = True
        await asyncio.sleep(0.1)    


def printl(*args):
    global skip
    skip = False
    mutablesleep[0] = immutablesleep
    asyncio.gather(aprintl(*args),break_print())

建议我同时需要操作系统独立代码的模块时要牢记,并且在将模块冻结为exe时可以很容易地将它们挂钩。

python console keyboard interrupt
1个回答
0
投票

我相信您的问题与最后一行有关

asyncio.gather(aprintl(*args),break_print())

查看docs,函数签名看起来像这样:awaitable asyncio.gather(*aws, loop=None, return_exceptions=False).gather调用可能无法按预期方式工作,因为您没有传递可调用对象列表,而是将aprintl(*args)传递给*aws,并且break_print()被传递给loop参数

将行更改为下面的行,然后查看它是否按预期工作。

asyncio.gather([aprintl(*args),break_print()])
© www.soinside.com 2019 - 2024. All rights reserved.