Python 诅咒 windows 每次我 运行 程序都没有正确显示

Python Curses windows are not shown properly every time I run the program

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

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 次中有两次我 运行 程序,显示两个 windows 带边框,里面有标题消息 IP-MONITORING。这是正确的,但其余 3 种情况仅显示 self.curses_windows["border"] window 的边框,没有其他任何内容。我在刷新方面做错了什么吗?为什么结果不可预测?

感谢您的帮助。

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

for wins in self.curses_windows.values():

没有预定义的顺序。所以有时一个 window 会最后被刷新,有时另一个。如果你最后写边框,它会擦掉 "contents".