0

I'm doing a script with a menu-like beginning , and I wanted to know if I could do something like this:

You open the file and it prints this menu :

LOGO
Welcome to script 11 , what would you like to do?
-write a file
-read a file
-create a file
> #input here

You select write ( for example) and it prints this OVER the previous menu intead of printing if after it:

LOGO
Writing a file!
-Select the path:
> #input here

Thanks for the help in advance.

Edit: I didn't want to completely erase the screen( I've seen the other threads about that) but if there is a method to erase only some lines , but the anwers tell me that it isn't possible without external libraries so I'll just clean all the screen and reprint some things, but thanks for all the anwsers

Tkappa
  • 25
  • 4
  • 2
    What platform? Windows console or POSIX terminal or something else? – Martijn Pieters Apr 23 '13 at 22:59
  • see http://stackoverflow.com/q/3593339/1176601 – Aprillion Apr 23 '13 at 23:00
  • You probably don't really want to "print over the previous menu" (because then you'd leave leftover characters from cases where the new lines were shorter than the old), but instead to just clear the screen, right? If so, see http://stackoverflow.com/questions/4810537/how-to-clear-the-screen-in-python (That question is a dup, but it has links to many of the other repeated dups, so follow the links for good answers.) – abarnert Apr 23 '13 at 23:05

2 Answers2

2

If you are using Linux (or OSX), you can use the curses module.

If you are using windows, use the console module.

mjgpy3
  • 8,597
  • 5
  • 30
  • 51
1

If you want to display a multi-line menu, and display an entirely new one after each menu choice, there are only a few ways to do this:

  1. Clear the screen before printing each menu. There are about 69102 questions on SO about how to do this; how to clear the screen in python has links to many of them.

  2. Print out a form feed/page feed. Which will not work on many modern terminals (Windows or Unix), but it rocks on old-school teletypes.

  3. Use terminal control sequences to move the cursor around. This will work on Windows if the cmd terminal is ANSI-enabled (I believe it usually isn't by default), and on everything else if you pick (or look up) the right terminal to send control sequences for. You will also have to make sure to overwrite each character, not just each line. Otherwise, when you overwrite line 2 you'll end up with "Writing a file!pt 11 , what would you like to do?".

  4. Use curses (and your favorite third-party Windows curses port), or some other terminal graphics library, to do this all at a higher level. (Writing separate code using console if it exists—for Windows—and curses otherwise—for almost everything else—is often the simplest way to do this.)

I'd suggest 1 or 4, but those are your options.

Community
  • 1
  • 1
abarnert
  • 354,177
  • 51
  • 601
  • 671