Printing to console (No Scroll)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Totte Karlsson

    Printing to console (No Scroll)

    Hi,
    How can I print to the console without having it scrolling to a new line for
    each print statement?
    I want to print a count down in the console, but for each count it scrolls
    the screen (of course).

    Is there another way?

    Here is the simple script for now

    print "Closing window in :"
    for second in range(10):
    time.sleep(1)
    print `10-second` +" seconds"

    thanks
    /totte




  • Diez B. Roggisch

    #2
    Re: Printing to console (No Scroll)

    > How can I print to the console without having it scrolling to a new line[color=blue]
    > for each print statement?
    > I want to print a count down in the console, but for each count it scrolls
    > the screen (of course).[/color]

    Use ncurses.

    Diez

    Comment

    • Joe Francia

      #3
      Re: Printing to console (No Scroll)

      Totte Karlsson wrote:[color=blue]
      > Hi,
      > How can I print to the console without having it scrolling to a new line for
      > each print statement?
      > I want to print a count down in the console, but for each count it scrolls
      > the screen (of course).
      >
      > Is there another way?
      >
      > Here is the simple script for now
      >
      > print "Closing window in :"
      > for second in range(10):
      > time.sleep(1)
      > print `10-second` +" seconds"
      >
      > thanks
      > /totte[/color]

      This works for me:

      import time, sys
      for second in range(10):
      time.sleep(1)
      sys.stdout.writ e(`10-second` + " seconds ")
      sys.stdout.flus h()

      Comment

      • Totte Karlsson

        #4
        Re: Printing to console (No Scroll)

        Any alternatives to ncurses? It seems like a overkill for this...
        cheers
        /totte


        "Diez B. Roggisch" <deets_noospaam @web.de> wrote in message
        news:bu41k5$4in $00$2@news.t-online.com...[color=blue][color=green]
        > > How can I print to the console without having it scrolling to a new line
        > > for each print statement?
        > > I want to print a count down in the console, but for each count it[/color][/color]
        scrolls[color=blue][color=green]
        > > the screen (of course).[/color]
        >
        > Use ncurses.
        >
        > Diez
        > --
        > http://mail.python.org/mailman/listinfo/python-list
        >[/color]




        Comment

        • Diez B. Roggisch

          #5
          Re: Printing to console (No Scroll)

          Totte Karlsson wrote:
          [color=blue]
          > Any alternatives to ncurses? It seems like a overkill for this...[/color]

          Maybe you can use sys.stdout.writ e in conjunction with control-codes for
          moving back the cursor to column one. But you'll have to lookup these for
          yourself :)

          Diez

          Comment

          • Derek

            #6
            Re: Printing to console (No Scroll)

            "Diez B. Roggisch" wrote[color=blue][color=green]
            > > Any alternatives to ncurses? It seems like a overkill for this...[/color]
            >
            > Maybe you can use sys.stdout.writ e in conjunction with control-
            > codes for moving back the cursor to column one. But you'll have
            > to lookup these for yourself :)[/color]

            I think \r is the control code (at least if you want to go back to the
            start of the line):

            import time, sys
            for second in range(10):
            time.sleep(1)
            sys.stdout.writ e(`10-second` + " seconds \r")
            sys.stdout.flus h()


            Comment

            • Totte Karlsson

              #7
              Re: Printing to console (No Scroll)

              Great, thanks for all help! I'll try these things

              "Derek" <none@none.co m> wrote in message
              news:bu45kp$d99 m3$1@ID-46268.news.uni-berlin.de...[color=blue]
              > "Diez B. Roggisch" wrote[color=green][color=darkred]
              > > > Any alternatives to ncurses? It seems like a overkill for this...[/color]
              > >
              > > Maybe you can use sys.stdout.writ e in conjunction with control-
              > > codes for moving back the cursor to column one. But you'll have
              > > to lookup these for yourself :)[/color]
              >
              > I think \r is the control code (at least if you want to go back to the
              > start of the line):
              >
              > import time, sys
              > for second in range(10):
              > time.sleep(1)
              > sys.stdout.writ e(`10-second` + " seconds \r")
              > sys.stdout.flus h()
              >
              >
              > --
              > http://mail.python.org/mailman/listinfo/python-list
              >[/color]




              Comment

              • Samuel Walters

                #8
                Re: Printing to console (No Scroll)

                The cheesy dirt simple way:

                print "Closing window in :"
                for second in range(10):
                time.sleep(1)
                #print enough spaces to cover up the last message.
                print " "*20 + "\r",
                print `10-second` +" seconds",

                Note the comma at the end of each print statement.
                That causes python not to output a newline.

                HTH

                Sam Walters.

                --
                Never forget the halloween documents.

                """ Where will Microsoft try to drag you today?
                Do you really want to go there?"""

                Comment

                • Dennis Lee Bieber

                  #9
                  Re: Printing to console (No Scroll)

                  Samuel Walters fed this fish to the penguins on Wednesday 14 January
                  2004 21:30 pm:
                  [color=blue]
                  > print " "*20 + "\r",
                  > print `10-second` +" seconds",
                  >
                  > Note the comma at the end of each print statement.
                  > That causes python not to output a newline.
                  >[/color]
                  Don't you need a \r on the second print line also? Otherwise the
                  spaces will be printed at the end of the previous counter value?


                  Though would \r even work if this was to be run on a Mac -- I thought
                  Macs used to use \r for line ending (maybe OS-X has gone to \n).


                  --[color=blue]
                  > =============== =============== =============== =============== == <
                  > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                  > wulfraed@dm.net | Bestiaria Support Staff <
                  > =============== =============== =============== =============== == <
                  > Bestiaria Home Page: http://www.beastie.dm.net/ <
                  > Home Page: http://www.dm.net/~wulfraed/ <[/color]

                  Comment

                  • Samuel Walters

                    #10
                    Re: Printing to console (No Scroll)

                    | Dennis Lee Bieber said |[color=blue]
                    > Don't you need a \r on the second print line also? Otherwise the
                    > spaces will be printed at the end of the previous counter value?[/color]

                    Um, yeah. You do.
                    [color=blue]
                    >
                    >
                    > Though would \r even work if this was to be run on a Mac -- I
                    > thought
                    > Macs used to use \r for line ending (maybe OS-X has gone to \n).[/color]

                    I don't know. I've never owned a mac, and haven't ever worked on one.
                    Thus, mac nuances rarely enter my mind even when I know about them.

                    Sam Walters.

                    --
                    Never forget the halloween documents.

                    """ Where will Microsoft try to drag you today?
                    Do you really want to go there?"""

                    Comment

                    • Dennis Lee Bieber

                      #11
                      Re: Printing to console (No Scroll)

                      Samuel Walters fed this fish to the penguins on Thursday 15 January
                      2004 17:23 pm:

                      [color=blue]
                      >
                      > I don't know. I've never owned a mac, and haven't ever worked on one.
                      > Thus, mac nuances rarely enter my mind even when I know about them.
                      >[/color]
                      While I don't know about OS-X (being a BSD/Mach core?), I'm sure the
                      earlier versions used <CR> (\r) for line endings (I think the TRS-80
                      did too). The Amiga used <LF> (\n)... And of course, you get the
                      confusion in Windows of <CR><LF> (but not <LF><CR>!)


                      --[color=blue]
                      > =============== =============== =============== =============== == <
                      > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                      > wulfraed@dm.net | Bestiaria Support Staff <
                      > =============== =============== =============== =============== == <
                      > Bestiaria Home Page: http://www.beastie.dm.net/ <
                      > Home Page: http://www.dm.net/~wulfraed/ <[/color]

                      Comment

                      • Valentino Volonghi aka Dialtone

                        #12
                        Re: Printing to console (No Scroll)

                        "Totte Karlsson" <mtk@qm.com> writes:
                        [color=blue]
                        > Any alternatives to ncurses? It seems like a overkill for this...[/color]
                        import sys, time
                        nrchars = 0

                        for i in xrange(10):
                        sys.stdout.writ e("\b \b"*nrchars)
                        sys.stdout.flus h()
                        Str = "%i seconds to go" % (10-i)
                        nrchars = len(Str)
                        sys.stdout.writ e(Str)
                        sys.stdout.flus h()
                        time.sleep(1)
                        print


                        --
                        Valentino Volonghi, Regia SpA, Milan
                        Linux User #310274, Gentoo Proud User

                        Comment

                        Working...