Goto XY

Discussion in 'Python' started by ale.of.ginger, Nov 9, 2005.

  1. Is there some command in python so that I can read a key's input and
    then use a gotoxy() function to move the cursor on screen? e.g.:
    (psuedo-code)

    When the right arrow is pushed, cursor gotoxy(x+1,y)

    Thanks.
     
    ale.of.ginger, Nov 9, 2005
    #1
    1. Advertisements

  2. ale.of.ginger

    David Wahler Guest

    On Unix-like platforms, this functionality is provided by the standard
    curses module. A similar module for Windows appears to be available at
    http://www.effbot.org/zone/console-index.htm but I haven't tested it
    out.

    -- David
     
    David Wahler, Nov 9, 2005
    #2
    1. Advertisements

  3. ale.of.ginger

    Mike Meyer Guest

    You want curses. A version is included in the standard library if
    you're on Unix. If you're on Windows, there are third party curses
    libraries. You should be able to install that and then build the
    curses module against it. If you're not on either of those two - tell
    us what you're using, and maybe someone who knows that system will
    answer you.

    <mike
     
    Mike Meyer, Nov 9, 2005
    #3
  4. ale.of.ginger

    jmdeschamps Guest

    jmdeschamps, Nov 9, 2005
    #4
  5. Thanks -- I downloaded WConio.

    When I just tried it out in the IDLE, it said:

    NameError: name 'WConio' is not defined

    I assume I have to use a header somewhere (import WConio ?). Or is
    there something I'm missing (I downloaded the Python 2.4 (I have 2.4.2)
    auto installer and it ran fine...)
     
    ale.of.ginger, Nov 9, 2005
    #5
  6. ale.of.ginger

    David Wahler Guest

    If you had tried it, you would have discovered that "import WConio" is
    exactly what you need. Don't be afraid to experiment!

    -- David
     
    David Wahler, Nov 9, 2005
    #6
  7. ale.of.ginger

    jmdeschamps Guest

    Like David said below, you need to import WConio but then repemeber
    when you *import someLib* you have to use qualified names such as
    WConio.getkey()

    Example:
    import WConio

    s=WConio.getkey()

    if s == "right":
    WConio.gotoxy(10,10)
    WConio.putch("W")

    s=WConio.getch()
     
    jmdeschamps, Nov 9, 2005
    #7
  8. ale.of.ginger

    jmdeschamps Guest

    Like David said above... ;-)
     
    jmdeschamps, Nov 9, 2005
    #8
  9. You can use curses, but that may be more trouble than it's worth.

    If you don't mind limiting your program to an ANSI-type terminals
    (vt100, xterm, rxvt, linux, putty, etc....), then you can just use
    the codes to position the cursor:

    ESC = '\033'
    CSI = ESC + "["

    def printat(row,col,arg=""):
    sys.stdout.write( CSI + str(row) + ";" + str(col) + 'H' + str(arg))
    To read a single keystroke, see Claudio Grondi's post in the
    thread "python without OO" from last January.

    Function and cursor keys return more than a single character, so
    more work is required to decode them. The principle is outlined in
    <http://www.unixreview.com/documents/s=9920/ur0511a/ur0511a.html>;
    the code there is for the shell, but translating them to python
    should be straightforward. I'll probably do it myself when I have
    the time or the motivation.
     
    Chris F.A. Johnson, Nov 9, 2005
    #9
  10. In which case you could always use tput -- and it will still be
    portable.
     
    Grant Edwards, Nov 9, 2005
    #10
  11. More or less; probably much less.
     
    Chris F.A. Johnson, Nov 9, 2005
    #11
  12. Not necessarily. There are differences in tput from system to
    system, not to mention how much it slows things down to call it for
    every sequence you need.

    Then there are some old systems which don't have tput.
     
    Chris F.A. Johnson, Nov 9, 2005
    #12
  13. OK - I added the import WConio line. But when I run

    import WConio
    print "going to x10,y10..."
    WConio.gotoxy(10,10)
    print "Done"

    the above, I get the following error:

    WConio.gotoxy(10,10)
    error: GetConOut Failed

    I installed the WConio to the ../site-packages/ folder in Python24, and
    when it didn't work I also moved the files in there to the /Lib/ folder
    where other things are like random, but that didn't seem to work either.
     
    ale.of.ginger, Nov 9, 2005
    #13
  14. [ale.of.ginger]
    Are you running at a Windows Command Prompt, or in an IDE? As I understand
    it, WConio will only work in a Windows Command Prompt.
     
    Richie Hindle, Nov 9, 2005
    #14
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.