如何编写类型python脚本的代码

时间:2014-12-13 17:48:15

标签: python timer

我正在做一个基于菜单的程序,我想要它,所以当你打开脚本它会说。 "正在加载菜单等待.." 然后菜单将开始出现,但不是马上它会一次开始出现一个单词,好像我在那里写出来然后如果你找到我。 到目前为止我有这样的例子:

    #Imports time so i can delay things:D
import time

# This will ask the end user to input his/her choice ;D
def area():
    print("Loading Menu please wait..")
    time.sleep(2)
    print ("  _______________")
    time.sleep(1)
    print ("||               ||")
    time.sleep(1)
    print ("||Area Calculator||")
    time.sleep(1)
    print ("||     V1.0      ||")
    time.sleep(1)
    print ("||  By William F ||")
    time.sleep(1)
    print ("||_______________||")
    time.sleep(1)
    print ("Please Select A Shape")
    time.sleep(1)
    print ("1. Square")
    time.sleep(1)
    print ("2. Rectangle")
    time.sleep(1)
    print ("3. Circle")
    time.sleep(1)
    print ("4. Triangle")
    time.sleep(1)
    print ("5. Exit")
    print()
    time.sleep(1)
    return

#This will call the function to display the menu
area()

这有效,但我希望它在加载时输入自己,所以我尝试添加这个:

print ("  _", time.sleep(1),"______________")

要在每个字母之后添加,但是当我在一个字母上进行测试时,它会显示"无"睡觉的时间在哪里? 有任何想法吗? 非常感谢。非常感谢。

2 个答案:

答案 0 :(得分:2)

这是一个可以帮助你的功能:

import time
import sys


def slowshow(text, delay=0.2):
    for c in text:
        time.sleep(delay)
        sys.stdout.write(c)
    print ""

slowshow("Hello, I am Reut Sharabani!")

更有趣的一个版本是让它同步,这样你就可以在不阻止程序的情况下进行计算:

import time
import sys
import threading

# lock the sys.stdout stream when a thread is printing.
slowshow_lock = threading.Lock()

def slowshow(text, delay=0.2):
    slowshow_lock.acquire()
    for c in text:
        time.sleep(delay)
        sys.stdout.write(c)
    print ""
    slowshow_lock.release()

# they will show when needed
threading.Thread(target=slowshow, args=("Hello, I am Reut Sharabani!", )).start()
threading.Thread(target=slowshow, args=("Hello, I am Albus Dumbledore!",)).start()

# this should show immediately, even though we've sent text to printing!
print "VERY COMPLEX BACKGROUND CALCULATION"

答案 1 :(得分:0)

用它来做它"键入自己&#34 ;;这只是删除换行符:

import sys, time
sys.stdout.write(' _')
time.sleep(1)
sys.stdout.write('____________")

输出:

 _(1 second wait)_____________