Clear screen in Python

This is the place for queries that don't fit in any of the other categories.

Re: Clear screen in Python

Postby Somelauw » Mon Dec 02, 2013 8:51 pm

mlempjr wrote:What I would like to be able to do is to clear all of that text and move the cursor to the top. I guess that it's an old fashioned Idea, but, heck I've peen programming since 1964 and this is one of the few languages that I have run across that doesn't have an easily accessible command like the "old fashioned" "CLS".


It's not a python limitation. It's an IDLE limitation.
As we already told you, these are your options:
1. use IDLEX,
2. just open a new shell window
3. use a console window instead of IDLE
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 57
Joined: Tue Feb 12, 2013 8:30 pm

Re: Clear screen in Python

Postby mlempjr » Tue Dec 03, 2013 3:36 am

Hi metulburr , Thanks for all of the info. Your observation is very accurate, I don't speak very good "Python". I am improving though. What I probably failed to mentioned in my previous question is that I would like to clear everything off the "Screen" and move the cursor to the top left many times in the same "Program" execution, not just at the start. If the program asks the user a question, I'd like the "screen" to clear and the cursor to move to the top after the user responds and before the next question is asked. In other words, I would like this to happen under my control while the "Program" is running. And, I'm beginning to think that this is not possible in Python. If so, I may just do the basic stuff with the "Pi" and be done with Python after that.

Thanks for the fun tutorial, Although it really confuses me why it is necessary to deal with so many levels in describing the area that the program can communicate with.

Mike
mlempjr
 
Posts: 19
Joined: Thu Nov 28, 2013 3:47 am

Re: Clear screen in Python

Postby Mekire » Tue Dec 03, 2013 4:28 am

It isn't that what you want isn't possible in python. It is just that it isn't done directly in the terminal window. If you were using some GUI toolkit or Curses or some other library then you could do this. This is a fairly large leap though for a beginner. If you feel you are ready and comfortable with the language then by all means dive in to tkinter or wxpython. If you use a gui you will be able to change screen color, cursor location, whatever you desire.

Again, it isn't that the language doesn't do what you want, but that it doesn't do it in the way you might expect.

-Mek
User avatar
Mekire
 
Posts: 817
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Clear screen in Python

Postby metulburr » Tue Dec 03, 2013 7:19 am

What I probably failed to mentioned in my previous question is that I would like to clear everything off the "Screen" and move the cursor to the top left many times in the same "Program" execution, not just at the start. If the program asks the user a question, I'd like the "screen" to clear and the cursor to move to the top after the user responds and before the next question is asked. In other words, I would like this to happen under my control while the "Program" is running.

You didnt fail to mention anything. I have the same perception of your interests now in what you want as what you asked in the first question, and the response i gave to that.
Does this not clear the screen and move the cursor to the top as how you wished?

Code: Select all
import sys
import os
import time

def clear():
    if sys.platform == 'win32':
        os.system('cls')
    else:
        os.system('clear')

while True:
    ch = raw_input('Enter something: ')
    print('you entered: {}'.format(ch))
    time.sleep(2)
    clear()

upon each iteration "Enter something:" is the only text seen because that is what is printed. If you wanted nothing, then dont print anything after clearing the screen. Thus this gives the output the appearance of clearing the screen and clearing it upon each input after 2 seconds, while moving the cursor back to the top.

However if you are referring to movement of the cursor around, such as with arrow keys...You will need NCurses or if even a GUI library stated by Mekire. There is limited things you can do with just the terminal/console anyways.

I would also agree with stranac:
Real programs don't do that. The ones that do are annoying.

"Real" terminal programs let you view the previous I/O. You can spot a noob program simply by the fact that it clears the screen at all.
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1107
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Clear screen in Python

Postby mlempjr » Tue Dec 03, 2013 3:39 pm

Hi,
I get the following message:

Traceback (most recent call last):
File "C:/Python33/testClearAll.py", line 13, in <module>
ch = raw_input('Enter something: ')
NameError: name 'raw_input' is not defined
mlempjr
 
Posts: 19
Joined: Thu Nov 28, 2013 3:47 am

Re: Clear screen in Python

Postby Mekire » Tue Dec 03, 2013 3:47 pm

In python 3 raw_input was renamed to input.

Just change that line to:
Code: Select all
ch = input('Enter something: ')
and you should be good.

-Mek
User avatar
Mekire
 
Posts: 817
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Clear screen in Python

Postby mlempjr » Tue Dec 03, 2013 3:58 pm

Yup, Changed it to raw and it runs, But other than a flash of something, everything is still there. the flash is probably clearing something, but not my "screen"
mlempjr
 
Posts: 19
Joined: Thu Nov 28, 2013 3:47 am

Re: Clear screen in Python

Postby mlempjr » Tue Dec 03, 2013 4:12 pm

ok, I run it, and it prints what I typed and brings up a window with the program in it. But the "screen" is still not cleared. here is what I have after two more enters"

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Enter something: clear
you entered: clear
Enter something: please clear
you entered: please clear
Enter something: waiting for clear
you entered: waiting for clear
Enter something:

Maybe my confusion is in what clear means.
oh yes, and after each of the subsequent entries, it does the flash of something.
mlempjr
 
Posts: 19
Joined: Thu Nov 28, 2013 3:47 am

Re: Clear screen in Python

Postby mlempjr » Tue Dec 03, 2013 4:36 pm

Hi Mekire, Thought I should mention that so far I have tried "cups", "tkinter" and "curses" and have had the same result.
Thanks four patience,
Mike
mlempjr
 
Posts: 19
Joined: Thu Nov 28, 2013 3:47 am

Re: Clear screen in Python

Postby stranac » Tue Dec 03, 2013 4:54 pm

As Somelauw already said, you can't do that in IDLE. IDLE is awful, you should stop using it.
If you want help with what you have tried with other libraries, you should post your code for those as well.

Also, that was your 13th post. It's about time you start using code tags.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 896
Joined: Thu Feb 07, 2013 3:42 pm

Re: Clear screen in Python

Postby Marbelous » Tue Dec 03, 2013 6:31 pm

I would normally suggest you update to a real IDE (I love PyCharm's free community edition), but firstly, they have their own learning curve and, secondly, I haven't tried them on my Pi's and I think they might run a "bit" slow (I'll let you know when I get PyCharm installed). When I code on my Pi's I use IDLE for the editing, save to my personal Python folder, go to a terminal window that's already open to the Python folder, change the permission and ownership, then run the program with python MyLatestProgram.py . That may seem like a lot to do but after you save and chmod/chown once you just have to save your changes in IDLE, click in the terminal window and press the UP ARROW to recall the last command (python MyLatestProgram.py), and press enter to run it again. This avoids all the problems with python trying to run your program whiles it's already running IDLE. And if you think IDLE is confusing things now, wait until you try to write a GUI program or game! :o

In any case, if you using a Raspberry Pi you should know how to edit a program with ANY editor (vi, geany, IDLE) and then change the permissions/ownership to your needs and then execute it like it was meant to be executed and NOT relying on the "RUN PROGRAM" functions in an editor.

Enjoy your Raspberry! :P :P :P
Marbelous
 
Posts: 63
Joined: Fri May 31, 2013 8:12 pm

Re: Clear screen in Python

Postby metulburr » Tue Dec 03, 2013 7:44 pm

I literally had to download IDLE and use it just to see what you mean. Oh yes, it puts its output in their window. OK, Here is what you do: Stop using IDLE.
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1107
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Clear screen in Python

Postby mlempjr » Tue Dec 03, 2013 8:29 pm

Hi stranac, No worries, I won't be posting any more code!
mlempjr
 
Posts: 19
Joined: Thu Nov 28, 2013 3:47 am

Re: Clear screen in Python

Postby mlempjr » Wed Dec 04, 2013 5:56 am

Hi metulburr, What do you think of eclipse, would that be a good choice over idle?
mlempjr
 
Posts: 19
Joined: Thu Nov 28, 2013 3:47 am

Re: Clear screen in Python

Postby metulburr » Wed Dec 04, 2013 6:30 am

Hi stranac, No worries, I won't be posting any more code!

we will hold you to it the next time you post code

Hi metulburr, What do you think of eclipse, would that be a good choice over idle?

I tried eclipse once when i first started. I didnt like it that much. Of course that is just my preference. From what i remember... there was a crazy sequence of instructions to carry out in order to get it to work ok with python using pydev. I used this:
http://www.rose-hulman.edu/class/csse/r ... ration.htm
However, i do not know if this is still valid with whatever is the latest version of eclipse

Here is a list of python IDE's
https://wiki.python.org/moin/Integrated ... vironments
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1107
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Clear screen in Python

Postby Kebap » Wed Dec 04, 2013 11:56 am

mlempjr wrote:No worries, I won't be posting any more code!

Thinking about that, now I am worried and hope this doesn't mean you won't be using the forums anymore, or even drop python altogether. I am confident, there is a solution for you. Python is so versatile, can't believe this clearing screen is creating such a problem currently. Also, I would welcome having more programmers with 50 years of experience in the forums.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 282
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Clear screen in Python

Postby mlempjr » Wed Dec 04, 2013 5:35 pm

To all: Well I wish I could quit, but after all these years, I guess I just can not give up. Sooner or later I'll figure it out!
Mike
mlempjr
 
Posts: 19
Joined: Thu Nov 28, 2013 3:47 am

Re: Clear screen in Python

Postby mlempjr » Sun Dec 08, 2013 5:39 am

Hi metulburr , Hope you read this, Since we last talked, I learned that if (in Windows) I just double click on my program it runs in the interactive mode and does all the things that it should do. The problem was again with my inexperience and unfamiliarity with the vocabulary. Thanks again for all of your help!
Mike
mlempjr
 
Posts: 19
Joined: Thu Nov 28, 2013 3:47 am

Re: Clear screen in Python

Postby Mekire » Sun Dec 08, 2013 8:39 am

Glad to hear you have solved your current problem. I would like to make one correction however. The word "interactive" does not refer to what you think. The interactive interpreter is the one with the prompt >>>. It is used for quickly testing small snippets of code. Double clicking in a windows environment is not this interactive mode however; it is just another way of executing your code. You would get the same results if you opened a command-prompt/terminal-window and (assuming your paths are set up correctly) typed python my_script.py.

Metulburr already posted it, and I believe you have looked at it, but reading through the tutorial on executing python code again would be good.
http://python-forum.org/viewtopic.php?f=25&t=55

If you have further python questions don't hesitate to ask.
-Mek
User avatar
Mekire
 
Posts: 817
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Clear screen in Python

Postby mlempjr » Sun Dec 08, 2013 8:09 pm

Hi Mekire, You are right, It does exactly that! My point would be, (after 50 years of programming). That Python is more focused on the structure rather than on accomplishing something productive! Probably a result of letting anyone contribute anything, and never being finished with the language!

Very frustrating!
Mike
mlempjr
 
Posts: 19
Joined: Thu Nov 28, 2013 3:47 am

PreviousNext

Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 4 guests