GTK3+ Specifying Monospace Fonts

I’m working in GTK3+ in C on Debian and trying to specify a monospace font for both

  1. Gtk_Label and
  2. Gtk_Entry.
    I see how to do this using Glade (for Gtk_Label only), but have been searching for how to do it programatically in C and have not been successful in finding code samples. I tried just using some reference docs, but I’m missing something since I can’t even get the code to compile.
    I also tried creating an .ini file but did not know the format, so that failed (& would have been generic across all fonts in the program, which I would like to avoid if possible).
    Can anyone point me in the right direction for this please? Thanks!

Do you think such a statement will attract many helpful people? What about reading a good book – at least C books exists, GTK books not that many unfortunately.

I think for modern GTK you have to use CSS, which is unfortunately not just a single statement in code. I do most in code too, but maybe it is easier with an XML UI file. For code, I once created this example, it is for GTK4, but I think it is not that different from latest GTK3:

GTK4 for Graphical User Interfaces

As you do not intend to do it dynamically with a font chooser, you can ignore a lot of the code. But I think you need the CssProvider and the loadFromData() function. I think when you accept that there is not a single function call available, but you need the CSS stuff, then you will manage to do it in C and GTK3. If not, let us know, I may be able to provide a full example in the next days… But maybe you would prefer to use an XML UI file? Let us know.

[EDIT]

I just remembered an old GTK3 example, it uses CSS for color, but you should be able to tune it:

https://github.com/StefanSalewski/gintro#css-styles-gerrors-and-exceptions

Stefan - thank you for those tips!

Two questions:

  1. Could you recommend a good GTK3+ book? I have been using the reference docs & examples found through google, but a good book would be invaluable.

  2. I’ve added the code below, but there is no change to the font in use (not monospace).

GtkCssProvider *cssProvider = gtk_css_provider_new();
char *data = “label {font-family: monospace; font-size: 10px;}”;
gtk_css_provider_load_from_data(cssProvider,data,-1,NULL);
gtk_style_context_add_provider(gtk_widget_get_style_context(win),
GTK_STYLE_PROVIDER(cssProvider),
GTK_STYLE_PROVIDER_PRIORITY_USER);

Do you have any suggestions? Thanks!

BTW: I did try:
char *data = “label {font-family: “monospace”; font-size: 10px;}”;
but still no change to the font.

Thanks for reporting, you are right. Indeed my NIm GTK3 version was working fine, but in the old C code was a bug: Here is the working version:

// https://stackoverflow.com/questions/30791670/how-to-style-a-gtklabel-with-css
// gcc `pkg-config gtk+-3.0 --cflags` test.c -o test `pkg-config --libs gtk+-3.0`
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
    gtk_init(&argc, &argv);
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    GtkWidget *label = gtk_label_new("Label  OiOlOx");
    GtkCssProvider *cssProvider = gtk_css_provider_new();
    //char *data = "label {color: green;}";
    char *data = "label {font-family: monospace;}";
    gtk_css_provider_load_from_data(cssProvider, data, -1, NULL);
    //gtk_style_context_add_provider(gtk_widget_get_style_context(window),
    gtk_style_context_add_provider(gtk_widget_get_style_context(label),
                                   GTK_STYLE_PROVIDER(cssProvider),
                                   GTK_STYLE_PROVIDER_PRIORITY_USER);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    gtk_container_add(GTK_CONTAINER(window), label);
    gtk_widget_show_all(window);
    gtk_main();
}

For the books: For GTK3 there is not much, I am aware of a Python GTK3 book, based on the GTK2 book of A. Krause. I have seen a few pages, would not recommend. But GTK4 is out for more than a year now. There is a free C book, see https://github.com/ToshioCP/Gtk4-tutorial. There is my GTK4 Nim one, at least a start: GTK4 for Graphical User Interfaces. And there are the Michi B youtube videos. For more see

You’ve cracked it! It is now working - Many thanks!

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