Clear screen in Python

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

Clear screen in Python

Postby mlempjr » Thu Nov 28, 2013 3:53 am

I would like some help with the Python 3.3 code that will clear the screen and place the cursor in the upper left corner of the screen.
mlempjr
 
Posts: 19
Joined: Thu Nov 28, 2013 3:47 am

Re: Clear screen in Python

Postby metulburr » Thu Nov 28, 2013 7:19 am

You can also use the subprocess module but since you do not need to pipe the output of the command, i guess os.system() suffices.
assuming you mean the console window and not a GUI window:
Code: Select all
import os
os.system('cls') #windows

os.system('clear') #linux / os x
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1490
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Clear screen in Python

Postby mlempjr » Thu Nov 28, 2013 4:43 pm

Well, being new to the terminology, I was not sure what to call the window. Since neither worked for me I must have meant the GUI window.

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

Re: Clear screen in Python

Postby metulburr » Thu Nov 28, 2013 5:55 pm

That would depend on what GUI library you are using then
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1490
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Clear screen in Python

Postby Somelauw » Thu Nov 28, 2013 6:01 pm

mlempjr wrote:Well, being new to the terminology, I was not sure what to call the window. Since neither worked for me I must have meant the GUI window.

Thanks, Mike


It probably does not work in idle, but does one of them work when running python in a console window?
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 73
Joined: Tue Feb 12, 2013 8:30 pm

Re: Clear screen in Python

Postby mlempjr » Thu Nov 28, 2013 6:14 pm

Well as I said, I'm new. What do I do to get to run in the console window? Thanks again, Happy thanksgiving!
Pretty sure I am running idle. I got into Python because of the Raspberry Pi and I'm using idle3 for that so I try to keep them the same. Much less confusion. And as you see I'm usually confused!
mlempjr
 
Posts: 19
Joined: Thu Nov 28, 2013 3:47 am

Re: Clear screen in Python

Postby Somelauw » Thu Nov 28, 2013 10:22 pm

mlempjr wrote:Well as I said, I'm new. What do I do to get to run in the console window? Thanks again, Happy thanksgiving!
Pretty sure I am running idle. I got into Python because of the Raspberry Pi and I'm using idle3 for that so I try to keep them the same. Much less confusion. And as you see I'm usually confused!


Right click the python file and press something like "run script".
Or open cmd, navigate to the python file and enter "python filename.py".
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 73
Joined: Tue Feb 12, 2013 8:30 pm

Re: Clear screen in Python

Postby Somelauw » Thu Nov 28, 2013 11:12 pm

If you don't want to use terminal, you might also try using IdleX instead of idle, which is like Idle but does support clearing the window.
http://sourceforge.net/projects/idlex/
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 73
Joined: Tue Feb 12, 2013 8:30 pm

Re: Clear screen in Python

Postby mlempjr » Fri Nov 29, 2013 12:34 am

Well neither instruction worked in idlex. Should I be using a different instruction to do the clear?

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

Re: Clear screen in Python

Postby mlempjr » Fri Nov 29, 2013 2:26 am

Hi Somelauw, actually, I may not have been using idlex. I thought I was, but now when I try to bring it up I get an error message. I'll keep trying to get it to run.
Mike
mlempjr
 
Posts: 19
Joined: Thu Nov 28, 2013 3:47 am

Re: Clear screen in Python

Postby Kebap » Fri Nov 29, 2013 8:04 am

Hi, why do you want to clear a screen? And which screen are you talking about there? Maybe a screenshot may help explain.
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: 397
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Clear screen in Python

Postby mlempjr » Fri Nov 29, 2013 2:38 pm

I want to clear all of the input and responses that continue to build up as the program runs. I give the program some data and it responds. Then I would like to get rid of that info and start the next dialog at the top of the screen. You know, like real programs do!

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

Re: Clear screen in Python

Postby Crimson King » Fri Nov 29, 2013 2:49 pm

I don't think there's a way to do that in IDLE.

You may want to start using the terminal and trying metulburr suggestions.
User avatar
Crimson King
 
Posts: 131
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Re: Clear screen in Python

Postby mlempjr » Fri Nov 29, 2013 4:29 pm

I'm beginning to think that you are right!
Thanks
Mike
mlempjr
 
Posts: 19
Joined: Thu Nov 28, 2013 3:47 am

Re: Clear screen in Python

Postby stranac » Fri Nov 29, 2013 4:46 pm

mlempjr wrote:You know, like real programs do!

Real programs don't do that. The ones that do are annoying.

If you really have the need for some fancy user interaction, you should either use a gui toolkit, or something like urwid.
Friendship is magic!

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

Re: Clear screen in Python

Postby mlempjr » Fri Nov 29, 2013 9:52 pm

Hey Stranac, notice how the reply area comes up, all blank with the cursor in the upper left!
mlempjr
 
Posts: 19
Joined: Thu Nov 28, 2013 3:47 am

Re: Clear screen in Python

Postby stranac » Fri Nov 29, 2013 10:42 pm

This is not a cli program, it's a web forum.

From the reply page, if I want to see what was told before, I just scroll down and there it is.
Also, I can have multiple tabs open, so it wouldn't really matter if I couldn't do that.

If you remove information from a screen in a cli program, you're just making it harder to use.
It makes it harder for the user to see the context, and it's simply annoying.
Friendship is magic!

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

Re: Clear screen in Python

Postby Marbelous » Mon Dec 02, 2013 5:34 pm

It's true that most all real programmers would never want the history of the Standard Output (your PC's display) to erase itself for no reason. It's not there to look pretty, it's there to provide you with information. The next common request from beginners is "How do I color certain words?". The answer really is... Don't. If you want a pretty interface you need to learn a GUI toolkit. Trying to force the console window to jump through hoops is a frustrating, OS-specific waste of your effort that will almost certainly disappoint you even if you do figure it out. All computer languages (except LabVIEW and some GUI-based) take input as a stream of ASCII characters (from your keyboard) and output the same (to your display). They have no knowledge of a graphics card or it's drivers which means you have to learn all that or use an OS specific package like CURSES (http://en.wikipedia.org/wiki/Curses_(pr ... ng_library)) to control the screen. By the time you do that, you could have learned TKinter and be making MUCH more professional looking GUI programs. EDIT: I don't mean that TKinter is the most professional looking, but it is easy and comes with most Python distributions.

BTW, as Stranac already said, the input screen I'm typing this into is not a console. It's a TextBox Widget running in a GUI interface on a webpage. ;) Spend your time learning how to do that and leave console manipulation back in the 80's.

If you really need a little screen space:
Code: Select all
for i in range(10): print "" # Print 10 blank lines
Marbelous
 
Posts: 153
Joined: Fri May 31, 2013 8:12 pm

Re: Clear screen in Python

Postby mlempjr » Mon Dec 02, 2013 6:18 pm

Let me try this again. First I am a beginner to Python. I discovered it as a result of getting a Raspberry Pi. So... I start my Python program and it looks like this:

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 ================================
>>>

Then as the program continues, it scrolls down.

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".

Any real suggestions?

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

Re: Clear screen in Python

Postby metulburr » Mon Dec 02, 2013 8:41 pm

I suggest you read this tutorial, to learn the difference between Python's interactive prompt and running a Python script.

Code: Select all
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 ================================
>>>

^this is the interactive prompt

Code: Select all
os.system('cls')

Code: Select all
os.system('clear')

(depending on what OS is on the rasberry pi)
This is what your looking for then. However it is not going to clear the interactive prompt. It is going to clear the console/terminal when you run it as a script.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1490
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Next

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests