Which GTK widget combination to use for scrollable column of widgets?

Question:

I’m working with PyGTK, trying to come up with a combination of widgets that will do the following:

  • Let me add an endless number of widgets in a column
  • Provide a vertical scrollbar to get to the ones that run off the bottom
  • Make the widgets’ width adjust to fill available horizontal space when the window is resized

Thanks – I’m new to GTK.

Asked By: JasonFruit

||

Answers:

  • An endless number of widgets in a column: Sounds like a GtkVBox.
  • Vertical scrollbar: Put your VBox in a GtkScrolledWindow.
  • Horizontal stretching: This requires setting the appropriate properties for the VBox, ScrolledWindow, and your other widgets. At least in Glade the defaults seem to mostly handle this (You will probably want to change the scrollbar policy of the ScrolledWindow).

Now for the trick. If you just do what I’ve listed above, the contents of the VBox will try to resize vertically as well as horizontally, and you won’t get your scrollbar. The solution is to place your VBox in a GtkViewport.

So the final hierarchy is ScrolledWindow( Viewport( VBox( widgets ) ) ).

Answered By: Steve S

What Steve said in code:

vbox = gtk.VBox()
vbox.pack_start(widget1, 1, 1) ## fill and expand
vbox.pack_start(widget2, 1, 1) ## fill and expand
vbox.pack_start(widget3, 1, 1) ## fill and expand
swin = gtk.ScrolledWindow()
swin.add_with_viewport(vbox)
Answered By: saeedgnu

I’m a C developer working on an a GTK4 application. My Google search for "gtk4 how to make a widget scrollable" led me here!

It was hard to find a minimal example that used GtkScrolledWindow in the C API that I could compile and run, so I wanted to share one here, where I just add a scrollbar around a label that is always visible.

https://gist.githubusercontent.com/angstyloop/a5d9863826d3cd739ca202cb6bcaa3d2/raw/c85e569a7bd0d96fffd70da763ccaa11c3a94d7c/scrolledwindow.c

/* scrolledwindow.c
 *
 * Example of putting a GtkLabel in a GtkScrolledWindow.
 *
 * COMPILE
 *
 * gcc `pkg-config --cflags gtk4` -o scrolledwindow scrolledwindow.c `pkg-config --libs gtk4`
 *
 * RUN
 *
 * ./scrolledwindow
 */

#include <gtk/gtk.h>

#if GLIB_CHECK_VERSION(2, 74, 0)
#define APP_FLAGS G_APPLICATION_DEFAULT_FLAGS
#else
#define APP_FLAGS G_APPLICATION_FLAGS_NONE
#endif

static void
activate( GtkApplication *app, gpointer user_data )
{
    GtkWidget *window, *vbox, *scrolled_window, *label;

    window = gtk_application_window_new( app );

    gtk_window_set_default_size (GTK_WINDOW (window), 100, 50);

    vbox = gtk_box_new( GTK_ORIENTATION_VERTICAL, 10 );

    scrolled_window = gtk_scrolled_window_new();

    label = gtk_label_new("<0.o>");

    gtk_scrolled_window_set_child( GTK_SCROLLED_WINDOW( scrolled_window ),
        label );

    gtk_box_append( GTK_BOX( vbox ), scrolled_window );

    gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scrolled_window ),
        GTK_POLICY_NEVER,
        GTK_POLICY_ALWAYS );

    gtk_window_set_child( GTK_WINDOW( window ), vbox );

    gtk_widget_show( window );
}

int
main( int argc, char **argv )
{
    GtkApplication *app;
    int status;

    puts("Hover to the right of the label to see the scrollbar.");

    app = gtk_application_new( "org.gtk.example", APP_FLAGS );
    g_signal_connect( app, "activate", G_CALLBACK( activate ), NULL );
    status = g_application_run( G_APPLICATION( app ), argc, argv );
    g_object_unref( app );

    return status;
}
Answered By: angstyloop
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.