gpt4 book ai didi

python - 使用像 VRAM 这样的 python shell(使用 print 绘图)

转载 作者:太空宇宙 更新时间:2023-11-03 18:23:12 26 4
gpt4 key购买 nike

我正在尝试在 python shell 中创建一个非常基本的界面,该界面将当前信息保留在屏幕上,但也会绘制一个小“....”,其尺寸会增加,然后一旦达到某个值就重新开始尺寸。一个简单的“这个程序正在进行中”类型的事情。

我有一些可行的方法,将屏幕保存在字符串列表中,然后逐行打印元素(因此标题中的 vram 引用),但屏幕偶尔会闪烁。这只是以这种方式使用打印的限制吗?有没有办法减少眨眼(也许可以少画画)?有没有更好的方法来做到这一点,而不需要学习制作完整的 GUI?

这是我在 stackoverflow 上提出的第一个问题,所以如果我违反了任何提问禁忌,请原谅我。

编辑:我刚刚意识到我可以通过仅在需要使点消失时清除屏幕来保存重绘,然后添加一个“。”否则到我当前打印的屏幕。还有其他解决办法吗?

示例代码如下:

import os
import time
ts = 0
n = ""
m = "."
vram = ["This is the first line","This is the second line","This is the third line","This is the fourth line","This is the fifth element","This is the sixth line"]
while True:
if((time.time()-ts)>.4): #redraw every .4 seconds
os.system('cls')
if (len(n)>5):
n = "."
for item in vram:
print (item)
print ("hmmm so many lines"+n)
n+=m
ts = time.time()

最佳答案

我无法复制您的问题(我在 Linux 上尝试过),但您可以更改一些内容。

您不必要地计算每个步骤之间的时间。 time.sleep 就足够了,而且比定制的 ts 检查 更优化(更不用说也更干净)。

此外,在长度检查中,不要重新创建字符串,只需引用您之前创建的 m var。

import os
import time
n = ''
m = '.'
vram = ['This is the first line',
'This is the second line',
'This is the third line',
'This is the fourth line',
'This is the fifth element',
'This is the sixth line']
while True:
time.sleep(.4) # Sleep for.4 seconds and redraw
os.system('cls')
for item in vram:
print (item)
if len(n)>5:
n=m
print ('hmmm so many lines'+n)
n+=m

请告诉我这是否有什么不同。

关于python - 使用像 VRAM 这样的 python shell(使用 print 绘图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23719634/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com