我正在尝试在pygame中创建类似功能的下拉菜单。该程序由“Drop”类组成,其上有文本,并描述了下拉菜单的一般用途。然后是包装类,其中包含下拉菜单中的所有不同标签。 Child类是标签,可以通过发送错误的“add_child”函数添加到包装类中。
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,在add_child中 self.children.append(儿童(自我,kwargs)) TypeError: init ()需要2个位置参数,但是给出了3个
答案 0 :(得分:2)
您想要Child(self, **kwargs)
,而不仅仅是Child(self, kwargs)
。当你想要一个关键字时,它试图将它作为参数发送。