Difference between revisions of "AHM2012-Slicer-Python"
From NAMIC Wiki
Line 6: | Line 6: | ||
[[Image:SlicerQT-real 174.png|thumb|300px|right|Run-time query of all Qt widgets in the application]] | [[Image:SlicerQT-real 174.png|thumb|300px|right|Run-time query of all Qt widgets in the application]] | ||
See widgetTree.py example | See widgetTree.py example | ||
+ | <code> | ||
+ | import slicer | ||
+ | |||
+ | def widgetTree(root=""): | ||
+ | if not root: | ||
+ | root = slicer.util.mainWindow() | ||
+ | |||
+ | global treeItems | ||
+ | tree = qt.QTreeWidget() | ||
+ | tree.setHeaderLabels(["Widget", "Class", "Title", "Text", "Name"]) | ||
+ | treeItems = {} | ||
+ | treeItems[root] = qt.QTreeWidgetItem(tree) | ||
+ | parents = [root] | ||
+ | while parents != []: | ||
+ | widget = parents.pop() | ||
+ | if not widget: | ||
+ | break | ||
+ | widgetItem = qt.QTreeWidgetItem(treeItems[widget]) | ||
+ | children = widget.children() | ||
+ | for child in children: | ||
+ | treeItems[child] = widgetItem | ||
+ | parents += children | ||
+ | widgetItem.setText(0, str(widget)) | ||
+ | try: | ||
+ | widgetItem.setText(1, widget.className()) | ||
+ | except AttributeError: | ||
+ | pass | ||
+ | try: | ||
+ | widgetItem.setText(2, widget.title) | ||
+ | except AttributeError: | ||
+ | pass | ||
+ | try: | ||
+ | widgetItem.setText(3, widget.text) | ||
+ | except AttributeError: | ||
+ | pass | ||
+ | try: | ||
+ | widgetItem.setText(4, widget.name) | ||
+ | except AttributeError: | ||
+ | pass | ||
+ | tree.setGeometry(100, 100, 1000, 500) | ||
+ | tree.setColumnWidth(0,200) | ||
+ | tree.setColumnWidth(0,300) | ||
+ | tree.expandAll() | ||
+ | tree.show() | ||
+ | return tree | ||
+ | |||
+ | |||
+ | </code> | ||
It is possible to implement the logic of the effect using [http://www.vtk.org/Wiki/VTK/Python_Wrapping_FAQ VTK Python binding]. You can get ideas of what is possible from [http://www.itk.org/Wiki/VTK/Tutorials the VTK tutorials]. | It is possible to implement the logic of the effect using [http://www.vtk.org/Wiki/VTK/Python_Wrapping_FAQ VTK Python binding]. You can get ideas of what is possible from [http://www.itk.org/Wiki/VTK/Tutorials the VTK tutorials]. |
Revision as of 22:49, 7 January 2012
Home < AHM2012-Slicer-PythonContents
What is accessible via python
Interfaces are build with PythonQt which exposes most of the Qt interface so that sophisticated interfaces can be easily created.
See widgetTree.py example
import slicer
def widgetTree(root=""):
if not root:
root = slicer.util.mainWindow()
global treeItems
tree = qt.QTreeWidget()
tree.setHeaderLabels(["Widget", "Class", "Title", "Text", "Name"])
treeItems = {}
treeItems[root] = qt.QTreeWidgetItem(tree)
parents = [root]
while parents != []:
widget = parents.pop()
if not widget:
break
widgetItem = qt.QTreeWidgetItem(treeItems[widget])
children = widget.children()
for child in children:
treeItems[child] = widgetItem
parents += children
widgetItem.setText(0, str(widget))
try:
widgetItem.setText(1, widget.className())
except AttributeError:
pass
try:
widgetItem.setText(2, widget.title)
except AttributeError:
pass
try:
widgetItem.setText(3, widget.text)
except AttributeError:
pass
try:
widgetItem.setText(4, widget.name)
except AttributeError:
pass
tree.setGeometry(100, 100, 1000, 500)
tree.setColumnWidth(0,200)
tree.setColumnWidth(0,300)
tree.expandAll()
tree.show()
return tree
It is possible to implement the logic of the effect using VTK Python binding. You can get ideas of what is possible from the VTK tutorials.
Slicer also comes bundled with numpy so many interesting array operations are easy to perform on image data.
It is also possible to invoke slicer command line modules from python. These can be written in any language, but often rely on ITK for processing. In the near future SimpleITK should be available directly inside slicer. See the Slicer4 Python page for examples.
Using the console
(See J2's excellent demo video)
Writing a Scripted Module
- PythonQt interface
- Logic with vtk/vtkITK/CLI Modules
- Accessing MRML Data via numpy