如何在python中使用curses和pyfiglet

2024-05-28 19:44:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在我的显示器上面有个横幅。我对线程显示使用诅咒,如下所示:

Total Requests :      $
Successful Requests : $
Failed Requests :     $
Current Threads :     $

是否可以在总请求的顶部添加横幅。 我试过用pyfiglet,但没用

def outputSpot():
        global threads,counter,rcounter,logfile
        curses.start_color()
        curses.use_default_colors()
        banner = Figlet(font='slant')
        print(banner.renderText('Log Replay'))

        for i in range(0, curses.COLORS):
                curses.init_pair(i + 1, i, -1)
        while True:
#               stdscr.addstr(5,10, "Traffic Replay",curses.color_pair(51))

                stdscr.addstr(6,0, "File Streaming:\t{0}".format(logfile),curses.color_pair(185))
                stdscr.addstr(7,0, "Total Requests:\t\t{0}".format(counter),curses.color_pair(124))
                stdscr.addstr(8,0, "Successful Requests:\t{0}".format(rcounter),curses.color_pair(76))
                stdscr.addstr(9,0, "Failed Requests:\t{0}".format(fcounter),curses.color_pair(161))
                stdscr.addstr(10,0, "Current Threads:\t{0}  ".format(len(threads)),curses.color_pair(124))
                stdscr.refresh()

Tags: formatcountercurrentrequestscursescolortotal横幅
1条回答
网友
1楼 · 发布于 2024-05-28 19:44:12

欢迎使用堆栈溢出!你知道吗

我猜curses接管了控制台,您将无法看到通过print写入的任何内容。你知道吗

this other question推断,您的代码如下所示:

import curses
from pyfiglet import Figlet

stdscr = curses.initscr()

def outputSpot():
    global threads, counter, rcounter, logfile, stdscr

    curses.start_color()
    curses.use_default_colors()

    banner = Figlet(font="slant").renderText("Log Replay")

    for i in range(0, curses.COLORS):
        curses.init_pair(i + 1, i, -1)

    while True:
        # Iterate through the lines of the Figlet
        y = 0
        for line in banner.split("\n"):
            stdscr.addstr(y, 0, line)
            y += 1

        # You can also print the rest without repeating yourself so often ->

        # WARNING: Unless you can safely assume that the number of lines 
        # in your Figlet is 6 or less, I would leave the following line commented
        # y = 6
        for line, color in [
            ("File Streaming:\t{0}".format(logfile), curses.color_pair(185)),
            ("Total Requests:\t\t{0}".format(counter), curses.color_pair(124)),
            ("Successful Requests:\t{0}".format(rcounter), curses.color_pair(76)),
            ("Failed Requests:\t{0}".format(fcounter), curses.color_pair(161)),
            ("Current Threads:\t{0}".format(len(threads)), curses.color_pair(124)),
        ]:
            stdscr.addstr(y, 0, line, color)
            y += 1

        stdscr.refresh()

相关问题 更多 >

    热门问题