Commit 43849449 authored by Luke Campagnola's avatar Luke Campagnola
Browse files

Cleaned up parametertree example

parent 2213dea9
......@@ -11,7 +11,7 @@ examples = OrderedDict([
('Command-line usage', ''),
('Basic Plotting', ''),
('ImageView', ''),
('ParameterTree', '../parametertree'),
('ParameterTree', ''),
('Crosshair / Mouse interaction', ''),
('Video speed test', ''),
('Plot speed test', ''),
## tests for ParameterTree
# -*- coding: utf-8 -*-
This example demonstrates the use of pyqtgraph's parametertree system. This provides
a simple way to generate user interfaces that control sets of parameters. The example
demonstrates a variety of different parameter types (int, float, list, etc.)
as well as some customized parameter types
## make sure pyqtgraph is in path
import sys,os
md = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(md, '..', '..'))
import initExample ## Add path to library (just for examples; you do not need this)
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import collections
app = QtGui.QApplication([])
import pyqtgraph.parametertree.parameterTypes as pTypes
......@@ -14,11 +22,11 @@ from pyqtgraph.parametertree import Parameter, ParameterTree, ParameterItem, reg
## test subclassing parameters
## This parameter automatically generates two child parameters which are always reciprocals of each other
class ComplexParameter(Parameter):
class ComplexParameter(pTypes.GroupParameter):
def __init__(self, **opts):
opts['type'] = 'bool'
opts['value'] = True
Parameter.__init__(self, **opts)
pTypes.GroupParameter.__init__(self, **opts)
self.addChild({'name': 'A = 1/B', 'type': 'float', 'value': 7, 'suffix': 'Hz', 'siPrefix': True})
self.addChild({'name': 'B = 1/A', 'type': 'float', 'value': 1/7., 'suffix': 's', 'siPrefix': True})
......@@ -82,42 +90,58 @@ registerParameterType('text', TextParameter)
params = [
{'name': 'Group 0', 'type': 'group', 'children': [
{'name': 'Param 1', 'type': 'int', 'value': 10},
{'name': 'Param 2', 'type': 'float', 'value': 10},
{'name': 'Group 1', 'type': 'group', 'children': [
{'name': 'Param 1.1', 'type': 'float', 'value': 1.2e-6, 'dec': True, 'siPrefix': True, 'suffix': 'V'},
{'name': 'Param 1.2', 'type': 'float', 'value': 1.2e6, 'dec': True, 'siPrefix': True, 'suffix': 'Hz'},
{'name': 'Group 1.3', 'type': 'group', 'children': [
{'name': 'Param 1.3.1', 'type': 'int', 'value': 11, 'limits': (-7, 15), 'default': -6},
{'name': 'Param 1.3.2', 'type': 'float', 'value': 1.2e6, 'dec': True, 'siPrefix': True, 'suffix': 'Hz', 'readonly': True},
{'name': 'Basic parameter data types', 'type': 'group', 'children': [
{'name': 'Integer', 'type': 'int', 'value': 10},
{'name': 'Float', 'type': 'float', 'value': 10.5, 'step': 0.1},
{'name': 'String', 'type': 'str', 'value': "hi"},
{'name': 'List', 'type': 'list', 'values': [1,2,3], 'value': 2},
{'name': 'Named List', 'type': 'list', 'values': {"one": 1, "two": 2, "three": 3}, 'value': 2},
{'name': 'Boolean', 'type': 'bool', 'value': True, 'tip': "This is a checkbox"},
{'name': 'Color', 'type': 'color', 'value': "FF0", 'tip': "This is a color button"},
{'name': 'Subgroup', 'type': 'group', 'children': [
{'name': 'Sub-param 1', 'type': 'int', 'value': 10},
{'name': 'Sub-param 2', 'type': 'float', 'value': 1.2e6},
{'name': 'Param 1.4', 'type': 'str', 'value': "hi"},
{'name': 'Param 1.5', 'type': 'list', 'values': [1,2,3], 'value': 2},
{'name': 'Param 1.6', 'type': 'list', 'values': {"one": 1, "two": 2, "three": 3}, 'value': 2},
ScalableGroup(name="ScalableGroup", children=[
{'name': 'ScalableParam 1', 'type': 'str', 'value': "hi"},
{'name': 'ScalableParam 2', 'type': 'str', 'value': "hi"},
{'name': 'Param 5', 'type': 'bool', 'value': True, 'tip': "This is a checkbox"},
{'name': 'Param 6', 'type': 'color', 'value': "FF0", 'tip': "This is a color button. It cam be renamed.", 'renamable': True},
{'name': 'TextParam', 'type': 'text', 'value': 'Some text...'},
{'name': 'Numerical Parameter Options', 'type': 'group', 'children': [
{'name': 'Units + SI prefix', 'type': 'float', 'value': 1.2e-6, 'step': 1e-6, 'siPrefix': True, 'suffix': 'V'},
{'name': 'Limits (min=7;max=15)', 'type': 'int', 'value': 11, 'limits': (7, 15), 'default': -6},
{'name': 'DEC stepping', 'type': 'float', 'value': 1.2e6, 'dec': True, 'step': 1, 'siPrefix': True, 'suffix': 'Hz'},
{'name': 'Extra Parameter Options', 'type': 'group', 'children': [
{'name': 'Read-only', 'type': 'float', 'value': 1.2e6, 'siPrefix': True, 'suffix': 'Hz', 'readonly': True},
{'name': 'Renamable', 'type': 'float', 'value': 1.2e6, 'siPrefix': True, 'suffix': 'Hz', 'renamable': True},
{'name': 'Removable', 'type': 'float', 'value': 1.2e6, 'siPrefix': True, 'suffix': 'Hz', 'removable': True},
ComplexParameter(name='Custom parameter group (reciprocal values)'),
ScalableGroup(name="Expandable Parameter Group", children=[
{'name': 'ScalableParam 1', 'type': 'str', 'value': "default param 1"},
{'name': 'ScalableParam 2', 'type': 'str', 'value': "default param 2"},
{'name': 'Custom parameter class (text box)', 'type': 'text', 'value': 'Some text...'},
#p = pTypes.ParameterSet("params", params)
## Create tree of Parameter objects
p = Parameter(name='params', type='group', children=params)
## If anything changes in the tree, print a message
def change(param, changes):
print("tree changes:")
for param, change, data in changes:
print(" [" + '.'.join(p.childPath(param))+ "] ", change, data)
path = p.childPath(param)
if path is not None:
childName = '.'.join(path)
childName =
print(' parameter: %s'% childName)
print(' change: %s'% change)
print(' data: %s'% str(data))
print(' ----------')
## Create two ParameterTree widgets, both accessing the same data
t = ParameterTree()
t.setParameters(p, showTop=False)
......@@ -127,6 +151,10 @@ t2.setParameters(p, showTop=False)
## Start Qt event loop unless running in interactive mode or using pyside.
import sys
if sys.flags.interactive == 0:
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
......@@ -131,11 +131,16 @@ class Parameter(QtCore.QObject):
return name
def childPath(self, child):
"""Return the path of parameter names from self to child."""
Return the path of parameter names from self to child.
If child is not a (grand)child of self, return None.
path = []
while child is not self:
child = child.parent()
if child is None:
return None
return path
def setValue(self, value, blockSignal=None):
......@@ -348,6 +348,7 @@ class GroupParameterItem(ParameterItem):
def treeWidgetChanged(self):
self.treeWidget().setFirstItemColumnSpanned(self, True)
if self.addItem is not None:
self.treeWidget().setItemWidget(self.addItem, 0, self.addWidgetBox)
self.treeWidget().setFirstItemColumnSpanned(self.addItem, True)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment