Clearing the screen

M

mwt

I'm doing some python programming for a linux terminal (just learning).
When I want to completely redraw the screen, I've been using
os.system("clear")
This command works when using python in terminal mode, and in IDLE.
However, when running a little .py file containing that command, the
screen doesn't clear.

What to do?
 
F

Felipe Almeida Lessa

Em Sáb, 2006-02-11 às 12:04 -0800, mwt escreveu:
I'm doing some python programming for a linux terminal (just learning).
When I want to completely redraw the screen, I've been using
os.system("clear")
This command works when using python in terminal mode, and in IDLE.
However, when running a little .py file containing that command, the
screen doesn't clear.

What to do?

There's one escape sequence that does what you want.

I am *not* sure if this solution is the correct one, but:

$ clear | hd
00000000 1b 5b 48 1b 5b 32 4a |.[H.[2J|
00000007
$ python
Python 2.3.5 (#2, Nov 20 2005, 16:40:39)
[GCC 4.0.3 20051111 (prerelease) (Debian 4.0.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
--
"Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação'. Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas."

-- Sun Tzu, em "A arte da guerra"
 
M

mwt

I can't seem to get that to behave properly. It works fine in a python
shell, like you're demonstrating it, but not as a command in a module.
 
S

Steven D'Aprano

Em Sáb, 2006-02-11 às 12:04 -0800, mwt escreveu:
I'm doing some python programming for a linux terminal (just learning).
When I want to completely redraw the screen, I've been using
os.system("clear")
This command works when using python in terminal mode, and in IDLE.
However, when running a little .py file containing that command, the
screen doesn't clear.
What to do?

There's one escape sequence that does what you want.

I am *not* sure if this solution is the correct one, but:

$ clear | hd
00000000 1b 5b 48 1b 5b 32 4a |.[H.[2J|
00000007
$ python
Python 2.3.5 (#2, Nov 20 2005, 16:40:39)
[GCC 4.0.3 20051111 (prerelease) (Debian 4.0.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.chr(0x1b)+chr(0x5b)+chr(0x48)+chr(0x1b)+chr(0x5b)+chr(0x32)+chr(0x4a),

Or even easier:

print "\x1b[H\x1b[2J"


which may or may not work, depending on the terminal you are using.
 
S

Steven D'Aprano

I can't seem to get that to behave properly. It works fine in a python
shell, like you're demonstrating it, but not as a command in a module.

Would you like to tell us how you are using it and what happens when you
do, or would you like us to guess? Because it works for me, both as a
stand-alone script:

$ cat clear.py
magic = "\x1b[H\x1b[2J"

def clear():
print magic

if __name__ == "__main__":
clear()

$ python clear.py


and also from inside Python as a module:
 
M

mwt

No guessing needed. If I just use os.system("clear") on its own, no
problem. Also, if I use the magic formula you gave on its own, that
works to. But in the app, (see below), neither command works. I'm
missing something obvious, but I'm... missing it.

def run(self, userinfo):
"""Time when to do stuff"""
self.teamname = userinfo[0]
self.username = userinfo[1]
self.refreshinterval = userinfo[2]
self.statsfile = userinfo[3]
print self.username, "", self.teamname
self.minutes = 0
while 1:
try:
""" Checks for internet stats once per hour """
if self.minutes == 0:
self.get_online_stats(self.statsfile)
self.minutes += 1
if self.minutes == 60:
self.minutes = 0
self.output()
time.sleep(60) #nite-nite
except KeyboardInterrupt:#user types Ctrl-C
print "Bye bye."
os.system("clear")
sys.exit()


def output(self):
os.system("clear") #clear the screen
print "****************************************"
print "* Monitor *"
print "****************************************"
print "* *"
self.get_unit_info('/opt/foldingathome/1/unitinfo.txt')#print
the current unit info
print "\n last updated at", time.strftime("%Y-%m-%d %H:%M:%S",
time.localtime(time.time()))
print "* *"
print "************* Stats **************"
print "* *"
self.get_saved_stats(self.statsfile)
print "* *"
print "****************************************"
 
M

mwt

Arrgghh... Is there any way to edit posts on this thing?
The os.system("clear") doesn't work at all in a module.
 
F

Fredrik Lundh

mwt said:
Arrgghh... Is there any way to edit posts on this thing?

are you aware that you're posting to a usenet newsgroup?
The os.system("clear") doesn't work at all in a module.

works for me (as long as I'm running the code on a platform that
has a clear command). in what way does it fail for you?

</F>
 
M

mwt

It clears the screen by scrolling all the characters out of sight at
the top of the terminal window. So the screen is blank, but not cleared
in the sense that I mean it. The behavior I want is for the display to
be effectively "erased" and ready to receive the next wave of data --
like you would do in a UI -- which is what I'm used to working with,
which is why this is mildly frustrating, and at least 1/3 of why I
sound like an idiot with this question.

Anyway, I don't want it to scroll, just, in essense "refresh". As a
total Python noob, my approach has been os.system("clear") ,then print
"something".
 
D

Dennis Lee Bieber

It clears the screen by scrolling all the characters out of sight at
the top of the terminal window. So the screen is blank, but not cleared
in the sense that I mean it. The behavior I want is for the display to
be effectively "erased" and ready to receive the next wave of data --
like you would do in a UI -- which is what I'm used to working with,
which is why this is mildly frustrating, and at least 1/3 of why I
sound like an idiot with this question.

Anyway, I don't want it to scroll, just, in essense "refresh". As a
total Python noob, my approach has been os.system("clear") ,then print
"something".

One problem is that there is no "universal standard" control set for
consoles (or, at least, M$ Windows ignores the ANSI/VT-100 compatible
set -- which even my 20year old Amiga understood!) So one is forced to
either come up with some external support (UNIX termcap and/or curses)
or code stuff specific to a single OS/console. IOW, while a text console
may look like something low-level and common, it is as different as
doing a clear operation in Tkinter vs MFC/win32api.l
--
 
M

mwt

You know, for now, I just would like it to work in a standard Gnome
terminal (my version is 2.12.0).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top