Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-14-2010, 09:20 AM   #1
LQ Newbie
Registered: Jul 2010
Location: Mumbai, India
Distribution: Ubuntu 10.04, Ubuntu Netbook Remix 10.04
Posts: 4

Rep: Reputation: 0
Increase Font size in GTK2 C program


I am writing a program in C to create a GUI application using GTK 2. My actual program is quite large, so I am using a modified version of the sample program from GNOME library for trial.

I wish to increase the font size, and I have using PangoFontDescription. I have made that section of the code bold and italic below for quick reference to the code that affects fonts. But it does not make any difference. I am compiling the C file using gcc.

The code is as follows(hello.c):

#include <gtk/gtk.h>
#include <glib.h>
#include <gdk/gdk.h>

static void hello( GtkWidget *widget,
gpointer data )
g_print ("Hello World\n");

static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )

g_print ("delete event occurred\n");

return TRUE;

static void destroy( GtkWidget *widget,
gpointer data )
gtk_main_quit ();

int main( int argc,
char *argv[] )

GtkWidget *window;
GtkWidget *button;

gtk_init (&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

g_signal_connect (window, "delete-event",
G_CALLBACK (delete_event), NULL);

g_signal_connect (window, "destroy",
G_CALLBACK (destroy), NULL);

gtk_container_set_border_width (GTK_CONTAINER (window), 10);

button = gtk_button_new_with_label ("Hello World");

g_signal_connect (button, "clicked",
G_CALLBACK (hello), NULL);

g_signal_connect_swapped (button, "clicked",
G_CALLBACK (gtk_widget_destroy),

gtk_container_add (GTK_CONTAINER (window), button);

PangoFontDescription *df;

df = pango_font_description_from_string("Monospace");

gtk_widget_modify_font(button, df);

gtk_widget_show (button);
gtk_widget_show (window);

gtk_main ();

return 0;
Old 07-15-2010, 12:04 PM   #2
LQ Newbie
Registered: Mar 2010
Posts: 2

Rep: Reputation: 0
You could either modify the rc style, or create a label and place that into the button, snippet:
PangoFontDescription *df;

df = pango_font_description_from_string("Monospace");

label = gtk_label_new("Hello World");
gtk_widget_modify_font(label, df);
gtk_container_add(GTK_CONTAINER(button), label);

gtk_widget_show (label);
gtk_widget_show (button);
gtk_widget_show (window);
Since button is a bin/container, you can add widgets inside of it.

Also, please use the code button when placing code inside, it keeps the tabs for easier reading.
Old 07-18-2010, 01:50 AM   #3
LQ Newbie
Registered: Jul 2010
Location: Mumbai, India
Distribution: Ubuntu 10.04, Ubuntu Netbook Remix 10.04
Posts: 4

Original Poster
Rep: Reputation: 0
I tried the above code, yet the font size is fixed at its default value. Plus when I am executing the code, I get the following message in the terminal.

(hello:2479): Gtk-WARNING **: Attempting to add a widget with type GtkLabel to a GtkButton, but as a GtkBin subclass a GtkButton can only contain one widget at a time; it already contains a widget of type GtkLabel

Can a label be added inside the button? This message seems to suggest that.
Old 07-18-2010, 08:15 AM   #4
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Originally Posted by arnie001 View Post
Can a label be added inside the button?

In fact, a button itself has no way of displaying text. It does so by containing a label. That way, they don't have to rewrite the text-displaying code for every widget with text in it, and also it means you can put anything in a button, including pictures and even other buttons!
Old 07-18-2010, 09:22 AM   #5
Hidden Windshield
Registered: Jul 2010
Distribution: Fedora
Posts: 65

Rep: Reputation: 26
A button, as a subclass of bin, can only hold one child object at a time. The function "gtk_button_new_with_label", as the name implies, creates a button with a label already in place. That's why tecknophreak's code fails, it's trying to add a label to a button that already has one. Change the new button code to just "gtk_button_new()", then use the code to add a label with the bigger font.

Alternatively, use "gtk_button_new_with_label", then use "gtk_bin_get_child" to get the actual label, and change the font of that control.
Old 07-19-2010, 06:18 AM   #6
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread get the exposure it deserves.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to increase font size of xterm? cola Debian 3 12-20-2009 12:37 PM
aterm - how do I increase the font size? dkaplowitz Linux - Newbie 4 09-24-2009 08:03 PM
increase font size? MsDoomshroom DamnSmallLinux 4 04-22-2008 03:15 PM
How to increase font size in EmacsX? kornerr Linux - General 2 06-30-2005 12:50 AM
increase font size in applications jbolt Mandriva 4 08-30-2004 05:05 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:36 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration