Gtk.ScrolledWindow keep same widget/area visible when changing height of child

I have a scrolledwindow widget with a box child.
I am implementing a sort of continuous scroll.
Widgets with content get appended, or prepended to the box child as needed when the user scrolls up or down.
When content is scrolled out of view it is removed (the widget is removed).
This all works fine except:

When new content is added to the bottom, for example, and old content is removed from the top of the box child, the scrolledwindow scrolls all the way to the bottom instead of staying put with the same content visible.

# Add new content widget at bottom
self.box_child_of_scrolledwindow.append(content_page_widget)
# Remove content widget from top
self.box_child_of_scrolledwindow.remove(self.box_child_of_scrolledwindow.get_first_child())

I would like the scrollbars to move to adjust to the new height of the box child, but the position of the box (the part currently visible in the scrolledwindow) to not change.

I have tried using scroll adjustment:

vadj = self.scrolledwindow.get_child().get_vadjustment()
vadj.set_value(0.5*(vadj.get_upper() - vadj.get_page_size()))

Which sets scroll to rougly the middle of the content, but that’s not exactly what I’m after…

Is there a way to achieve this?

Sometimes asking the question leads to finding the answer (or at least a possible answer).

curval = self.scrolledwindow.get_child().get_vadjustment().get_value()
newval = cva - self.box_child_of_scrolledwindow.get_first_child().get_height()
self.box_child_of_scrolledwindow.remove(self.scrollpage.get_first_child())
self.scrolledwindow.get_child().get_vadjustment().set_value(newval)
  1. Get the current scroll position
  2. Subtract the height of the element to remove
  3. Remove the widget element
  4. Apply the new scroll value