【问题标题】:Python Curses windows are not shown properly every time I run the program每次我运行程序时,Python Curses 窗口都没有正确显示
【发布时间】:2016-11-17 16:12:06
【问题描述】:

我在 Python 3.4 中遇到了一个关于 ncurses 的奇怪问题,我正在尝试将一些窗口作为我的应用程序的布局,代码如下所示:

import curses
import time
class Presentator():
    def __init__(self, device_db, address_db):
        self.curses_windows = {}
    def update_all_windows(self):
        for wins in self.curses_windows.values():
            wins.noutrefresh()
        curses.doupdate()

    def update_all_windows(self):
        for wins in self.curses_windows.values():
            wins.noutrefresh()
        curses.doupdate()

    def create_windows(self):
        max_height = curses.LINES - 5
        max_width = curses.COLS - 5
        self.curses_windows["border"] = curses.newwin(max_height, max_width, 0, 0)
        self.curses_windows["border"].border()
        self.curses_windows["title"] = curses.newwin(3, max_width, 0, 0)
        self.curses_windows["title"].border()
        title_msg = "IP MONITORING"
        self.curses_windows["title"].addstr(1, max_width // 2 - len(title_msg), title_msg)


    def print_results(self, stdscr):
        self.create_windows()
        while True:
            self.update_all_windows()
            time.sleep(5)


    def start_ui(self):
        curses.wrapper(self.print_results)

if __name__ == "__main__":
    p = Presentator()
    p.start_ui()

在我运行程序的 5 次中,有两次显示了两个带边框的窗口,其中包含标题消息 IP-MONITORING。但是这是正确的,其余 3 种情况仅显示 self.curses_windows["border"] 窗口的边框,没有其他内容。我在刷新方面做错了吗?为什么结果不可预测?

感谢您的帮助。

【问题讨论】:

  • 可能是因为窗口列表是无序的(关联数组的常见情况)

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


【解决方案1】:

当你使用非数字数组索引时,它实际上是一个关联数组(在python中也称为字典),并且迭代

for wins in self.curses_windows.values():

没有预定义的顺序。所以有时一个窗口会最后刷新,有时另一个。如果你最后写边框,它会抹去“内容”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2015-07-15
    • 1970-01-01
    • 2022-08-17
    • 2021-12-08
    相关资源
    最近更新 更多