0

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:

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

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 before the corresponding second click on the pygame surface that python prints out an output like :

('start', (166, 115))

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.

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.

joyalrj22
  • 115
  • 4
  • 12

2 Answers2

1

In the most basic form here's how you do it in pygame:

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

More information on how to handle pygame events can be found in the docs.

And here's how you do it in tkinter:

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("<Button-1>", print_pos)
root.mainloop()

More information on tkinter events can be found in effbots documentation.

I would suggest not putting break 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.

Your "EDIT: [...]" 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.

Graham
  • 7,431
  • 18
  • 59
  • 84
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
0

First I have to say that I don't know anything about pygame. However, if you are already using Tkinter, I could help you maybe: I would define a new function (let's call it mouse_click). In the button-click-function I would bind the new function to the game's surface. In the new function I print out the current mouse position:

def button_click(self):
    game_surface.bind("<Button-1>", self.mouse_click)

def mouse_click(self, event):
    print "X:", event.x
    print "Y:", event.y

I hope this is helpful. Please notice that you should modify this code to make it work in your program (using the correct widget names and so on). By the way, "Button-1" is the event identifier of the left mouse button.

Aemyl
  • 1,501
  • 1
  • 19
  • 34