【问题标题】:How to easily display multiple columns in the console with curses?如何使用诅咒轻松在控制台中显示多列?
【发布时间】:2016-11-18 16:37:29
【问题描述】:

我想在控制台中使用 curses 在多列中打印数据。有什么简单的方法可以做到这一点?

目前,我的应用程序打印如下信息:

import curses
(...)
# create default screen
std_scr = curses.initscr()
(...)
# example
for line in lines:
    std_scr.addstr(line + "\n")
std_scr.refresh()

这会产生以下输出

line 1
line 2
...
line n

但是,我想要的是这样的:

columns = 3
for line in lines:
   # easy way to separate columns

这应该导致:

line 1         | line 11          | line 21
line 2         | line 12          | line 22
...            | ...              | ...
line 10        | line 20          | line 30

有什么简单的方法可以做到这一点?我是否应该简单地使用迭代器按line 1line 11line 21line 2line 12line 22line 3 等顺序进行迭代,同时使用line.ljust(30, " ") 到得到空格?

或者我应该在 curses 中创建多个屏幕?

还有其他更简单的解决方案吗?

我对 curses 很陌生,因此任何一般性建议 + 示例都会很棒。

【问题讨论】:

    标签: python python-3.x multiple-columns ncurses python-curses


    【解决方案1】:

    我会使用std_scr.getmaxyx 获得屏幕尺寸,然后将屏幕宽度除以列数。此外,从迭代器的限制中获取每列中的行数。使用 addnstr 打印消息(并根据示例中的列数和行数计算每次调用 addnstr 的 y,x 坐标对)。

    【讨论】:

      猜你喜欢
      • 2013-02-28
      • 2012-02-23
      • 2021-06-27
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      • 2022-07-07
      • 2017-12-20
      相关资源
      最近更新 更多