Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
functool - partial errors
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  partial functions before knowing the values mikisDeWitte 4 536 Dec-24-2023, 10:00 AM
Last Post: perfringo
  Move Files based on partial Match mohamedsalih12 2 743 Sep-20-2023, 07:38 PM
Last Post: snippsat
  Partial KEY search in dict klatlap 6 1,198 Mar-28-2023, 07:24 AM
Last Post: buran
  remove partial duplicates from csv ledgreve 0 746 Dec-12-2022, 04:21 PM
Last Post: ledgreve
  Webhook, post_data, GPIO partial changes DigitalID 2 953 Nov-10-2022, 09:50 PM
Last Post: deanhystad
  Optimal way to search partial correspondence in a large dict genny92c 0 974 Apr-22-2022, 10:20 AM
Last Post: genny92c
  Partial Matching Rows In Pandas DataFrame Query eddywinch82 1 2,338 Jul-08-2021, 06:32 PM
Last Post: eddywinch82
  Partial key lookup in dictionary GaryNR 1 3,383 Jul-16-2020, 06:55 PM
Last Post: Gribouillis
  Partial using Tkinter function chesschaser 10 6,647 Jul-03-2020, 03:57 PM
Last Post: chesschaser
  Partial Word Search Kristenl2784 2 2,068 Jun-29-2020, 08:26 PM
Last Post: Kristenl2784

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020