Reputation: 7875
I'm working with PyGTK, trying to come up with a combination of widgets that will do the following:
Thanks - I'm new to GTK.
Upvotes: 4
Views: 5743
Reputation: 327
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.
/* 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;
}
Upvotes: 0
Reputation: 4366
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)
Upvotes: 2
Reputation: 5476
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 ) ) ).
Upvotes: 9