# GUI - problem with delete-child

**URL:** <https://racket.discourse.group/t/gui-problem-with-delete-child/1160>\
**Category:** Questions & Answers\
**Tags:** gui\
**Created:** [July 19, 2022, 5:54pm UTC](https://racket.discourse.group/t/gui-problem-with-delete-child/1160 "2022-07-19T17:54:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtp](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jtp](https://racket.discourse.group/u/jtp)\
**Post date:** [July 19, 2022, 5:54pm UTC](https://racket.discourse.group/t/gui-problem-with-delete-child/1160/1 "2022-07-19T17:54:17Z")

</div>

Below is a simple example of something which I am having trouble with. I want to be able to delete all the children of a frame, regardless of what they are. It seems like this should work but it errors out as shown below.

#lang racket

(require racket/gui)

(define frame (new frame%  
[label "My Frame"]))

(define my-pane (new vertical-pane% [parent frame]))

(send frame show #t)

(for ([this-child (send frame get-children)])  
(send frame delete-child this-child))

delete-child in area-container\<%\>: contract violation  
expected: (is-a?/c subwindow\<%\>)  
given: (object:vertical-pane% ...)

---

<div class="post-metadata">

**Author:** ![dannypsnl](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/dannypsnl/32/917_2.png) [@dannypsnl](https://racket.discourse.group/u/dannypsnl)\
**Post date:** [July 20, 2022, 7:07am UTC](https://racket.discourse.group/t/gui-problem-with-delete-child/1160/2 "2022-07-20T07:07:49Z")

</div>

Using `vertical-panel%` will resolve this because `panel%` is a `subwindow<%>` but `pane%` is not.

---

<div class="post-metadata">

**Author:** ![jtp](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jtp](https://racket.discourse.group/u/jtp)\
**Post date:** [July 20, 2022, 4:10pm UTC](https://racket.discourse.group/t/gui-problem-with-delete-child/1160/3 "2022-07-20T16:10:03Z")

</div>

Thanks but that will not be easy. The reason is that one of the actual things I want to delete is a qresults-list table which contains a vertical pane. That would require editing the library which was contributed to  
Racket by someone else and I don't know if there is a particular reason that it's a vertical pane rather than a vertical panel. There must be some way to delete a vertical pane from a frame. If not delete-child, than something else?

---

<div class="post-metadata">

**Author:** ![jtp](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jtp](https://racket.discourse.group/u/jtp)\
**Post date:** [July 20, 2022, 4:29pm UTC](https://racket.discourse.group/t/gui-problem-with-delete-child/1160/4 "2022-07-20T16:29:22Z")

</div>

Actually, I just looked at the source for qresults-list and nothing is explicitly defined as a vertical pane but I am still getting that contract violation saying that it's a vertical-pane and not a subwindow. So something in qresults-list is being recognized a a vertical-pane% that does not also inherit subwindow% and I don't know what object it is. I wrote that example so as to be simple for the list. Meanwhile, there must be some way to delete a vertical pane from a frame. Also, since deleting and redrawing objects is a common part of GUI programming, I think there needs to be a way to simply delete all children from a GUI element the way my for loop was intended to do.

---

<div class="post-metadata">

**Author:** ![alexh](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/alexh/32/315_2.png) [@alexh](https://racket.discourse.group/u/alexh)\
**Post date:** [July 21, 2022, 9:49am UTC](https://racket.discourse.group/t/gui-problem-with-delete-child/1160/5 "2022-07-21T09:49:26Z")

</div>

The qresults list does contain a `vertical-pane%` as the container for the widget:

> <https://github.com/alex-hhh/qresults-list/blob/b680a09a8e83cc72fb306e3d9a8ebaff91a7040d/private/qresults-list.rkt#L362>

`pane%` objects cannot be deleted from their parent container. Note that "deleting" an object from a container (such as a `frame%`) does not actually destroy the object, it just sets its style to `'deleted` and makes it hidden. This operation is not valid for `pane%` and classes derived from `pane%` so they cannot be deleted from their parent container. All other objects however can be deleted.

As a workaround, I would recommend adding an intermediate `vertical-panel%` object to the frame and add the `qresults-list%` to it. You should than be able to delete the panel. Following your example in your original post, this would be:

```scheme
#lang racket
(require racket/gui)

(define frame (new frame% [label "My Frame"]))

(define my-panel (new vertical-panel% [parent frame]))
(define my-pane (new vertical-pane% [parent my-panel]))

(send frame show #t)

(for ([this-child (send frame get-children)])
(send frame delete-child this-child))

```

Finally, deleting child widgets one by one will trigger layout re-calculations which cause visible flickering. I would recommend either wrapping the for loop with the `delete-child` inside a `begin-container-sequence` /`end-container-sequence`, or using `change-children`, like so:

```scheme
(send frame change-children (lambda (previous-children) null))

```

Alex.

---

<div class="post-metadata">

**Author:** ![jtp](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jtp](https://racket.discourse.group/u/jtp)\
**Post date:** [July 21, 2022, 9:01pm UTC](https://racket.discourse.group/t/gui-problem-with-delete-child/1160/6 "2022-07-21T21:01:31Z")

</div>

Thanks. That's very helpful. I actually came up with a similar solution but I used a frame instead of a vertical-panel. Both work. I am getting a little flickering either way but that may just be because I am working in a virtual machine that doesn't work well with my GPU.
