Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 217
Cesco Avatar asked Sep 12 '11 19:09

Cesco


People also ask

How do I print one character at a time in Python?

To print one character at a time on the same line: Use a for loop to iterate over the string. Use the print() function to print each character.

Which of the following functions prints one character at a time?

As we can see in the above program, it takes a single character at the run time from the user using the getchar() function. After getting the character, it prints the letter through the putchar() function.

How does Python prevent the print function from moving to the next line?

In Python 3, print() is a function that prints output on different lines, every time you use the function. However, you can avoid this by introducing the argument end and assigning an empty string to it. This will prevent the next output from being printed in a new line.


2 Answers

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

like image 197
rumpel Avatar answered Sep 30 '22 20:09

rumpel


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.

like image 31
Draxdo Avatar answered Sep 30 '22 18:09

Draxdo