1,105,585 Community Members

Press Any Key to Continue

Member Avatar
FreezeBlink
Light Poster
26 posts since Apr 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

A classic of early text-based interfaces... Any way to do it in Python? I tried getch(), but it doesn't really work (for instance, if you hit Enter to confirm a menu selection, and then it goes to a "press any key" thing with getch(), getch() will pick up the Enter keypress.)

Any ways around this?

Member Avatar
jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 77 [?]
Q&As Helped to Solve: 160 [?]
Skill Endorsements: 0 [?]
 
0
 

raw_input("Press any Key")

Member Avatar
woooee
Posting Maven
2,796 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
0
 

The simpliest way is an easygui or Tkinter/PMW dialog box with one button. However, this is something found on the web long ago. It is probably also possible to do with ncurses.

import termios, sys, os
#import termios, TERMIOS, sys, os
TERMIOS = termios
def getkey():
        fd = sys.stdin.fileno()
        old = termios.tcgetattr(fd)
        new = termios.tcgetattr(fd)
        new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
        new[6][TERMIOS.VMIN] = 1
        new[6][TERMIOS.VTIME] = 0
        termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
        c = None
        try:
                c = os.read(fd, 1)
        finally:
                termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
        return c

if __name__ == '__main__':
        print 'type something'
        s = ''
        while 1:
                c = getkey()
                if c == 'q':
                        break
                print 'got ("q" = end)', c
                s = s + c

        print s
Member Avatar
Shadow14l
Light Poster
39 posts since May 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

raw_input("Press any Key")

I would use this function, but for the text I would prefer using:

raw_input("Press Enter to Exit")

Member Avatar
Scooter789
Newbie Poster
1 post since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
# press any key to continue function
def anykey(prompt="Press any key to continue...", failChars=""):
    '''
    Displays a prompt to the user, then waits for the user to press a key.
    
    Accepts a string for prompt, and a string containing all characters for which it should return False.
    
    Returns False if the char pressed was in the failChars string, True otherwise.
    
    Raises KeyboardInterrupt on Ctrl + C'''
    from msvcrt import getch, kbhit
    print prompt,
    ch = getch()
    while kbhit():
        getch()
    if ch == '\x03':
        raise KeyboardInterrupt
    else:
        return (ch not in failChars)
# end anykey definition

I made it return a bool so it is suitable for using in a loop. For example...

while anykey("Press 'q' to quit.", 'qQ'):
    pass # keep prompting until user presses 'q' or 'Q'

It also deals with the problem of getch() not handling Ctrl+C properly. It's a little messy, but it works, and it's not extremely complicated.

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

The Tkinter GUI toolkit that installs with just about every Python version, has some power under the hood even for console applications. See:
http://www.daniweb.com/forums/post1288813.html#post1288813

Member Avatar
mrBlok
Newbie Poster
2 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

assuming it's in a console window (and windows)

import os
os.system("PAUSE")

or if you want Tkinter:

import tkMessageBox as tkmb
tkmb.showinfo("Alert", "Press OK  to continue")

(tkMessageBox should already be included with Tkinter)

Member Avatar
mrBlok
Newbie Poster
2 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

assuming it's in a console window (and windows)

import os
os.system("PAUSE")

or if you want Tkinter:

import tkMessageBox as tkmb
tkmb.showinfo("Alert", "Press OK to continue")

(tkMessageBox should already be included with Tkinter)

Member Avatar
richieking
Posting Shark
926 posts since Jun 2009
Reputation Points: 40 [?]
Q&As Helped to Solve: 172 [?]
Skill Endorsements: 3 [?]
 
0
 

why not also in wxpython....

import wx

app=wx.App()

msg= wx.MessageBox("Are you sure you want to quit?","Press yes To quit",style=wx.YES_NO)
if msg == wx.ID_OK:
  msg.Destroy()
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article