Python 将函数作为参数传递
我知道这里有一些非常基本的问题,我只是看不到。如果在Python方面有经验的人能指出我在这里误解的地方,我将不胜感激。我在下面发布了我的代码的简化版本。要追溯问题的起因,请注意以下几点: 实例化为Python 将函数作为参数传递,python,function,arguments,Python,Function,Arguments,我知道这里有一些非常基本的问题,我只是看不到。如果在Python方面有经验的人能指出我在这里误解的地方,我将不胜感激。我在下面发布了我的代码的简化版本。要追溯问题的起因,请注意以下几点: 实例化为Main=MainProgram()的Main程序有一个菜单(self.menu=Startmenu()),它是菜单类的实例。这个菜单类有一个按钮列表(self.buttons=[…]),在本例中只有一个按钮。它们是按钮类的一个实例。现在,我需要将单击按钮时要触发的函数传递到按钮类的实例,以便实际使用它
Main=MainProgram()
的Main程序
有一个菜单(self.menu=Startmenu()
),它是菜单
类的实例。这个菜单
类有一个按钮列表(self.buttons=[…]
),在本例中只有一个按钮。它们是按钮
类的一个实例。现在,我需要将单击按钮时要触发的函数传递到按钮
类的实例,以便实际使用它
我真的很感谢任何有经验的用户能快速浏览一下。我肯定我没有看到一些非常基本的东西。我打赌我太累了,看不到它。。。自从现在家里有婴儿以来,我只睡了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)
然后我得到以下错误回溯:
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()
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()
它会触发,但随后会导致以下回溯:
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()
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()
测试文本
回溯(最近一次呼叫最后一次):
文件“D:\Python27\\u RPG DEMO\DEMO\main.py”,第41行,在
Main=MainProgram()
文件“D:\Python27\\u RPG DEMO\DEMO\main.py”,第21行,在\uuu init中__
self.menu=menus.startmenus()
文件“D:\Python27\\ RPG DEMO\DEMO\menus.py”,第11行,在\uuu init中__
,testfunc(“测试文本”))
文件“D:\Python27\\ RPG DEMO\DEMO\buttons.py”,第7行,在\uuu init中__
self.img_normal=helper.loadImage(路径,image+“_normal.jpg”,alpha)
loadImage中第15行的文件“D:\Python27\\u RPG DEMO\DEMO\helper.py”
返回pygame.image.load(os.path.join(文件夹,名称)).convert()
文件“D:\Python27\lib\ntpath.py”,第96行,在join中
断言len(路径)>0
TypeError:类型为“NoneType”的对象没有len()
到目前为止,我已经阅读了一些教程,但我真的不知道我在哪里误解了它们。我真的很感激在这方面的任何帮助 您正在将测试函数传递给
按钮的路径
参数
类:
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):
与路径匹配
。改为使用关键字参数:
self.buttons = [Button("d_bt_start", (100, 60)
, func=testfunc, args="TEST-TEXT")]
这并不能回答您的问题,但Python2类应该始终继承自
对象
;在新代码中使用旧式类是个坏主意。@MartijnPieters:它是一个外部函数(来自另一个模块),用于加载图像并将其转换为pygame。因为这与问题无关,所以我在这里没有解释,但如果您感兴趣,我们也可以讨论。@Wooble:谢谢您的评论。我真的在拼命寻找关于这类事情的更深入的信息,因为教程似乎总是想当然地认为人们已经知道了它:/所以我非常感谢您提供的任何信息,我渴望更好地学习:D@PatricHartmann:不,这是一个骗局;在我意识到出了什么问题之前,我一直在想你是如何将一个函数传递给os.path.join()
函数的。非常感谢!在德语中,我们有一句谚语,有那么多的树,你再也看不到森林了。。。这就是发生在我身上的事情,你指引了我正确的方向。同样的谚语在英语中也适用(尽管我在荷兰语和德语中也知道):-)不以树见林。这很有趣,我对语言学很感兴趣:D我的另一个爱好,它没有得到应有的重视。。。顺便说一句:它现在很好用。。。我必须承认,犯这样的错误却找不到它,真是令人尴尬……:D