this is my current button code in Pygame with Python 3.
def button(text, posX, posY, width, height, inactiveColor, activeColor,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if posX + width > mouse[0] > posX and posY + height > mouse[1] > posY:
pygame.draw.rect(displays, activeColor, (posX,posY, width, height))
if click[0] == 1 and action!= None:
action()
else:
pygame.draw.rect(displays,inactiveColor,(posX,posY,width,height))
textSurf, textRect = text_objects(text, smallfont)
textRect.center = ( (posX + (width/2)), (posY+(height/2)) )
displays.blit(textSurf, textRect)
At the moment this has some problems, and I have tried searching this site but without a proper fix.
When the button is clicked, it will register nearly 20 function calls. I know this is because it is essentially running the function whilst the mouse button is held down, even if this is for only half a second.
I tried utilising MOUSEBUTTONDOWN but failed.
Additionally, I am trying to find out a way to have an 'action' (i.e. a function) run with arguments but only if arguments are given.
Finally, I am also trying to find a way to run an exec call as the 'action' so I can change a variable within the function call.
For example:
usernameRectangle = button("",200,195,500,30,GREY,BRIGHTGREY,exec('usernameActiveCheck = True')
At the moment, this is not possible.
I understand this is asking quite a lot, but I would appreciate any help.
Thank you!
Edit:
def button(text, posX, posY, width, height, inactiveColor, activeColor,action=None):
mouse = pygame.mouse.get_pos()
#click = pygame.mouse.get_pressed()
click = pygame.MOUSEBUTTONDOWN
if posX + width > mouse[0] > posX and posY + height > mouse[1] > posY:
events = pygame.event.get()
pygame.draw.rect(displays, activeColor, (posX,posY, width, height))
hover = True
while hover:
for event in events:
if event.type == click and action!= None:
hover = False
print("hover false")
action()
if posX + width > mouse[0] > posX != posY + height > mouse[1] > posY:
hover = False
Is code that I tried, but it didnt not work. It also had a wierd bug of triggering the action function without the button even being clicked
Second edit:
def button(text, posX, posY, width, height, inactiveColor, activeColor,action=None,actionArgs=None):
global buttonDown
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if posX + width > mouse[0] > posX and posY + height > mouse[1] > posY:
pygame.draw.rect(displays, activeColor, (posX,posY, width, height))
if click[0] == 1 and not buttonDown and action!= None:
print("Clicked, button down and action")
if actionArgs!=None:
print("Actionargs")
action(actionArgs)
else:
action()
buttonDown = True
elif click[0] == 0:
buttonDown = False
else:
pygame.draw.rect(displays,inactiveColor,(posX,posY,width,height))
textSurf, textRect = text_objects(text, smallfont)
textRect.center = ( (posX + (width/2)), (posY+(height/2)) )
displays.blit(textSurf, textRect)
Code now working thanks to comment responses and trying different things.