2013-03-29 44 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