首页 > 解决方案 > Python 诅咒如何用 curses.setsyx(y,x) 改变光标位置?

问题描述

我正在学习如何使用诅咒,我正在尝试创建一个简单的程序来开始。我希望这个程序总是在左上角打印 aq,然后它会在 1,1 中打印最近按下的字符。但我不确定如何设置光标的位置,应该使用 curses.setsyx(y,x) 函数来更改它,但它不起作用。这是我的程序:

import curses
import sys,os

def screen_init(stdscr):
    stdscr.clear()
    stdscr.refresh()
    stdscr.scrollok(1)

    height, width = stdscr.getmaxyx()

    curses.start_color()
    curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)

    while True:
            stdscr.addstr(0, 0, "q", curses.color_pair(1))
            key = stdscr.getch()
            stdscr.addstr(1, 1, chr(int(key)), curses.color_pair(1))
            if key == ord('q'):
                    break
            elif key == ord('p'):
                    curses.setsyx(3, 3)
                    curs_y, curs_x = curses.getsyx()
                    curses.doupdate()
                    stdscr.addstr(1, 1, "clicked! " + str(curs_x) + " " + 
str(curs_y), curses.color_pair(1))

    stdscr.refresh()
    curses.endwin()

def main():
    curses.wrapper(screen_init)

if (__name__ == "__main__"):
    main();

关于它为什么不做任何事情的任何想法?当我使用 getyx() 它得到 3, 3 但光标的真实位置没有改变

标签: pythonpython-3.xcurses

解决方案


你可以使用stdscr.getyx()andstdscr.move(y, x)代替。


推荐阅读