'how to remove draw objects from pygame window?

I am creating a game using pygame and python. I have drawn a rectangle in the window which acts as a button. But i need to know how to remove the button once it is clicked. Heres my code:

import pygame, sys
from pygame.locals import *
x = 0
y = 0

#Basic stuff:
pygame.init()
screen=pygame.display.set_mode((640,360),0,32)
pygame.display.set_caption("Some random Title")


while True:
    evc = pygame.event.get()
    for event in evc:
        if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
x,y = pygame.mouse.get_pos()
screen.lock()
#draw button:
my_rect = pygame.draw.rect(screen,(205,201,201),Rect((245,40),(130,80)))
bf1 = pygame.font.SysFont("monospace", 15)
bl = bf1.render("Play!!!", 1, (255, 255, 255))
screen.unlock()
#Check mouse click!!!
if my_rect.collidepoint(x,y):
   for event in evc:
       if event.type ==pygame.MOUSEBUTTONUP:
           screen.fill((255,255,255))



screen.blit(bl, (280, 70))
pygame.display.flip
pygame.display.update()

So yea i hope you can help me with this.



Solution 1:[1]

You would need to draw everything to the screen again except for the parts you don't want in any more.

pygame.init()
screen = pygame.display.set_mode((700, 700))
# set background or fill
background = pygame.image.load("/background.png")
screen.blit(background, (0, 0))    

my_btn = pygame.image.load("/button.png")
btn_rect = my_btn.get_rect()
screen.blit(my_btn, (0, 0))

for evt in pygame.event.get():

    if evt.type == pygame.MOUSEBUTTONDOWN:
        pos = pygame.mouse.get_pos()
        if btn_rect.collidepoint(pos):
            # do stuff
            # apply your background fill or image
            screen.blit(background, (0, 0))
            for element in elements_you_want_on_screen:
                screen.blit(element, (location))
    pygame.display.flip()

If you don't have an array of surfaces to blit:

pygame.init()
screen = pygame.display.set_mode((700, 700))
# set background or fill
background = pygame.image.load("/background.png")
screen.blit(background, (0, 0))    

my_btn = pygame.image.load("/button.png")
btn_rect = my_btn.get_rect()
screen.blit(my_btn, (0, 0))

for evt in pygame.event.get():

    if evt.type == pygame.MOUSEBUTTONDOWN:
        pos = pygame.mouse.get_pos()
        if btn_rect.collidepoint(pos):
            # do stuff
            # apply your background fill or image
            screen.blit(background, (0, 0))
            screen.blit(your_image, (location))
            # ...and others if you have them
    pygame.display.flip()

Solution 2:[2]

i think you should just create a blank screen for every frame and draw everything over again...

def draw(self):
self.screen.fill(self.color) #clear screen
self.draw(self.screen) # draw updated screen

if you have different classes in your code try adding that part in too:

self.#class#.draw(self.screen)

Solution 3:[3]

I just found out but you can also just set the width and height of your rectangle to 0, and draw it again. In my opinion, is that way easier.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Theo Boston
Solution 3 ouflak