Graphics
Each task in an AxoPy experiment is given a Container
. The container
is effectively an empty QWidget
from Qt, so you can set up its contents
quite flexibly. That is, any valid QWidget
or QLayout
can be used as
the container’s contents, so you can create arbitrarily complex graphics for
a task.
To set up graphics for a task, override the Task.prepare_graphics
method, which takes the Container
as an input argument, then use Container.set_widget()
to establish the
main widget for the task.
from axopy.task import Task
class CanvasTask(Task):
def prepare_graphics(self, container):
# set up graphical widget/layout here
widget = ...
container.set_widget(widget)
While you can always set up completely custom graphics using PyQt5 classes directly, AxoPy includes some graphical elements commonly used in human-computer interface experiments, making it possible to write experiments without knowing how to use Qt.
Note
In the examples below, get_qtapp()
will be used to demonstrate
different graphical widgets and layouts. This function creates or retrieves
a QApplication
instance. We can then use app.exec_()
to run the Qt
event loop and test out the graphics code.
Built-In Graphics Widgets
Canvas Graphics
The axopy.gui.canvas
module contains a Canvas
class which can
be directly inserted into a container. You can then add items like
a Circle
or Text
to the canvas. In the context of a task, you
can create a canvas as follows:
from axopy.gui.main import get_qtapp
from axopy.gui.canvas import Canvas, Circle
app = get_qtapp()
canvas = Canvas()
canvas.add_item(Circle(0.1, color='red'))
canvas.show()
app.exec_()
All of the built-in items inherit from the Item
class, which means
they all have a number of properties that can be set, such as the position and
visibility.
canvas = Canvas()
circle = Circle(0.1)
canvas.add_item(circle)
# set the x coordinate
circle.x = 0.5
# read the y coordinate
y = circle.y
# hide the circle
circle.hide()
All of the Item
classes are actually just wrappers around
QGraphicsItem classes. In general, the various subclasses of QGraphicsItem
(e.g. QGraphicsEllipseItem
) have a large number of methods that may not be
exposed by AxoPy, so all items have a qitem
attribute pointing to
the underlying QGraphicsItem
. For example, the Line
item wraps
a QGraphicsLineItem. In AxoPy, a line is just a solid line with a specific cap
style. If you need to customize this behavior, you can use the qitem
attribute and dig into the Qt API:
from axopy.gui.canvas import Line
# horizontal line 0.4 units long
line = Line(-0.2, 0, 0.2, 0)