17

EDIT: I just discovered that it's possible to obtain a similar behaviour by using the standard library "curses". There are some demonstrations about how it works here and there, for example on YouTube: http://www.youtube.com/watch?v=Bj-H9uPEa5U

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

Nimantha
  • 5,793
  • 5
  • 23
  • 56
Cesco
  • 3,340
  • 7
  • 22
  • 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
  • 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

29

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,336
  • 2
  • 37
  • 39
1

This question only has one real answer and it isn't a very good one. The method:

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

Isn't perfect. I'd recommend staying clear of doing things like this in the terminal. If you want to do Gui's and stuff use Pygame, Tkinter, or Django.

Draxdo
  • 15
  • 3
0

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
  • 11
  • 4
-1

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

Ehsan Rahi
  • 65
  • 7