20.5. Displaying a GUI Widget in a Window
Problem
You want to display a window with a GUI widget, such as a button, in it.
Solution
Create the window, create the widget, and then add the widget to the window:
// create the window
$window = &new GtkWindow();
// create the button and add it to the window
$button = &new GTKButton('Click Me, Alice');
$window->add($button);
// display the window
$window->show_all();
// necessary so that the program exits properly
function shutdown() { gtk::main_quit(); }
$window->connect('destroy','shutdown');
// start GTK's signal handling loop
gtk::main();Discussion
First,
you create a window by instantiating a new
GtkWindow
object. GTK objects must be created
as references: &new GtkWindow( ), not
new GtkWindow( ). You then create a new
GtkButton
object with a label
“Click Me, Alice”. Passing
$button to the window’s
add( )
method
adds the button to the window. The show_all( )
method displays the window and any
widgets inside of it. The only widget inside the window in this
example is the button. The next two lines ensure that the program
quits when the window is closed. The shutdown( )
function is a callback, as is
explained later in Recipe 20.8.
The last line is necessary in all PHP-GTK programs. Calling
gtk::main( )
starts the
signal-handling loop. This means that the program waits for signals emitted by its GUI widgets and then responds to the signals as they occur. These signals are activities like clicking on buttons, resizing windows, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access