I've made a class that contains a circle (a Psychopy Circle object). I want to know how I can instantiate 2 circle objects using this class, each for example, with a different fill color.
class Circle(object):
def __init__(self):
self.circle = visual.Circle(win, units = 'deg', pos=(1,1),
radius=1, lineColor="black", fillColor="red")
self.components = [self.circle]
def draw(self):
[component.draw() for component in self.components]
circle=Circle() #red colour
circle2=Circle() #blue colour if possible
Is there a way for me to instantiate circle2 whilst accessing some of the visual.circle parameters e.g. to change it's position or fill color? This is my first use of classes. Currently, If I draw 'circle' and 'cirle2' to the screen, one simply overlays the other, as one is merely a copy of the other.
Cheers, Jon
Based on your clarification in the comment, I assume you want something like this:
class Circle(psychopy.visual.circle.Circle):
def __init__(self, win, lineColor='black', fillColor='red'):
super(Circle, self).__init__(
win=win, lineColor=lineColor, fillColor=fillColor, units='deg',
pos=(1,1), radius=1)
Circle
would then default to units='deg'
, pos=(1,1)
, and radius=1
. You could, however, specify different lineColor
s and fillColor
s for each instance. Since Circle
inherits from the PsychoPy visual.Circle
class, it has all its features. The call to super()
actually initializes the parent class. See e.g. this post for more information on the super()
function.
Let's put this to work.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from psychopy import core, visual, monitors
import psychopy.visual.circle
class Circle(psychopy.visual.circle.Circle):
def __init__(self, win, lineColor='black', fillColor='red'):
super(Circle, self).__init__(
win=win, lineColor=lineColor, fillColor=fillColor, units='deg',
pos=(1,1), radius=1)
def main():
# Create a temporary monitor configuration.
monitor = monitors.Monitor('test_display')
monitor.setWidth(60)
monitor.setDistance(80)
monitor.setSizePix((1440, 900))
win = visual.Window(monitor=monitor)
colors = ('red', 'green', 'blue')
circles = [Circle(win=win, fillColor=color) for color in colors]
for circle in circles:
circle.draw()
win.flip()
core.wait(1)
core.quit()
if __name__ == '__main__':
main()
This code will create three Circle
s with different colors, and display them one after the other. I had to create a temporary monitor configuration or else PsychoPy would refuse to open a Window
on my current computer.