python interpreter throwing a type error where there is none
我正在尝试在PyGame中创建类似下拉列表的功能。程序由"Drop"类组成,其中包含文本并描述下拉菜单的一般用途。然后是包装类,它在下拉菜单中包含所有不同的标签。子类是标签,可以通过发生错误的"add_child"函数添加到包装类中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | import pygame as pg doc ="c:/users/user/documents/code/py/pg/project/" vec = pg.math.Vector2 class Drop(Sprite): def __init__(self, parent, **kwargs): self.parent = parent default = { "size":[100, 25], "pos":vec(0, 0), "bg":pg.Color("#ff0000"), } for key, value in default.items(): if key not in kwargs: kwargs[key] = value self.size = kwargs["size"] self.pos = vec(kwargs["pos"]) self.bg = kwargs["bg"] super().__init__() class Child(Sprite): def __init__(self, parent, **kwargs): self.parent = parent default = { "size":[100, 25], "pos":vec(0, 0), "bg":pg.Color("#00ff00") } for key, value in default.items(): if key not in kwargs and key !="name": kwargs[key] = value self.size = kwargs["size"] self.pos = vec(kwargs["pos"]) self.bg = kwargs["bg"] self.name = kwargs["name"] super().__init__() def render(self): self.parent.image.blit(self.image, self.rect) class Wraper(Sprite): def __init__(self, parent, index=1): self.index = index self.children = [] # childs which are labels with texts on it self.parent = { "0":parent.parent, "1":parent.parent.parent }[str(self.index)] self.y = { "0":parent.pos.y + parent.size[1], "1":parent.parent.pos.y + parent.parent.size[1] }[str(self.index)] self.size = [100, 100] self.bg = pg.Color("#000000") self.x = self.parent.pos.x self.pos = vec(self.x, self.y) super().__init__() # this is a custom sprite initialization class i'ts not the problem def add_child(self, **kwargs): self.children.append(Child(self, kwargs)) # this is where the interpreter throws the error # aperantly the child class takes 2 positional arguments # but i gave 3. but as you can see there are clearly only # two arguments given, the parent parameter and kwargs def render(self): if self.children: for child in self.children: child.render() if hasattr(self.parent,"image"): self.parent.image.blit(self.image, self.rect) else: self.parent.blit(self.image, self.rect) self.wraper = Wraper(self, 1) def render(self): self.wraper.render() self.parent.image.blit(self.image, self.rect) |
错误:
152,加上孩子self.children.append(child(self,kwargs))。类型错误:init()接受2个位置参数,但给出了3个
你要的是