Is it possible to make a Gtk.Box scrollable?

I’m trying to use Gtk.Viewport appended inside a Gtk.Box, but when I try to, the size of the widget still makes the entire window larger, instead of the single box being scrollable.

I’ve tried searching everywhere online, but they all say to use “Gtk.ScrollableWindow”. But the problem is, I don’t want the entire window to be scrollable. And besides, because it’s a top-level widget, it can’t be appended into a Gtk.Box.

Is there any way to implement a scrollable Gtk.Box using Gtk.Viewport without Gtk.ScrollableWindow?

The reason I ask, is because I’m implementing a GridView list inside of a single scrollable box, and I really don’t know how to implement it properly.

Thank you in advance!

A GtkScrolledWindow is not a window—that’s just some unfortunate naming that we’ve been stringing along for three major releases. A scrolled window is just a widget that you put into your widget tree, and provides a scrollable region for its contents.

If you want to scroll a box, you put the box into a GtkScrolledWindow widget.

Hi PlatinumLucario

I did just that this morning…

Been battling away with the Columnview / Gridview and have found both totally undocumented (maybe used as well) and have been pulling out my hair trying to figure out how to simply read database contents into it.

ebassi is correct you need to use the Gtk.ScrollableWindow which is a scrollable widget, set’s its size and add a Gtk.Grid widget to that.

For example:

content_section = Gtk.ScrolledWindow()
content_section.set_size_request(400, 100)

grid = Gtk.Grid()

assumes database, field1, field2 is another widget type (Label, Button etc.) which you will set it’s text with the contents from your data.

for rec in data.cursor:
field1 = rec[0]
field2 = rec[2]

grid.attach(field1, 0, 1, 1, 1)
grid.attach(field2, 1, 1, 1, 1)

content_section.set_child(grid)

You will need to do additional content siziing and plan what events to hook up to the child widgets but the contents_section will be scrollable.

Ah I see, so it’s not a top-level widget. That makes sense, it provides a scrollable region for the contents.

That explains it really well, and as a result, I’ve got it working now. So now I can continue with implementing the grid list that I need to implement.

Much appreciated!

Glad to hear it helped…

Someone should mention to the GyGObject folks to get on with some decent documentation and working examples. I wasted a considerable amount of time attempting to implement a simple grid widget using the Gridview / Columnview which I solved in 30 minutes with a ScrolledWindow widget containing a Grid widget. I find the Gtk4 API Reference is only useful for Developers that are already experienced with using its widgets… as it states Reference.

5 posts were split to a new topic: ColumnView/GridView with Python

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.