topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday April 18, 2024, 9:53 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Python newbie question  (Read 9688 times)

kartal

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 1,529
    • View Profile
    • Donate to Member
Python newbie question
« on: April 13, 2009, 10:53 PM »
Hi Guys

I am working on my own alarm, countdown application. I do not have a gui setup now( will use wxpython). However I need a way to be able to print current time in a window(or command line) without printing every second in a new line. I can query time module to get the current time but I want to be able to print in a simple seperate window and terminate the window or time counting when the time is up. I have figured out the rest except printing time. This is for only testing the application not for final version. Currently I am using rudimentarty print command which prints the time on ever new line which is unpleasant.

thanks

Ehtyar

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,237
    • View Profile
    • Donate to Member
Re: Python newbie question
« Reply #1 on: April 14, 2009, 12:40 AM »
I apologize in advance to Tin Man for treading on toes.

A little Googling would have found you the answer:
Code: Python [Select]
  1. import time
  2.  
  3. def PrintStatic(a_string=''):
  4.         print '\b%s%s'%(a_string,'\b' * len(a_string)),
  5.  
  6. PrintStatic("1")
  7. time.sleep(5)
  8. PrintStatic("2")
From this ActiveState recipe, comment 4.

Ehtyar.

CWuestefeld

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,009
    • View Profile
    • Donate to Member
Re: Python newbie question
« Reply #2 on: April 14, 2009, 12:13 PM »
I think you'll find that once you move to the GUI, this simply ceases to be an issue. For most GUI work you're not "printing" onto the screen; you're setting a property of a control (e.g. a text label), specifying the text that you want it to display. That control is in charge of (and the framework should contain all the implementation necessary for) rendering that value onto the screen.

Ehtyar

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,237
    • View Profile
    • Donate to Member
Re: Python newbie question
« Reply #3 on: April 14, 2009, 11:18 PM »
That's certainly true CWuestefeld, but I think in many cases the console is superior when debugging an application.

Ehtyar.

kartal

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 1,529
    • View Profile
    • Donate to Member
Re: Python newbie question
« Reply #4 on: April 15, 2009, 12:50 AM »
Ehtyar 
that code did not work here and it looks lke it was quite old. SO far I could not find any solution.

The reason I want to use the console is that it makes me keep things single. I am trying to make sure that my code works nice and easy for now :) A gui is definetely needed but it looks like too much coding and complication for me. Althout I have found the oa Constructor to be as a nice easy gui-python developement environment. I have not tried it fully but its gui construction kit looks sufficient.

« Last Edit: April 15, 2009, 12:52 AM by kartal »

Ehtyar

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,237
    • View Profile
    • Donate to Member
Re: Python newbie question
« Reply #5 on: April 15, 2009, 01:42 AM »
I wrote the demo code I posted and tested it myself. What version of Python are you using?

Ehtyar.

kartal

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 1,529
    • View Profile
    • Donate to Member
Re: Python newbie question
« Reply #6 on: April 15, 2009, 02:09 AM »
well the code works but it does not do anything useful regarding what I was asking for. It prints the output on the same line rather than replacing the line(the line keeps getting bigger) content, at least here under my python 2.5 (32bit)


Ehtyar

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,237
    • View Profile
    • Donate to Member
Re: Python newbie question
« Reply #7 on: April 15, 2009, 02:15 AM »
I'm on same version, and it definitely works. Windows console?

Ehtyar.

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: Python newbie question
« Reply #8 on: April 15, 2009, 08:13 AM »
If you're always printing text of the same length and don't mind printing it at the start of the line, just use a single \r character at the start of your string - if that doesn't work, something's wrong :)
- carpe noctem

tinjaw

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,927
    • View Profile
    • Donate to Member
Re: Python newbie question
« Reply #9 on: April 15, 2009, 09:55 AM »
Yes, \r will do a carriage return. \n is new line (linefeed).

This example should illustrate.

Code: Python [Select]
  1. print "one\rtwo\rthree"
  2. print "one\ntwo\nthree"

And to make sure you are zero padding properly so all the digits line up.

kartal

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 1,529
    • View Profile
    • Donate to Member
Re: Python newbie question
« Reply #10 on: April 15, 2009, 12:45 PM »
Hi

here is the kind of code i have implemented to test it, and it is not working for me. It just prints all in a new line. I have tried it under Idle as well.

a=0
while (1):
    a=a+1
    if a>20:
        break
    print str(a)+'\r'



Ehtyar

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,237
    • View Profile
    • Donate to Member
Re: Python newbie question
« Reply #11 on: April 15, 2009, 04:03 PM »
kartal, Python's print() automatically appends a newline to each statement. Add a comma to the end of your call to print() to prevent it.

Code: Python [Select]
  1. a=0
  2. while (1):
  3.     a=a+1
  4.     if a>20:
  5.         break
  6.     print str(a)+'\r',

Ehtyar.

kartal

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 1,529
    • View Profile
    • Donate to Member
Re: Python newbie question
« Reply #12 on: April 15, 2009, 04:06 PM »
ok this is getting weird. Here is a screenshot from idle running with ',' and without it. First one is with a comma, second(vertical) is without

« Last Edit: April 15, 2009, 04:09 PM by kartal »

Ehtyar

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,237
    • View Profile
    • Donate to Member
Re: Python newbie question
« Reply #13 on: April 15, 2009, 05:38 PM »
You're going to need to use a console that at least supports control codes kartal. I did ask if you were using the Windows console to which you did not respond.

Ehtyar.

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: Python newbie question
« Reply #14 on: April 15, 2009, 05:41 PM »
Hm, your IDE probably doesn't handle the \r character properly - try running it from a cmd.exe console instead...
- carpe noctem

kartal

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 1,529
    • View Profile
    • Donate to Member
Re: Python newbie question
« Reply #15 on: April 15, 2009, 06:05 PM »
Well this was from Python`s native console. I am not sure if it supports or not really. I am using Pythong Ide Idle for this example.

Which console software would support control codes? Any free ones out there?

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: Python newbie question
« Reply #16 on: April 15, 2009, 06:10 PM »
Umm... cmd.exe == console/shell.

The "capture console output" window in IDEs lack a lot of stuff that a "real" console has. Unprofessional that \r isn't handled correctly, though :)
- carpe noctem

kartal

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 1,529
    • View Profile
    • Donate to Member
Re: Python newbie question
« Reply #17 on: April 15, 2009, 06:41 PM »
Thanks for assistance guys, I am giving up at this point. I think I will use some kind of 2d image library to print results on a canvas.

tinjaw

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,927
    • View Profile
    • Donate to Member
Re: Python newbie question
« Reply #18 on: April 16, 2009, 07:41 AM »
Yes. Your problems are because you are using an interactive IDLE session. You need to run it from a console. Personally, I never use IDLE.