0

Okay so basically I have some code that checks to see if a "finish" button is clicked. the way I have it set up is a while loop that has an event for loop to see if the button is clicked by checking the coordinates of the click. when the done button is clicked it should call my next function and the button should disappear (which it does, Its just the last while loop is still listening for my clicks or something). BUT for some reason when I go to the next screen where my results are printing and click in the coordinates of the button (even tho its not there) it will still click it and show up again.

Here is some pseudo-code of what its like:

   def findSomething():
      #main block of code more than what is here 
      #I am just showing how I set up the surface
      surface = pygame.display.set_mode((400, 300))
      background_colour = (18,18,19)
      surface.fill(background_colour)

      #button code
      while running:
         pygame.display.flip()
         for event in pygame.event.QUIT:
            if event.type == pygame.QUIT:
               pygame.quit()
               quit()
            if event.type == pygame.MOUSEBUTTONDOWN:
               x,y = pygame.mouse.get_pos()
               if 125 <= x <= 275 and 190 <= y <= 240:
                  print("clicked done")
                  printOptions()
   
   def printOptions():
      surface = pygame.display.set_mode((400, 700))
      background_colour = (18,18,19)
      surface.fill(background_colour)
      phraseGuess = buttonFont.render(("Remaining Words: "), True, white)
      surface.blit(phraseGuess, (25, 50))
      #code for printing stuff out

so Im not sure why its still checking for clicks after i call printOptions(). is there a way to exit the loop while simultaneously calling printOptions() and still having it work? Or is there something I can do at the top of the printOptions function to kind of reset the display and stop looking for clicks in that certain spot?

will cuny
  • 11
  • 4
  • *"is there a way to exit the loop while simultaneously calling printOptions() and still having it work?"* - No. Never run loops waiting for something inside the application loop. Use the application loop. See [Pygame mouse clicking detection](https://stackoverflow.com/questions/10990137/pygame-mouse-clicking-detection/64533684#64533684) – Rabbid76 Aug 11 '22 at 18:15

1 Answers1

0

You could stop this while loop from continuing to execute after printOptions by setting running to False, or by using 'break' inside the while loop body.

With break:

if 125 <= x <=275 and 190 <= y <= 240:
    print("clicked done")
    printOptions()
    break #ends execution of the loop after printOptions() runs
    

Or by setting running to False:

if 125 <= x <=275 and 190 <= y <= 240:
    print("clicked done")
    printOptions()
    running = False #loop condition will fail next iteration, so it will stop
Addison Schmidt
  • 374
  • 1
  • 7
  • I have tried that. What happens when I do that is the printOptions() just runs and I seems like that while loop is somehow still running in the background (hence still being able to click on those coordinates). Within the printOptions function I have a similar process with another while loop and button so when I call the function its not just a one and done thing, it is running for a while. – will cuny Aug 11 '22 at 18:21
  • So basically with that method it just runs through the whole printOptions() function and THEN goes back to that break after I'm done with the printOptions(). so findSomething() is just continuously running in the background... I think. python isn't my first language – will cuny Aug 11 '22 at 18:28
  • Unless you're multi threading or using async, it should only be possible for one function/loop to be executing at a time. – Addison Schmidt Aug 11 '22 at 18:33