我不知道這是否可以完成,但我確定我在某些腳本中看到了這一點,這就是爲什麼我要問。打印一個奇特的字符串,並在它打印後更新它來模擬一個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()
在什麼操作系統,你運行該代碼計劃? –
Backbox,基於Ubuntu – StepTNT