0

I am trying to write a module for a button so that I don't have to rewrite the code a lot but I keep experiencing the same issue.

import pygame, sys
import tkinter
from collections import Counter as count
from pygame.locals import *

click = bool
pygame.init()
mainClock = pygame.time.Clock()
pygame.display.set_caption('CreditCard')
screen = pygame.display.set_mode((1200,800),0,32)
windowSurface = pygame.display.set_mode((1200,650),pygame.DOUBLEBUF)
font = pygame.font.SysFont(None, 35)
font2 = pygame.font.SysFont(None, 20)
ic = pygame.Color("gray15")
ac = pygame.Color(25,255,25)

PinNumber = ""
input_rect = pygame.Rect(25,25,25,25)
colour = pygame.Color('gray15')
keys = pygame.key.get_pressed()
click = pygame.MOUSEBUTTONDOWN

def card():

    global click
    global colour
    global font
    global PinNumber

    def button(Num, X, Y, W, H, Action):
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                click = True

        button_Num = pygame.Rect(X, Y, W, H)

        if button_Num.collidepoint(mouse):
            if pygame.mouse.get_pressed():
                for i in 2:
                    Action
                    i=i+1
        

    mx, my = pygame.mouse.get_pos()



    
    while True:

        for event in pygame.event.get():
            if event.type == QUIT:
                print("Quit")
                pygame.quit()
                sys.exit()


#Change input_rect to the desired box
            if event.type == click:
                
                if input_rect.collidepoint(event.pos):
                    print("click")

            button(1,30,30,100, 100, print("Clicked Button"))
                    
                    
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit()

        screen.fill((255,255,255))

        Image_load = pygame.image.load("Card_Scanner.jpg")
        Image_load = pygame.transform.scale(Image_load, (303, 534))
        screen.blit(Image_load, (90,90))

        click = False

        pygame.display.update()
        mainClock.tick(60)


def Quit():
    running = True
    while running:
        pygame.quit()
        pygame.display.update()
        mainClock.tick(60)






card()
pygame.quit()
sys.exit()

When running this, it doesn't stop printing Clicked Button while the mouse is moving I have also tried running other examples found online to the same problem Any help is really appreciated. Thanks

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

1

You cannot pass a called function. By passing print("Clicked Button") what you are really doing is printing "Clicked button" then passing the result of the function (None). Instead what you need to do is pass the action function itself and the parameters for the action function, then call the action function from inside the button function.

Here is a basic example

#                                 unnamed params  named params
def any_function(action_function, action_args=[], action_kwargs={}):
  # This will call the action function
  action_function(*action_args, **action_kwargs)


# What you are looking for
any_function(print, ["Clicked Button"])

# Example using `action_kwargs`
any_function(print, ["Clicked Button"], {"end": " "})

Implemented in your button function

def button(Num, X, Y, W, H, Action, Action_args=[], Action_kwargs={}):
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                click = True

        button_Num = pygame.Rect(X, Y, W, H)

        if button_Num.collidepoint(mouse):
            if pygame.mouse.get_pressed():
                for i in 2:
                    Action(*Action_args, **Action_kwargs)
                    i=i+1
button(1,30,30,100, 100, print, ["Clicked Button"])
  • Hi, I tried this but it still keeps printing button clicked when the mouse is hovered over –  Mar 25 '21 at 01:05
  • 1
    I believe your issue lies in the use of `pygame.mouse.get_pressed()`. I recommend putting the action call under `if event.type == MOUSEBUTTONDOWN:` if you want to see i the mouse has been pressed. –  Mar 25 '21 at 22:18