【问题标题】:Python diffent line display different print outputs asynchronouslyPython不同行异步显示不同的打印输出
【发布时间】:2019-07-12 05:13:29
【问题描述】:

我想知道如何在一个控制台的不同显示区域打印不同的值。

例如:

Hello, world! # 100 times
---------------------------------------------------------------------
Hi, world! # 20 times

如何使输出控制台显示为这样?好像我有两个屏幕,每个屏幕都显示一个输出。 这两个部分同时显示在此控制台中。但是会在不同的时间更新自己的值。输出始终显示在屏幕上。

【问题讨论】:

    标签: python-3.x printing


    【解决方案1】:

    您可以使用curses 模块:Curses Programming with Python

    一个例子:

    #!/usr/bin/env python3
    
    import curses
    
    stdscr = curses.initscr()
    curses.noecho()
    curses.cbreak()
    stdscr.keypad(True)
    
    stdscr.refresh()
    begin_x = 0; begin_y = 0
    height = 10; width = 80;
    
    win1 = curses.newwin(height, width, begin_y, begin_x)
    win2 = curses.newwin(height, width, begin_y + 11, begin_x)
    
    for i in range(1,9):
        win1.addstr(i, 0, "Hello {0}\n".format(i))
        win1.refresh()
    
    for i in range(1,9):
        win2.addstr(i, 0, "World {0}\n".format(i))
        win2.refresh()
    
    while True:
        c = stdscr.getch()
        if c == ord('q'):
            break
    
    stdscr.keypad(False)
    curses.nocbreak()
    curses.echo()
    curses.endwin()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-02
      • 1970-01-01
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多