How to design a widget to select current volume in Python

Hi,all.
Here I load three volumes in Slicer as follows,


I want to design a python script module in which there is a widget showing all the loaded volumes.When I select a particular volume,I can get its voxels and make some changes.

Thanks in advance for your help!

这个功能很简单
首先使用
volumeNodes = slicer.util.getNodesByClass(“vtkMRMLScalarVolumeNode”)
可以获取到所有的Volume,然后用Combox显示出来
点击的时候使用
slicer.util.arrayFromVolume 可以获取到每一个体素里的值

1 Like

谢谢兄弟,简单明了。
那如果后续我又加了一个volume,Combox怎么更新名字呢

十分感谢。

监听Scene的NodeAdded事件,比如官网上的例子

@vtk.calldata_type(vtk.VTK_OBJECT)
def onNodeAdded(caller, event, calldata):
node = calldata
if isinstance(node, slicer.vtkMRMLVolumeNode):
# Call showVolumeRendering using a timer instead of calling it directly
# to allow the volume loading to fully complete.
qt.QTimer.singleShot(0, lambda: showVolumeRendering(node))

1 Like

这例子有点抽象 :crying_cat_face:
比如我有个类叫 class xxxWidgetclass xxxLogic

class xxxWidget:
  def setup(self):
    self.volume_choice_box = qt.QComboBox()
    # 这里怎么监听Scene,将新的volume名字填入到???
    self.volume_choice_box.addItems(['???'])

您可以先了解一下Slicer的事件系统,特别是NodeAdded事件的监听,这个对你后面的Slicer编程有很大的帮助

1 Like

好的,我会去官网看看例子,多谢指教。
:+1:

It would be more inclusive if all conversation happened in English. My native language is not English either (and I’d venture that it’s true for the majority of the forum users), but we always write in English so any conversation is potentially useful to the whole community. Bottom line: please stick to English. Thanks.

3 Likes