我知道这里有些非常基本的错误是我看不到的。如果有人对Python有更丰富的经验,可以指出我在这里误解了什么,我将非常感谢。我在下面发布了我的代码的简化版本。要追溯问题的根源,请注意以下几点:
的MainProgram
,作为实例化Main = MainProgram()
,有一个菜单(self.menu = Startmenu()
),它是的一个实例Menu
的类。此类Menu
具有一个按钮列表(self.buttons = [...]
)-在这种情况下只有一个。它们是Button
该类的一个实例。现在,我需要将单击按钮时要触发的功能传递给Button
该类的实例,这样我才能真正使用它。
我真的要感谢任何经验丰富的用户快速浏览它。我确定我看不到非常基本的东西。我敢打赌,我太累了,看不到它。。。因为家里有一个婴儿,所以只睡了两个小时: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")
它触发但导致以下回溯:
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()
到目前为止,我已经阅读了一些教程,但是我真的无法弄清楚我对它们的误解。我真的很感谢在此方面的任何帮助!
您正在将测试函数传递给类的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")]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句