GUI - problem with delete-child

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% ...)

2 Likes

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

1 Like

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?

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.

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

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:

#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:

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

Alex.

4 Likes

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.