2015-02-04 24 views
1

如何更新屏幕或同時等待密鑰?我使用Python的unicurses,但我想我會在C. 同樣的問題,這是我想要的僞代碼來完成:使用curses,我如何更新屏幕或等待密鑰?

function startScreen(){ 
    stdscr = initscr() 
    while True{ 
     - Update screen using a variable that is constantly changing (probably by a thread, right?) 
     - Get a key with getch() - to close or interact with the screen 
    } 
} 

我的問題是,屏幕不更新,除非有事如調整屏幕大小或按下某個鍵。我正在考慮使用while循環(和time.sleep(1)?)來更新屏幕和一個線程來等待鍵。那可能嗎?我不太瞭解線程,這就是爲什麼我問。有一種更簡單的方法嗎?

謝謝。

回答

2

這可以在沒有任何複雜的多線程的情況下完成。有一個功能curses.halfdelay,也可以在您使用的unicurses庫中找到。它需要等待十分之一秒才能繼續。 https://docs.python.org/3/library/curses.html#curses.halfdelay

下面是一個示例代碼,每半秒刷新一次,除非有一個按鈕被按下,在這種情況下,它會立即更新。

import curses 
scr = curses.initscr() 
curses.halfdelay(5)   # How many tenths of a second are waited, from 1 to 255 
curses.noecho()    # Wont print the input 
while True: 
    char = scr.getch()  # This blocks (waits) until the time has elapsed, 
           # or there is input to be handled 
    scr.clear()    # Clears the screen 
    if char != curses.ERR: # This is true if the user pressed something 
     scr.addstr(0, 0, chr(char)) 
    else: 
     scr.addstr(0, 0, "Waiting") 
+0

完美!非常感謝你! – SadSeven