482. How to change the font and font size of GtkEntry?

Problem

You would like to change the font and font size of a GtkEntry as shown below:

How to change the font and font size of GtkEntry?


Solution

To change the font or font size of a GtkEntry, use the method modify_font on the GtkEntry.


Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 150);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Change the font and font size of GtkEntry");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0, 0);

$hbox = new GtkHBox();
$vbox->pack_start($hbox, 0, 0);
$hbox->pack_start(new GtkLabel("Keyword: "), 0, 0);
$hbox->pack_start($entry = new GtkEntry(), 0, 0);
$hbox->pack_start($button = new GtkButton("Search"), 0, 0);

$entry->modify_font(new PangoFontDescription("Times New Roman Italic 21")); // note 1
$entry->set_size_request(240, -1);

$entry->connect('activate', 'on_enter', $button);
$button->connect('clicked', 'on_click', $entry);

function on_enter($entry, $button) {
    $keyword = $entry->get_text();
    echo "Enter pressed. keyword = $keyword\n";
    $button->clicked();
}

function on_click($button, $entry) {
    $keyword = $entry->get_text();
    echo "button clicked. keyword = $keyword\n";
}

$window->show_all();
Gtk::main();

?>

Output

As shown above.

 

Explanation

  1. Set the font and font size of the entry.

Related Links

Add comment


Security code
Refresh