# Looping through ctk.ctkCollapsibleButton widgets

**URL:** <https://discourse.slicer.org/t/looping-through-ctk-ctkcollapsiblebutton-widgets/2895>\
**Category:** Development\
**Created:** [May 23, 2018, 8:47am UTC](https://discourse.slicer.org/t/looping-through-ctk-ctkcollapsiblebutton-widgets/2895 "2018-05-23T08:47:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)\
**Post date:** [May 23, 2018, 8:47am UTC](https://discourse.slicer.org/t/looping-through-ctk-ctkcollapsiblebutton-widgets/2895/1 "2018-05-23T08:47:39Z")

</div>

Dear all,  
I am trying to access ctk.ctkCollapsibleButton, I tried different things but nothing works. The goal is to creating/editing/removing objects during the loading function of the python plugin. Here is something I tried:

```
 class myPluginWidget(ScriptedLoadableModuleWidget):
          def setup(self):
                print("=======================================================")   
                print(" myPlugin Title ")
                print("=======================================================")           
                ScriptedLoadableModuleWidget.setup(self)
                self.initMainPanel()

          def initMainPanel(self):
                print(" initMainPanel ")   

                self.mainClpsBtn = ctk.ctkCollapsibleButton()
                self.mainClpsBtn.text = "my Plugin Title"
                self.layout.addWidget(self.mainClpsBtn)
                self.mainFormLayout = qt.QFormLayout(self.mainClpsBtn)

                self.pEDt= qt.QLineEdit()            
                self.pEDt.setText("some Text")
                self.mainFormLayout.addRow( self.pEDt )
              
                self.runBtn = qt.QPushButton("Run")
                self.mainFormLayout.addRow(self.runBtn)

                items = (self.mainFormLayout.itemAt(i) for i in range(self.mainFormLayout.count())) 
                print(items)
                for w in items:
                     print(w.widget())

```

The output is

```
        None
        None

```

I tried:

```
                     print(w)

```

The out put is:

```
       <generator object <genexpr> at 0x7fa27003f690>
       QLayoutItem (C++ Object 0x6877ee0)
       QLayoutItem (C++ Object 0x68f3270)

```

Same happened when I replaced self.mainFormLayout with self.layout. How can I get the type of the widget example QLineEdit or QPushButton and access their properties?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [May 23, 2018, 12:10pm UTC](https://discourse.slicer.org/t/looping-through-ctk-ctkcollapsiblebutton-widgets/2895/2 "2018-05-23T12:10:56Z")

</div>

I don’t think PythonQt handles that level of runtime polymorphism - the w.widget() call should return a QWidget but it would need to be mapped back to the specific subtype. You could try with pure PythonQt and see if that’s a bug or a known limitation of the wrapping.

In any case it’s probably best to keep explicit python references to the widgets you want to manipulate, e.g. a list of QPushButton instances and then you can access them directly.

---

<div class="post-metadata">

**Author:** ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)\
**Post date:** [May 23, 2018, 12:45pm UTC](https://discourse.slicer.org/t/looping-through-ctk-ctkcollapsiblebutton-widgets/2895/3 "2018-05-23T12:45:33Z")

</div>

Thanks for your reply. I will go for the list idea for now.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [May 23, 2018, 4:34pm UTC](https://discourse.slicer.org/t/looping-through-ctk-ctkcollapsiblebutton-widgets/2895/4 "2018-05-23T16:34:55Z")

</div>

I would not recommend to delete any Qt widgets, as it is extremely difficult to do it correctly. You can hide any widgets that you don’t need (instead of deleting them); and show them if you need them again (instead of re-creating them).

---

<div class="post-metadata">

**Author:** ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)\
**Post date:** [May 23, 2018, 4:49pm UTC](https://discourse.slicer.org/t/looping-through-ctk-ctkcollapsiblebutton-widgets/2895/5 "2018-05-23T16:49:47Z")

</div>

Thanks for the suggestion, I will consider this.
