19

It's a strange and silly question I know, but I'm curious because I don't know that much about python and how it works. From the terminal or when you use IDLE, is there any way to print a string at a certain screen position?

I'll try to explain this better: Do you remember the old days when you used to make small programs in Basic, maybe on a Commodore 64, Apple II or ZX Spectrum? During that days if you wanted to print a string at a certain position you used to write something like this:

10 LOCATE 30, 40 : PRINT "hello world"

I'm just curious to know if there's any way to tell python to print a string at a certain position, and if there's a way to know how many columns and how many rows can be actually displayed inside the IDLE window.

I've also made a mockup draw, to explain this concept.

Mockup screen to explain what I mean

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Cesco
  • 3,770
  • 7
  • 24
  • 26
  • 4
    If you want anything this fancy, let go of IDLE and embrace curses. Or re-implement half of it yourself, which is quite some pain. –  Sep 12 '11 at 19:19
  • 1
    If you are going to use curses, try the blessed it has loads of easy functions to perform tasks the same tasks https://pypi.org/project/blessed/ (Note this comment is not for the original poster of the question but people ending up here after a google search) – Coenie Richards May 20 '20 at 15:59

4 Answers4

33

I don't know if this works on IDLE, but it does in any normal terminal:

import sys
def print_there(x, y, text):
     sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (x, y, text))
     sys.stdout.flush()

This uses Ansi-Escape Sequences

rumpel
  • 7,870
  • 2
  • 38
  • 39
  • @Caltor: Follow the link above which explains Ansi Escape Sequences. These are interpreted by the terminal software. – rumpel Nov 04 '13 at 19:54
  • 2
    Thanks, just what I was looking for. Although I had to reverse x and y. – Aaron R. Mar 24 '18 at 17:53
  • 2
    I got this instead, doesn't it work for window console? >>> print_there(0,0,'hello') ←7←[0;0fhello←8>>> – user3431399 Nov 07 '18 at 06:52
  • The link to Ansi-Escape Sequences page does not work anymore. You can use this one instead: https://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html – eNca Dec 10 '22 at 21:21
1

To avoid the issue raised by @user3431399, you first need to make win32 console recognize ANSI/VT100 escape sequences. I got the same problem as @user3431399 on my Windows 10 terminal and I solved it by following the solution recommended by @Daniel De Léon. That is, I logged in as administrator at the windows prompt (cmd command). Then I copied, pasted, and ran the command.

REG ADD HKCU\CONSOLE /f /v VirtualTerminalLevel /t REG_DWORD /d 1

pmntang
  • 21
  • 5
1

Use the cursor position ANSI escape code.

print("\033[{0};{1}H{2}".format(30, 40, "Hello world"))

or

print("\033[{0};{1}f{2}".format(30, 40, "Hello world"))
J_H
  • 17,926
  • 4
  • 24
  • 44
-1

Use Tkinter to make it perfect by select under frame and provide row and column number to display your output

Ehsan Rahi
  • 61
  • 7