【问题标题】:Python ncurses template, tablePython ncurses 模板、表格
【发布时间】:2013-08-16 01:51:54
【问题描述】:

我正在编写一个基于状态机的 python 程序,该程序当前将机器的状态打印到控制台。这作为日志很有用,但不是最用户友好的界面。

我很想知道是否有在 Python 中使用 ncurses 的好例子,最好是在 OOP 中在表格中呈现变化信息(例如:状态信息)的东西。

已经尝试过python中的curses包,但是它不像我想要的那样OO。

这是流当前的样子:

manager:    st_machine_01    state INITIALISE
manager:    st_machine_01    state GET_LIST_PAGES
manager:    st_machine_02    state EXIT
manager:    st_machine_03    state INITIALISE
manager:    st_machine_03    state GET_LIST_PAGES
manager:    st_machine_04    state EXIT
manager:    st_machine_05    state INITIALISE
manager:    st_machine_05    state GET_LIST_PAGES
manager:    st_machine_01    state GET_LIST_PAGES
manager:    st_machine_05    state GET_LIST_PAGES
manager:    st_machine_05    state EXIT
manager:    st_machine_01    state GET_LIST_PAGES
manager:    st_machine_06    state INITIALISE
manager:    st_machine_06    state GET_LIST_PAGES
manager:    st_machine_01    state GET_LIST_PAGES
manager:    st_machine_06    state GET_LIST_PAGES
manager:    st_machine_01    state EXIT
manager:    st_machine_06    state GET_LIST_PAGES
manager:    st_machine_07    state INITIALISE
manager:    st_machine_07    state GET_LIST_PAGES
manager:    st_machine_06    state GET_LIST_PAGES
manager:    st_machine_06    state EXIT

而在上述流的最后一点呈现的 ncurses “表格”看起来像:

manager:    st_machine_01    state EXIT
manager:    st_machine_02    state EXIT
manager:    st_machine_03    state GET_LIST_PAGES
manager:    st_machine_04    state EXIT
manager:    st_machine_05    state EXIT
manager:    st_machine_06    state EXIT
manager:    st_machine_07    state GET_LIST_PAGES

我正在寻求在 Python 2.7 64 位、Windows 7 64 位上执行此操作。

【问题讨论】:

  • 机器正常运行时看起来还不错。如果您将显示限制为仅 7 行和一个状态或 cmd 行,您可以在状态更改时短暂闪烁屏幕。如果您快速重新打印新状态,它看起来会很酷。你可以在最后一行添加一个日志条目,st_machine_05 changed to Get_List_Pages from state Exit...

标签: python ncurses state-machine


【解决方案1】:

如果curses 还不够(由于某些未描述的原因),pycdkurwid 可能更适合您的需求。

【讨论】:

    【解决方案2】:

    您可以使用标准的curses 模块。你可以找到它的文档here

    【讨论】:

    • curses 包没有我想要的那么完整。
    • 你能在你的问题中添加额外的条件吗?谢谢。
    【解决方案3】:

    .

    ncurses 包不是非常面向对象的,但是,您肯定会从使用面向对象的方法中受益。

    class screen {
      class rectangle {
        class line { 
          string default_text="please enter a command (press 'e' to list available events) >> ";
        }
      }
    }
    
    sm_window = new screen("sc1").new rectangle( "log_rect", lines=7 );
    log_rect.line[7] = "st_machine_05 changed to Get_List_Pages from state Exit";
    

    如果您将所有对 ncurses 包的访问权限保留在这几个类中,那么您将比在现有应用程序中使用 ncurses 调用更进一步。

    如果您由于某种原因没有将所有字段设为私有,则可以直接从屏幕类访问所有必需的程序数据。

    【讨论】:

    • 我以前在 Python 中使用过 ncurses,正如你所说,它到处都被 ncurses 调用“加注”,因此我想去 OO 并希望得到一些事件驱动的东西(比如GUI 框架)。
    • 我正在考虑学习一些python,并认为我可以为您提供一些帮助,
    猜你喜欢
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多