2017-01-23 82 views
0

我正在尋找使用python做文本動畫的類型。 如果你已經使用Metasploit的,在加載過程中的文本的浪潮出現 例子: 首先 第一 第一 第一 第一 (這一切將在同一條線路) 我用sys.stdout的思維的東西.write(「」),但我不能替換 我放下的最後一個符號,它是大寫後的小寫字母。Python大寫和非大寫動畫

# This version is fixed and works 
import sys, time 
lowerstr = "buffering" 
upperstr = lowerstr.upper() 
for x in range(len(lowerstr)): 
    s = '\r' + lowerstr[0:x] + upperstr[x] + lowerstr[x+1:] + '\r' 
    sys.stdout.write(s) 
    sys.stdout.flush() 
    time.sleep(0.1) 
+0

這似乎是你身邊正在尋找什麼http://stackoverflow.com/questions/2122385/dynamic-terminal-printing-with- python – nerdlyist

+0

我改變了下面的腳本,並添加了「\ r」來乞求並修復它,以便它可以做我想要的,謝謝你們的幫助! s ='\ r'+ lowerstr [0:x] + upperstr [x] + lowerstr [x + 1:] +'\ r' – RoNAPC

+0

更新的更改問題!在最後添加修改。現在沒有人會知道代碼中的oryginal代碼和oryginal問題是什麼。 – furas

回答

1
import sys 
import time 

data = ["b","u","f","f","e","r","i","n","g"] 

# display with one upper char 

for x in range(len(data)): 
    # remeber lower char 
    old = data[x] 

    # replace with upper char 
    data[x] = old.upper() 

    # create full text 
    text = "".join(data) 

    # display full text 
    sys.stdout.write("\r") 
    sys.stdout.write(text) 
    sys.stdout.flush() 

    # put back lower char 
    data[x] = old 

    time.sleep(1) 

# display without upper chars at the end 

text = "".join(data) 

sys.stdout.write("\r") 
sys.stdout.write(text) 
sys.stdout.flush() 

如果你把多餘的字符在隨後data結束不具備高版本 - 即。空字符串"" - 然後在for循環後不需要代碼。

您可以在開始時也放置空字符串以顯示沒有上字符的第一個文本。

import sys 
import time 

# text with extra chars at the start and at the end 
data = ["", "b","u","f","f","e","r","i","n","g", ""] 

# display with one upper char 

for x in range(len(data)): 
    # remeber lower char 
    old = data[x] 

    # replace with upper char 
    data[x] = old.upper() 

    # create full text 
    text = "".join(data) 

    # display full text 
    sys.stdout.write("\r") 
    sys.stdout.write(text) 
    sys.stdout.flush() 

    # put back lower char 
    data[x] = old 

    time.sleep(1) 
0

這個版本結合了字符串,而不是僅僅遍歷每個字符。您可以通過在相應的字符上使用.upper()完全消除upperstr。請注意,不應將list用作變量名稱,因爲它是Python內置的。

import sys 
import time 

lowerstr = "buffering" 
upperstr = lowerstr.upper() 
for x in range (len(lowerstr)): 
    s = lowerstr[0:x] + upperstr[x] + lowerstr[x+1:] + '\r' 
    sys.stdout.write(s) 
    sys.stdout.flush() 
    time.sleep(1) 
0

實現使用的是iOS pythonista現場這個動畫

import scene 

label_text = 'abcdefghijklmnopqrstuvwxyz' 

def custom_action(node, progress): 
    ntext = node.text 
    i = int(len(ntext)*progress) 
    iminus1txt = ntext[i-1].lower() if i > 0 else '' 
    ithtxt = ntext[i].capitalize() if i < len(ntext) else '' 
    if i > 0: 
     before_i = ntext[:(i-1)] 
    else: 
     before_i = '' 
    if i < len(ntext): 
     node.text = before_i+iminus1txt+ithtxt+ntext[(i+1):] 
    else: 
     node.text = label_text 

class TextCapitalAnimate(scene.Scene): 
    def setup(self): 
     center = self.bounds.center() 
     labelnode = scene.LabelNode(label_text, font=('Courier', 20), 
           position=center, parent=self) 
     A = scene.Action 
     animate_action = A.repeat(A.call(custom_action, 2), 0) 
     labelnode.run_action(animate_action) 

scene.run(TextCapitalAnimate())