Is it possible to print a string at a certain screen position inside IDLE?
Asked Answered
D

4

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

Dentalium answered 12/9, 2011 at 19:14 Comment(2)
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.Septennial
If you are going to use curses, try the blessed it has loads of easy functions to perform tasks the same tasks 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)Diplodocus
G
34

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

Griffen answered 14/9, 2011 at 11:26 Comment(4)
@Caltor: Follow the link above which explains Ansi Escape Sequences. These are interpreted by the terminal software.Griffen
Thanks, just what I was looking for. Although I had to reverse x and y.Absolution
I got this instead, doesn't it work for window console? >>> print_there(0,0,'hello') ←7←[0;0fhello←8>>>Atory
The link to Ansi-Escape Sequences page does not work anymore. You can use this one instead: tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.htmlLeung
S
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

Shadwell answered 21/5, 2022 at 16:53 Comment(0)
S
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"))
Spelt answered 21/5, 2023 at 12:59 Comment(1)
Could you please explain your answer a bit more to understand your way?Salmon
P
-1

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

Pickford answered 18/9, 2021 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.