2013-03-29 35 views
1

我不知道這是否可以完成,但我確定我在某些腳本中看到了這一點,這就是爲什麼我要問。打印一個奇特的字符串,並在它打印後更新它來模擬一個GUI

我需要打印像這樣的字符串:

++++++++++++++++++++++++++++++  +++++++++++++++ 
+  A 1  ++  A 2  +  +  A n  + 
+-------------++-------------+  +-------------+ 
+  B 1  ++  B 2  + ... +  B n  + 
+-------------++-------------+  +-------------+ 
+  C 1  ++  C 2  +  +  C n  + 
++++++++++++++++++++++++++++++  +++++++++++++++ 

其中n是列數,它依賴於用戶的輸入。 A行是固定的,而B和C必須在程序運行時更改。

所以,首先我需要一種方法來打印這種字符串,知道A和B是長度爲8的字符串,但C從8到1個字符。

我看了一下各種「格式化程序」解決方案和ppretty,但他們似乎離我所需要的太遠了(我沒有找到很多示例!)。 (我只是試着ppretty因爲其他的解決方案需要像一個數據源,而我得到我的數據與class.getData()因爲我從Java來了!)現在

,打印該字符串後,我想它隨着B和C的更改而更新,而不再打印,以避免大量打印內容,並使所有內容更加整潔,更易於閱讀。

有沒有辦法做到這一點?

編輯:

這是我已經試過(沒有成功)

def printCrackingState(threads): 
info_string = ''' 
++++++++++++++++++++++++++++++++++ 
+ Starting password = s.%08d + 
+--------------------------------+  
+ Current pin  = s.%08d + 
++++++++++++++++++++++++++++++++++ 
+ Missing pins  = %08d + 
++++++++++++++++++++++++++++++++++ 
       ''' 
while 1: 
    for t in threads: 
     printed_string = info_string % (t.starting_pin, t.testing_pin, t.getMissingPinsCount()) 
     sys.stdout.write(printed_string) 
    time.sleep(3500) 

,這是結果:

++++++++++++++++++++++++++++++++++ 
+ Starting password = s.00000000 + 
+--------------------------------+  
+ Current pin  = 00000523 + 
++++++++++++++++++++++++++++++++++ 
+ Missing pins  = 01249477 + 
++++++++++++++++++++++++++++++++++ 

++++++++++++++++++++++++++++++++++ 
+ Starting password = s.01250000 + 
+--------------------------------+  
+ Current pin  = 01250491 + 
++++++++++++++++++++++++++++++++++ 
+ Missing pins  = 01249509 + 
++++++++++++++++++++++++++++++++++ 

++++++++++++++++++++++++++++++++++ 
+ Starting password = s.02500000 + 
+--------------------------------+  
+ Current pin  = 02500465 + 
++++++++++++++++++++++++++++++++++ 
+ Missing pins  = 01249535 + 
++++++++++++++++++++++++++++++++++ 

++++++++++++++++++++++++++++++++++ 
+ Starting password = s.03750000 + 
+--------------------------------+  
+ Current pin  = 03750564 + 
++++++++++++++++++++++++++++++++++ 
+ Missing pins  = 01249436 + 
++++++++++++++++++++++++++++++++++ 

++++++++++++++++++++++++++++++++++ 
+ Starting password = s.05000000 + 
+--------------------------------+  
+ Current pin  = 05000592 + 
++++++++++++++++++++++++++++++++++ 
+ Missing pins  = 01249408 + 
++++++++++++++++++++++++++++++++++ 

++++++++++++++++++++++++++++++++++ 
+ Starting password = s.06250000 + 
+--------------------------------+  
+ Current pin  = 06250579 + 
++++++++++++++++++++++++++++++++++ 
+ Missing pins  = 01249421 + 
++++++++++++++++++++++++++++++++++ 

++++++++++++++++++++++++++++++++++ 
+ Starting password = s.07500000 + 
+--------------------------------+  
+ Current pin  = 07500577 + 
++++++++++++++++++++++++++++++++++ 
+ Missing pins  = 01249423 + 
++++++++++++++++++++++++++++++++++ 

++++++++++++++++++++++++++++++++++ 
+ Starting password = s.08750000 + 
+--------------------------------+  
+ Current pin  = 08750555 + 
++++++++++++++++++++++++++++++++++ 
+ Missing pins  = 01249445 + 
++++++++++++++++++++++++++++++++++ 

我用sys.stdout.write()有他們在同一行,但它不起作用!

編輯2:

我的第二個學嘗試與詛咒,在答覆建議。

我可以在同一行寫東西,但不會更新。

這裏是我的代碼:

import curses 
import time 
import threading 

class CursesPrinter(threading.Thread): 
    windows = [] 
    screen = None 
    processes = [] 

    info_string = ''' 
    +++++++++++++++++++++++++ 
    + Starting = s.%08d + 
    +-----------------------+  
    + Current = s.%08d + 
    +-----------------------+ 
    + Missing = %08d + 
    +++++++++++++++++++++++++ 
        '''    

    def _makeWindows(self, numWindows): 
     x = 0 
     y = 0 
     height = 15 
     width = 30   
     for i in range(numWindows): 
      win = curses.newwin(height, width, x, y) 
      #win.border(1) 
      y+=width 
      if y>self.screen.getmaxyx(): 
       #This should make a new line if we reach the end of the terminal 
       y = 0 
       x+=height 
      self.windows.append(win) 

    def run(self): 
     while 1: 
      for i in range(len(self.processes)-1): 
       print_string = self.info_string % (self.processes[i].starting_pin, self.processes[i].testing_pin, self.processes[i].getMissingPinsCount())     
       self.windows[i].addstr(0,0, print_string) 
       self.windows[i].refresh() 
     #time.sleep(60)        


    def __init__(self, threads, processes): 
     super(CursesPrinter, self).__init__() 
     self.screen = curses.initscr() 
     curses.curs_set(0) 
     self.processes = processes 
     self._makeWindows(threads)  
     #curses.endwin() 
+0

在什麼操作系統,你運行該代碼計劃? –

+0

Backbox,基於Ubuntu – StepTNT

回答

1

,依賴於你的終端處理VT100 escape sequences是清除屏幕,然後打印輸出前,將光標移動到起始位置的快速和骯髒的方法。

一個快速演示:

import sys 
import time 
import random 

ESC = "\x1b" 

def home(): 
    sys.stdout.write(ESC + '[0;0H') 
    sys.stdout.flush() 

def cls(): 
    sys.stdout.write(ESC + '[2J') 
    sys.stdout.flush() 

def print_status(): 
    w = 8 
    print '+' + '-'*w + '+' 
    for row in range(3): 
     fmt = '|%%%dd |' % (w - 1) 
     print fmt % random.randint(0, 1000) 
     print '+' + '-'*w + '+' 
    sys.stdout.flush() 

if __name__ == "__main__": 
    cls() 
    for k in range(16, 0, -1): 
     home() # or use cls() 
     print_status() 
     time.sleep(0.5) 

而不是使用「家」的順序,你可以跟蹤你打印多少行,並打印了許多「光標」轉義序列打印下一次更新之前。

如果你想獲得更多發現,請查看python curses moduleblessingsurwid

我不認爲任何這些工作在一個正常的Windows終端;這就是爲什麼我詢問你所針對的操作系統的原因。

+0

基本上它可以工作,但我無法在同一行上打印! 我用我的curses代碼更新我的問題。 – StepTNT

0

你可以打印出很多新的行字符,使它看起來像屏幕已被清除。它的易於實現和調試也很有幫助。

的代碼將是一些事情,如:

print "\n"*100