[solved] Creating a derived class from canvas
Hi Sebastiaan,
While trying to build a prototype for a new experiment, I try to develop a new class that is derived from canvas, as illustrated in the code snippet below. The problem is that the code crashes on the 'my_canvas.draw_dashed_circle()' call on the second-to-last line.
To check whether I can actually define and use new classes in inline scripts, I defined a simple test case: EenClass, created an instance of it, and succesfully used that class to print a debug message. So, the principle seems to work, but I'm rather at a loss why my class that is derived from canvas doesn't.
Cheers,
Durk
While trying to build a prototype for a new experiment, I try to develop a new class that is derived from canvas, as illustrated in the code snippet below. The problem is that the code crashes on the 'my_canvas.draw_dashed_circle()' call on the second-to-last line.
To check whether I can actually define and use new classes in inline scripts, I defined a simple test case: EenClass, created an instance of it, and succesfully used that class to print a debug message. So, the principle seems to work, but I'm rather at a loss why my class that is derived from canvas doesn't.
Cheers,
Durk
from openexp.canvas import canvas
class DtCanvas (canvas):
def draw_dashed_circle(self):
x=xcenter()
y=ycenter()
set_penwidth(5)
circle(x,y, 320, fill=False, color="red")
class EenClass:
def printADebugMessage(self):
print 'Dit is een test'
my_canvas = DtCanvas(self.experiment)
een_bericht = EenClass()
een_bericht.printADebugMessage()
x = my_canvas.xcenter()
y = my_canvas.ycenter()
my_canvas.set_penwidth(5)
counter = 0
draw = True;
for i in range(0, 1500):
my_canvas.clear()
if (counter > 5) :
counter = 0
if (draw == True):
draw = False
else:
draw = True
else:
counter = counter+1
if (draw == True):
#my_canvas.circle(x,y, 320, fill=False, color="red")
my_canvas.draw_dashed_circle()
my_canvas.show()
Comments
Hi Durk,
Welcome to the forum!
Yes, I can see why you're puzzled. The reason why it doesn't work specifically for the canvas class is that this class employs 'type morphing'. There actually a number of different canvasses (openexp._canvas.legacy.legacy, openexp._canvas.opengl.opengl, and openexp._canvas.psycho.psycho) and the openexp.canvas class is kind of a shape shifter that transforms into the appropriate class, based on your experiment settings
You can see how this works here:
https://github.com/smathot/OpenSesame/blob/master/openexp/canvas.py
The solution in your case would be to have DtCanvas inherit one of the "real" classes, such as openexp._canvas.legacy:
from openexp._canvas.legacy import legacy
class DtCanvas (legacy):
...
Hope this helps!
Regards,
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Yep, works like a charm now. Thanks!