简体   繁体   中英

Make a pinned print to the terminal with python?

How can I print to the terminal messages that will be pinned to the top or bottom on the terminal console?

Thanks !

You can use curses module to make a pinned message I found this solution in an already asked question here I believe you can add your string in the window.addstr function

import time
import curses

def pbar(window):
    height, width = window.getmaxyx()
    for i in range(10):
        window.addstr(height -1, 0, "[" + ("=" * i) + ">" + (" " * (10 - i )) + "]")
        window.refresh()
        time.sleep(0.5)

curses.wrapper(pbar)

You can get the terminal size you are running your code with help of the os module:

import os

def ptb(top_text, bottom_text):
    ts = os.get_terminal_size()
    n = ts.lines()
    os.system('clear') # Linux terminal only
    print(top_text + '\n'*(n-2) + bottom_text)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM