TargetItem#

class pyqtgraph.TargetItem(
pos=None,
size=10,
symbol='crosshair',
pen=None,
hoverPen=None,
brush=None,
hoverBrush=None,
movable=True,
label=None,
labelOpts=None,
)[source]#

Draws a draggable target symbol (circle plus crosshair).

The size of TargetItem will remain fixed on screen even as the view is zoomed. Includes an optional text label.

__init__(
pos=None,
size=10,
symbol='crosshair',
pen=None,
hoverPen=None,
brush=None,
hoverBrush=None,
movable=True,
label=None,
labelOpts=None,
)[source]#
Parameters:
  • pos (list, tuple, QPointF, QPoint, Optional) – Initial position of the symbol. Default is (0, 0)

  • size (int) – Size of the symbol in pixels. Default is 10.

  • pen (QPen, tuple, list or str) – Pen to use when drawing line. Can be any arguments that are valid for mkPen(). Default pen is transparent yellow.

  • brush (QBrush, tuple, list, or str) – Defines the brush that fill the symbol. Can be any arguments that is valid for mkBrush(). Default is transparent blue.

  • movable (bool) – If True, the symbol can be dragged to a new position by the user.

  • hoverPen (QPen, tuple, list, or str) – Pen to use when drawing symbol when hovering over it. Can be any arguments that are valid for mkPen(). Default pen is red.

  • hoverBrush (QBrush, tuple, list or str) – Brush to use to fill the symbol when hovering over it. Can be any arguments that is valid for mkBrush(). Default is transparent blue.

  • symbol (QPainterPath or str) – QPainterPath to use for drawing the target, should be centered at (0, 0) with max(width, height) == 1.0. Alternatively a string which can be any symbol accepted by setSymbol()

  • label (bool, str or Callable, optional) – Text to be displayed in a label attached to the symbol, or None to show no label (default is None). May optionally include formatting strings to display the symbol value, or a callable that accepts x and y as inputs. If True, the label is x = {: >.3n}\ny = {: >.3n} False or None will result in no text being displayed

  • labelOpts (dict) – A dict of keyword arguments to use when constructing the text label. See TargetLabel and TextItem

label()[source]#

Provides the TargetLabel if it exists

Returns:

If a TargetLabel exists for this TargetItem, return that, otherwise return None

Return type:

TargetLabel or None

pos()[source]#

Provides the current position of the TargetItem

Returns:

pg.Point of the current position of the TargetItem

Return type:

Point

setBrush(
*args,
**kwargs,
)[source]#

Set the brush that fills the symbol. Allowable arguments are any that are valid for mkBrush().

setHoverBrush(
*args,
**kwargs,
)[source]#

Set the brush that fills the symbol when hovering over it. Allowable arguments are any that are valid for mkBrush().

setHoverPen(
*args,
**kwargs,
)[source]#

Set the pen for drawing the symbol when hovering over it. Allowable arguments are any that are valid for mkPen().

setLabel(
text=None,
labelOpts=None,
)[source]#

Method to call to enable or disable the TargetLabel for displaying text

Parameters:
  • text (Callable or str, optional) – Details how to format the text, by default None If None, do not show any text next to the TargetItem If Callable, then the label will display the result of text(x, y) If a fromatted string, then the output of text.format(x, y) will be displayed If a non-formatted string, then the text label will display text, by default None

  • labelOpts (dict, optional) – These arguments are passed on to TextItem

setPen(
*args,
**kwargs,
)[source]#

Set the pen for drawing the symbol. Allowable arguments are any that are valid for mkPen().

setPos(
*args,
)[source]#

Method to set the position to (x, y) within the plot view

Parameters:

args (tuple or list or QPointF or QPoint or Point or float) – Two float values or a container that specifies (x, y) position where the TargetItem should be placed

Raises:

TypeError – If args cannot be used to instantiate a Point

TargetLabel#

class pyqtgraph.TargetLabel(
target,
text='',
offset=(20, 0),
anchor=(0, 0.5),
**kwargs,
)[source]#

A TextItem that attaches itself to a TargetItem.

This class extends TextItem with the following features :
  • Automatically positions adjacent to the symbol at a fixed position.

  • Automatically reformats text when the symbol location has changed.

Parameters:
  • target (TargetItem) – The TargetItem to which this label will be attached to.

  • text (str or Callable, Optional) – Governs the text displayed, can be a fixed string or a format string that accepts the x, and y position of the target item; or be a callable method that accepts a tuple (x, y) and returns a string to be displayed. If None, an empty string is used. Default is None

  • offset (tuple or list or QPointF or QPoint) – Position to set the anchor of the TargetLabel away from the center of the target in pixels, by default it is (20, 0).

  • anchor (tuple or list or QPointF or QPoint) – Position to rotate the TargetLabel about, and position to set the offset value to see TextItem for more information.

  • kwargs (dict) – kwargs contains arguments that are passed onto TextItem constructor, excluding text parameter

__init__(
target,
text='',
offset=(20, 0),
anchor=(0, 0.5),
**kwargs,
)[source]#
setFormat(
text,
)[source]#

Method to set how the TargetLabel should display the text. This method should be called from TargetItem.setLabel directly.

Parameters:

text (Callable or str) – Details how to format the text. If Callable, then the label will display the result of text(x, y) If a fromatted string, then the output of text.format(x, y) will be displayed If a non-formatted string, then the text label will display text