LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-23-2008, 10:55 PM   #1
xadrith
Member
 
Registered: Oct 2007
Distribution: Slack 12.1
Posts: 43

Rep: Reputation: 15
Suspending operations with raw_input (Python)


This is somewhat related to my previous post requesting assistance for code that would unfold text char by char to give that movie-like intro and plotline sequencing to my text-based game.

Borrowing from Rocket357's code I have the following:

Code:
def unfold_message(msg,delay):
        try:
            delay=float(delay)
        except ValueError:
            delay=0.100
        for char in msg:
            time.sleep(delay)
            sys.stdout.write(char)
            sys.stdout.flush()
        print
This worked out perfectly, exactly what I was looking for (thanks again )

However, I also took smoked kipper's advice in offering the user a way to "fast forward" through the scrolling, in the case of them having already read those parts and just wishing to get into the game again...

However I am having trouble with implementing user input (say to have the user press the enter button to instantly speed the scrolling) the way I want it to work.

I've tried many different ways, and some have come closer to others, my most recent attempt is this:

Code:
def unfold_message(msg,delay):
    x = True
    if x == True:
        try:
            delay=float(delay)
        except ValueError:
            delay=0.100
        for char in msg:
            time.sleep(delay)
            sys.stdout.write(char)
            sys.stdout.flush()
        print
    else:
        x = bool(raw_input())
        if x == False:
            clear()
            print msg
Not my best, but... my most recent .

I want to be able to have the user hit enter without stopping the scrolling with a prompt for him/her to do so.

However I seem to implement it, the scrolling will always STOP, and then wait for the user's raw_input() and then will either print the whole msg, or just break up the scrolling text into smaller chunks.

I know it's something elegant and simple that is required, but I just can't seem to find it. Am I using the wrong control statements? I have tried if statements, for statements, while statements.

Somewhat still new to this, so help is definitely appreciated...

Thanks for your time,
x.D
 
Old 07-24-2008, 05:09 PM   #2
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
What you are trying to do will be require platform-dependent code. The following will not work on Windows (and there is more than one way to do it), and you will need to use termios to handle user input not mangling the screen:

Code:
#!/bin/env python
import time
import sys
import select

def unfold_message(msg,delay):
     try:
          delay=float(delay)
     except ValueError:
          delay=0.100
     for char in msg:
            if not select.select([sys.stdin],[],[],0) == ([sys.stdin],[],[]):
                 time.sleep(delay)
            sys.stdout.write(char)
            sys.stdout.flush()
     print

if __name__ == "__main__":
     unfold_message("hello, this is a test", 0.1)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
suspending vmplayer gil_r21 Linux - General 2 10-26-2007 10:37 AM
Python: raw_input & puTTy on remote server Hairein Programming 1 01-01-2007 08:25 AM
Problems when suspending ziller Linux - Networking 0 01-01-2006 12:20 PM
array logical operations in python? zero79 Programming 2 05-13-2005 03:56 AM
suspending in slack Syncrm Slackware 5 06-17-2002 09:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration