Python - how do I get the ENTER key to work in a Python window animation?

RavinDJ

Supreme [H]ardness
Joined
Apr 9, 2002
Messages
4,446
So I have the following code:
Code:
from graphics import *
import random, time
import math
import curses

def main():
    win=GraphWin("CONTINUE?", 800, 800)
    isYesSelected = "0"
    while True:
        rect = Rectangle(Point(190,190), Point (310,310))
        rect.draw(win)
        rect.setFill('black')
        rect.setOutline('black')
        rect = Rectangle(Point(200,200), Point (300,300))
        rect.draw(win)
        rect.setFill('green')
        rect.setOutline('green')
        rect = Rectangle(Point(490,190), Point (610,310))
        rect.draw(win)
        rect.setFill('white')
        rect.setOutline('white')
        rect = Rectangle(Point(500,200), Point (600,300))
        rect.draw(win)
        rect.setFill('red')
        rect.setOutline('red')
        win.nodelay("1")
        win.getch()

        time.sleep(0.5)

        rect = Rectangle(Point(190,190), Point (310,310))
        rect.draw(win)
        rect.setFill('white')
        rect.setOutline('white')
        rect = Rectangle(Point(200,200), Point (300,300))
        rect.draw(win)
        rect.setFill('green')
        rect.setOutline('green')
        rect = Rectangle(Point(490,190), Point (610,310))
        rect.draw(win)
        rect.setFill('black')
        rect.setOutline('black')
        rect = Rectangle(Point(500,200), Point (600,300))
        rect.draw(win)
        rect.setFill('red')
        rect.setOutline('red')

        time.sleep(0.5)

main()

I'd like the user to press ENTER when the black box is around either GREEN or RED. But how do I have Python ready for a key input (Python 3.x on OS X) and have it quit the loop with the isYesSelected be 1 or 0.

Thanks!!
 
I wanted to go for something like the dude in the Hackers movie had... where the selection goes from one box to the other and you had to hit ENTER at the right time to either RUN or QUIT the program.
 
Back
Top