【问题标题】:Displaying different strings based on input根据输入显示不同的字符串
【发布时间】:2018-06-23 21:29:13
【问题描述】:

我一直试图让这个脚本按预期工作,但在这样做时遇到了一些问题。我正在尝试根据变量 pageCount 更改屏幕上显示的字符串,但到目前为止遇到了 2 个问题。

我希望在 getkey() 等待的击键之前打印第一个字符串 (page1),但我似乎无法实现这一点。当屏幕上打印新字符串时,我也无法正确刷新/清除屏幕。

解决这些问题的最佳方法是什么?

from curses import wrapper


def main(stdscr):
# Clear screen


pageCount=0
#stdscr.addstr(str(pageCount))
stdscr.clear()
while True:

    key=stdscr.getkey()






    if key == "KEY_LEFT":

        pageCount=pageCount-1
    if key == "KEY_RIGHT":
        pageCount=pageCount+1

    if pageCount < 1:
        pageCount=10
    if pageCount > 10:
        pageCount=1

    if pageCount==1:
        stdscr.addstr("page 1")

    if pageCount==2:
        stdscr.addstr("page 2")

    if pageCount==3:
        stdscr.addstr("page 3")
    if pageCount==4:
        stdscr.addstr("page 4")
    if pageCount==5:
        stdscr.addstr("page 5")
    if pageCount==6:
        stdscr.addstr("page 6")
    if pageCount==7:
        stdscr.addstr("page 7")
    if pageCount==8:
        stdscr.addstr("page 8")
    if pageCount==9:
        stdscr.addstr("page 9")
    if pageCount==10:
        stdscr.addstr("page 10")

    stdscr.refresh()




wrapper(main)

【问题讨论】:

    标签: python curses python-curses


    【解决方案1】:

    只需在 getkey() 之前打印页面名称即可解决您的问题。您可以使用您的页面作为字符串中的变量来真正简化您的代码,以避免使用该套件。像这样?

    from curses import wrapper
    
    def main(stdscr):
        # Clear screen
        pageCount=1
        #stdscr.addstr(str(pageCount))
        stdscr.clear()
        while True:
    
            stdscr.addstr("page %d"%(pageCount))
    
            key=stdscr.getkey()
            if key == "KEY_LEFT":
                pageCount=pageCount-1
            if key == "KEY_RIGHT":
                pageCount=pageCount+1
            if pageCount < 1:
                pageCount=10
            if pageCount > 10:
                pageCount=1
    
            stdscr.refresh()
    
    wrapper(main)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 2019-02-21
      • 1970-01-01
      • 2023-01-27
      • 1970-01-01
      • 2013-07-24
      相关资源
      最近更新 更多