将函数作为参数传递

时间:2014-04-16 10:24:59

标签: python function arguments

我知道这里有一些非常基本的错误,我无法看到。如果有经验丰富的Python人员可以指出我在这里误解了什么,我真的很感激。我在下面发布了我的代码的简化版本。要追溯问题的起点,请注意以下事项:

MainProgram,实例化为Main = MainProgram(),有一个菜单(self.menu = Startmenu()),它是Menu类的一个实例。这个Menu类有一个按钮列表(self.buttons = [...]) - 在这种情况下只有一个。它们是Button类的实例。我现在需要传递要在按钮被单击到Button类的实例时触发的函数,所以我实际上可以使用它。

我真的很感谢任何有经验的用户快速浏览一下。我确信我没有看到一些非常基本的东西。我敢打赌,我太累了,看不到它......因为现在家里有一个婴儿,所以只睡了2个小时:D

我的代码:

class MainProgram:
    #Much much more code
    def __init__(self):
        #And so much more code here
         self.menu = StartMenu()

class StartMenu:
    def __init__(self):
        #Much code which is not important to us here
        self.buttons = [Button("d_bt_start", (100, 60)
                                       , testfunc, "TEST-TEXT")]

class Button:
    def __init__(self, image, pos = (0, 0), path = Paths.path_img
                 , alpha = False, func = None, args = None):
        self.img_normal = helper.loadImage(path, image + "_normal.jpg", alpha)
        self.img_hover = helper.loadImage(path, image + "_hover.jpg", alpha)

        self.image = self.img_normal

        self.pos = pos

        self.x = pos[0]
        self.y = pos[1]
        self.width = self.x + self.image.get_width()
        self.height = self.y + self.image.get_height()

        self.mouseover = 0

        self.func = func
        self.args = args

    def update(self, (mx, my)):
        if (mx >= self.x and mx <= self.width and
            my >= self.y and my <= self.height):
            if self.mouseover == 0:
                self.image = self.img_hover
                self.mouseover = 1
        else:
            if self.mouseover == 1:
                self.image = self.img_normal
                self.mouseover = 0

    def clicked(self, button):
        if (mx >= self.x and mx <= self.width and
            my >= self.y and my <= self.height):
            if button == 1:
                self.func(self.args)

应该发生什么:主文件创建StartMenu类的实例。在StartMenu类中,我们将一个按钮定义为Button类的实例。该按钮应在单击时触发函数testfunc(args)。到目前为止,这个测试功能只是“TEST-TEXT”论点的印刷品。

如果我将该功能传递给

testfunc

然后我收到以下错误Traceback:

Traceback (most recent call last):
  File "D:\Python27\_RPG DEMO\DEMO\main.py", line 41, in <module>
   Main = MainProgram()
  File "D:\Python27\_RPG DEMO\DEMO\main.py", line 21, in __init__
    self.menu = menus.StartMenu()
  File "D:\Python27\_RPG DEMO\DEMO\menus.py", line 11, in __init__
    , zztestfile.testfunc, "TESTEST")
  File "D:\Python27\_RPG DEMO\DEMO\buttons.py", line 7, in __init__
    self.img_normal = helper.loadImage(path, image + "_normal.jpg", alpha)
  File "D:\Python27\_RPG DEMO\DEMO\helper.py", line 13, in loadImage
    return pygame.image.load(os.path.join(folder, name)).convert_alpha()
  File "D:\Python27\lib\ntpath.py", line 96, in join
    assert len(path) > 0
TypeError: object of type 'function' has no len()

如果我把它传递给:

testfunc("TEST-TEXT")

它会触发,但会导致以下追溯:

TEST-TEXT

Traceback (most recent call last):
  File "D:\Python27\_RPG DEMO\DEMO\main.py", line 41, in <module>
    Main = MainProgram()
  File "D:\Python27\_RPG DEMO\DEMO\main.py", line 21, in __init__
    self.menu = menus.StartMenu()
  File "D:\Python27\_RPG DEMO\DEMO\menus.py", line 11, in __init__
    , testfunc("TEST-TEXT"))
  File "D:\Python27\_RPG DEMO\DEMO\buttons.py", line 7, in __init__
    self.img_normal = helper.loadImage(path, image + "_normal.jpg", alpha)
  File "D:\Python27\_RPG DEMO\DEMO\helper.py", line 15, in loadImage
    return pygame.image.load(os.path.join(folder, name)).convert()
  File "D:\Python27\lib\ntpath.py", line 96, in join
    assert len(path) > 0
TypeError: object of type 'NoneType' has no len()

到目前为止,我已经完成了一些教程,但我真的无法弄清楚我在哪里误解了它们。我非常感谢你们的帮助!

1 个答案:

答案 0 :(得分:3)

您正在将测试函数传递给path类的 Button 参数:

self.buttons = [Button("d_bt_start", (100, 60)
                               , testfunc, "TEST-TEXT")]

那是第三个位置论点:

def __init__(self, image, pos = (0, 0), path = Paths.path_img
             , alpha = False, func = None, args = None):

path匹配。改为使用关键字参数:

self.buttons = [Button("d_bt_start", (100, 60)
                               , func=testfunc, args="TEST-TEXT")]