# How to design a widget to select current volume in Python

**URL:** https://discourse.slicer.org/t/how-to-design-a-widget-to-select-current-volume-in-python/25637
**Category:** Support
**Tags:** python
**Created:** [October 11, 2022, 5:30am UTC](https://discourse.slicer.org/t/how-to-design-a-widget-to-select-current-volume-in-python/25637 "2022-10-11T05:30:11Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![user4](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/user4/32/13172_2.png) [@user4](https://discourse.slicer.org/u/user4)
#### Post date: [October 11, 2022, 5:30am UTC](https://discourse.slicer.org/t/how-to-design-a-widget-to-select-current-volume-in-python/25637/1 "2022-10-11T05:30:12Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/a/1aeae5109ac424a49639abed4d417a9a3a08fa0f.png)  
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!

---

<div class="post-metadata">

### Author: ![jay1987](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jay1987/32/16183_2.png) [@jay1987](https://discourse.slicer.org/u/jay1987)
#### Post date: [October 11, 2022, 6:13am UTC](https://discourse.slicer.org/t/how-to-design-a-widget-to-select-current-volume-in-python/25637/2 "2022-10-11T06:13:01Z")

</div>

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

---

<div class="post-metadata">

### Author: ![user4](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/user4/32/13172_2.png) [@user4](https://discourse.slicer.org/u/user4)
#### Post date: [October 11, 2022, 6:41am UTC](https://discourse.slicer.org/t/how-to-design-a-widget-to-select-current-volume-in-python/25637/3 "2022-10-11T06:41:07Z")

</div>

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

十分感谢。

---

<div class="post-metadata">

### Author: ![jay1987](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jay1987/32/16183_2.png) [@jay1987](https://discourse.slicer.org/u/jay1987)
#### Post date: [October 11, 2022, 7:37am UTC](https://discourse.slicer.org/t/how-to-design-a-widget-to-select-current-volume-in-python/25637/4 "2022-10-11T07:37:09Z")

</div>

监听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))

---

<div class="post-metadata">

### Author: ![user4](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/user4/32/13172_2.png) [@user4](https://discourse.slicer.org/u/user4)
#### Post date: [October 11, 2022, 8:03am UTC](https://discourse.slicer.org/t/how-to-design-a-widget-to-select-current-volume-in-python/25637/5 "2022-10-11T08:03:57Z")

</div>

这例子有点抽象 😿  
比如我有个类叫 `class xxxWidget` 和 `class xxxLogic`

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

```

---

<div class="post-metadata">

### Author: ![jay1987](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jay1987/32/16183_2.png) [@jay1987](https://discourse.slicer.org/u/jay1987)
#### Post date: [October 11, 2022, 8:06am UTC](https://discourse.slicer.org/t/how-to-design-a-widget-to-select-current-volume-in-python/25637/6 "2022-10-11T08:06:16Z")

</div>

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

---

<div class="post-metadata">

### Author: ![user4](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/user4/32/13172_2.png) [@user4](https://discourse.slicer.org/u/user4)
#### Post date: [October 11, 2022, 8:12am UTC](https://discourse.slicer.org/t/how-to-design-a-widget-to-select-current-volume-in-python/25637/7 "2022-10-11T08:12:50Z")

</div>

好的，我会去官网看看例子，多谢指教。  
👍

---

<div class="post-metadata">

### Author: ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)
#### Post date: [October 11, 2022, 6:39pm UTC](https://discourse.slicer.org/t/how-to-design-a-widget-to-select-current-volume-in-python/25637/8 "2022-10-11T18:39:54Z")

</div>

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.
