【问题标题】:Print a fancy string and update it after it's printed to simulate a GUI打印一个花哨的字符串并在打印后更新它以模拟 GUI
【发布时间】:2013-03-29 11:43:47
【问题描述】:

我不知道这是否可以做到,但我确定我在某些脚本中看到了这一点,这就是我问的原因。

我需要像这样打印一个字符串:

++++++++++++++++++++++++++++++     +++++++++++++++
+     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的

标签: python string printing command-line-interface curses


【解决方案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)

您可以跟踪打印的行数,而不是使用“home”序列,并在打印下一次更新之前打印那么多“cursor up”转义序列。

如果您想变得更高级,请查看 python curses moduleblessingsurwid

我认为这些都不能在常规的 Windows 终端中工作;这就是我询问您所针对的操作系统的原因。

【讨论】:

  • 基本上可以,但我无法在同一行打印!我正在用我的诅咒代码更新我的问题。
【解决方案2】:

您可以打印出许多换行符,使其看起来像屏幕已被清除。此外,它易于实现且有助于调试。

代码大概是这样的:

print "\n"*100

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 2020-01-03
    • 2021-02-24
    相关资源
    最近更新 更多