Giả sử bạn có mã này, và bạn chỉ muốn biết làm thế nào để thực hiện draw()
:
def draw(window, string):
window.addstr(0, 0, string)
window.refresh()
draw(window, 'abcd')
draw(window, 'xyz') # oops! prints "xyzd"!
Các đơn giản nhất và "lời nguyền-ish "giải pháp chắc chắn là
def draw(window, string):
window.erase() # erase the old contents of the window
window.addstr(0, 0, string)
window.refresh()
Bạn có thể bị cám dỗ để viết những dòng này thay vì:
def draw(window, string):
window.clear() # zap the whole screen
window.addstr(0, 0, string)
window.refresh()
Nhưng đừng! Mặc dù tên thân thiện, clear()
thực sự chỉ dành cho when you want the entire screen to get redrawn unconditionally, tức là "nhấp nháy". Các chức năng erase()
làm điều đúng mà không nhấp nháy.
Frédéric Hamidi cung cấp các giải pháp sau đây để xóa đi chỉ là một phần (s) của cửa sổ hiện hành:
def draw(window, string):
window.addstr(0, 0, string)
window.clrtoeol() # clear the rest of the line
window.refresh()
def draw(window, string):
window.addstr(0, 0, string)
window.clrtobot() # clear the rest of the line AND the lines below this line
window.refresh()
Một ngắn hơn và tinh khiết-Python thay thế sẽ được
def draw(window, string):
window.addstr(0, 0, '%-10s' % string) # overwrite the old stuff with spaces
window.refresh()
Nó phải được thực hiện trước 'refresh' hoặc sau? – Pablo
Trước 'refresh()' và sau 'addstr()' (tất cả các thao tác này chỉ cập nhật màn hình "ảo" curses cho đến khi 'refresh()' được gọi). –