Python Forum

Full Version: functool - partial errors
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am making a game - I didn't put it in Game Development because this is just about functools - and I have this button that I give a function for one of the paramters. TIA for you help.

This one works -
SpikeBatBT.optClick(gD, blue, lighter_blue, partial(giveItem, args=(SpikeBat)))
Why doesn't this one -
SlotBtn.optClick(partial(self.equipChange, args=(False, self.equips + (n,))))
Error -
Error:
TypeError: equipChange() got an unexpected keyword argument 'args'
BTW they both use different functions. One is an invisible button whilst the other is a visible button.
If you need it -
Function for the top one -
class Button():
    def __init__(self, rect, text, textsize, textcolor):
        self.rect = rect
        self.font = pygame.font.SysFont('arial', textsize)
        self.textSurf, self.textRect = Screen_Display.text_objects(text, self.font, textcolor)
        self.textRect.center = ((rect[0] + (rect[2]/2), rect[1] + (rect[3]/2)))

    def draw(self, gD, ButColor):
        pygame.draw.rect(gD, ButColor, (self.rect))
        gD.blit(self.textSurf, self.textRect)

    def optClick(self, gD, ShadowColor, ButColor, command=None, command2=None):
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if self.rect[0] + self.rect[2] > mouse[0] > self.rect[0] and self.rect[1] + self.rect[3] > mouse[1] > self.rect[1]:
            pygame.draw.rect(gD, ShadowColor, (self.rect))
            gD.blit(self.textSurf, self.textRect)
            if click[0] == 1:
                    if command != None:
                        command()
                    if command2 != None:
                        command2()
            else:
                pygame.draw.rect(gD, ButColor, (self.rect))
                gD.blit(self.textSurf, self.textRect)
Invisible Button -
class InvisButton():
    def __init__(self, rect):
        self.rect = rect

    def optClick(self, command=None, command2=None):
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if self.rect[0] + self.rect[2] > mouse[0] > self.rect[0] and self.rect[1] + self.rect[3] > mouse[1] > self.rect[1]:
            if click[0] == 1:
                    if command != None:
                        command()
                    if command2 != None:
                        command2()

Full code if you need it -
https://github.com/Sheepposu/Destination
I recommend using argument names:

for example func1(arg1="tomato", arg2=partial(blehbleh))

This may help because you may be passing partial to the wrong argument/parameter.