我知道这里非常基本的东西是错误的,我只是看不到。如果有人在Python方面有经验,可以指出我在这里误解了什么地方,我真的很感激。我在下面发布我的代码的简化版本。追溯问题开始的地方,对下列的眼睛:将函数作为参数传递
的MainProgram
,实例化作为Main = MainProgram()
,有一个菜单(self.menu = Startmenu()
),这是Menu
类的一个实例。这个Menu
类有一个按钮列表(self.buttons = [...]
) - 在这种情况下只有一个。它们是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 (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")
它触发但随后的原因以下Traceback:
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()
到目前为止我已经经历了一些教程,但我真的无法弄清楚我误解他们的地方。我非常感谢这方面的帮助!
这并不回答你的问题,但Python 2类应该从'object'继承;在新代码中使用旧式类是一个糟糕的想法。 – geoffspear
@MartijnPieters:这是一个外部函数(来自另一个模块),它加载图像并将它们转换为pygame。既然它与这个问题无关,我在这里没有解释任何关于它的问题,但如果你有兴趣,我们也可以讨论它。 –
@Wooble:感谢您的评论。我真的非常拼命地寻找更深入的信息,因为看起来教程总是认为人们已经知道它:/所以我非常感谢你提供的任何信息,我很想学习更好:D –