Search code examples
pythonwhile-loopzelle-graphics

How to detect that the user has clicked on the screen in a GraphWin?


I'm trying to learn Python. I need this loop to keep rolling until I click on the screen.

I tried using win.getMouse but it's not working.

win = GraphWin("game of life", 600, 600)    
win.setCoords(-1, -3, s+1, s+1)

q = True
while q:
    board = iterate(board)
    x = 0
    while x < s:
        y = 0
        while y < s:
            update_color(x, y, board, rec)
            y = y + 1
        x = x + 1
    if win.getMouse():
        q = False

Solution

  • You can use checkMouse() instead. It basically checks if the user has clicked anywhere on the screen yet.

    win = GraphWin("game of life", 600, 600)  
    win.setCoords(-1, -3, s+1, s+1)
    
    q = True
    while q:
        board = iterate(board)
        x = 0
        while x < s:
            y = 0
            while y < s:
                update_color(x, y, board ,rec)
                y = y + 1
            x = x + 1
        if win.checkMouse() is not None:  # Checks if the user clicked yet
            q = False  # Better to use the break statement here
    

    Assuming http://mcsp.wartburg.edu/zelle/python/graphics/graphics/node2.html is correct and that the rest of your code is good.