1.11M Members

Press Any Key to Continue

 
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?

 
0
 

raw_input("Press any Key")

 
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
 
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")

 
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.

 
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)

 
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)

 
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 six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article