228. How to set up a simple slide show?

Problem

You would like to set up a simple slide show to display, for example, your favorite digital photos as shown below:

How to set up a simple slide show?


Solution


Sample Code

Note: The following image files are required by the sample code below. Please save a copy of the image files and put them in the same directory where you store the sample code.

 button_rew1.png
 button_play1.png
 button_ff1.png

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
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   
41   
42   
43   
44   
45   
46   
48   
49   
50   
51   
52   
<?php
$window = new GtkWindow();
$window->set_size_request(280, 200);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Simple Slide Show");
$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);
$title->set_justify(Gtk::JUSTIFY_CENTER);
$alignment = new GtkAlignment(0.5, 0, 0, 0);
$alignment->add($title);
$vbox->pack_start($alignment, 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

$image_files = array('button_play1.png', 
    'button_ff1.png', 
    'button_rew1.png'); // note 1
$img_counter = 0; // note 2

$img = new GtkImage(); // note 3
$img->set_from_file($image_files[0]); // note 4

$frame = new GtkFrame();
$frame->add($img);

$hbox = new GtkHBox();
$frame->set_size_request(100, 100);
$hbox->pack_start(new GtkLabel());
$hbox->pack_start($frame, 0);
$hbox->pack_start(new GtkLabel());

$vbox->pack_start($hbox, 0);

Gtk::timeout_add(1000, 'next_image'); // note 5

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

function next_image() {
    global $img, $image_files, $img_counter;
    ++$img_counter; // note 6
    if ($img_counter>count($image_files)-1) $img_counter = 0; 
    $img->set_from_file($image_files[$img_counter]); 
    return true;
}

?>

Output

As shown above.

 

Explanation

  1. This is the array containing the images. GtkImage can read all standard image files, including .gif, .tif, .jpg, .png, etc.
  2. This is the image counter.
  3. Create a GtkImage.
  4. Set the first image.
  5. Here we set a timeout of 1000 ms, which is 1 second.
  6. Get the next image and display it.

Related Links

Add comment


Security code
Refresh