Getting position of user click in pygame
<p>I have a pygame window embedded in a a frame in tkinter. In another frame I have a button which calls the following function when clicked:</p>

<pre><code>def setStart():
global start
# set start position
for event in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONUP:
        start = event.pos
        print("start",start)
        break
</code></pre>

<p>I am intending for the program to print the position of the place where the user clicked on the pygame surface after the button is clicked. However on the first click of the button and the following click of the pygame surface there is no output. It is on the second click of the button <em>before the corresponding second  click on the pygame surface</em> that python prints out an output like : </p>

<p>('start', (166, 115))</p>

<p>How can I get it to give me a result right after the click on the pygame surface? I had the same problem when I had two seperate tkinter and pygame windows so the embedding of pygame into tkinter is unlikely to be the cause of the problem.</p>

<p>EDIT: after further testing it appears that if the button is pressed and then the pygame surface is clicked on multiple times, upon a second click of the button the coordinates of all of these clicks are printed out as a batch. </p>

<p>In the most basic form here's how you do it in pygame:</p>

<pre><code>import pygame
pygame.init()

screen = pygame.display.set_mode((100, 100))
clock = pygame.time.Clock()

while True:
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONUP:  # or MOUSEBUTTONDOWN depending on what you want.
            print(event.pos)
        elif event.type == pygame.QUIT:
            quit()

    pygame.display.update()
</code></pre>

<p>More information on how to handle pygame events can be found in the <a href="http://www.pygame.org/docs/ref/event.html" rel="nofollow">docs</a> or in the <a class='doc-link' href="http://stackoverflow.com/documentation/pygame/5110/event-handling/18046/event-loop#t=201609231214073595012">Stackoverflow documentation</a>.</p>

<p>And here's how you do it in tkinter:</p>

<pre><code>try:
    import tkinter as tk  # Python 3
except ImportError:
    import Tkinter as tk  # Python 2

root = tk.Tk()

def print_pos(event):
    print(event.x, event.y)

root.bind("&lt;Button-1&gt;", print_pos)
root.mainloop()
</code></pre>

<p>More information on tkinter events can be found in <a href="http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm" rel="nofollow">effbots documentation</a>.</p>

<p>I would suggest <strong>not</strong> putting <code>break</code> in an event loop in pygame. Doing so makes all other events go un-handled, meaning that it's possible for the program to not respond to certain events.</p>

<p>Your <em>"EDIT: [...]"</em> is unfortunately wrong, given the code you've given us. I had no problem with the code; it printed the position of where I released the mouse button and always printed just one position. So there have to be a logical error somewhere else in your code.</p>

python python-2.7 tkinter pygame pygame-surface