The Gtk::main() loop - What does Gtk::main() do in PHP-GTK/PyGTK?

  • Filter
  • Time
  • Show
Clear All
new posts

    The Gtk::main() loop - What does Gtk::main() do in PHP-GTK/PyGTK?

    When first starting with PHP-GTK, you may be puzzled by two specific pieces of code (or maybe three):

    PHP Code:
    if (!class_exists('gtk')) {
    "Gtk class not found");

    $window->connect_simple('destroy', array('gtk''main_quit'));

    First and foremost, the first line of code checks whether the Gtk class exists. If it doesn't, there's a good chance that the end user doesn't have PHP-GTK installed or does not have the PHP-GTK extension in their php.ini configuration file. So if you want to verify that the user has PHP-GTK installed, you can make use of the conditional statement shown above. Within the curly braces, you could output an error if you wish (assuming the user executed the application via the Terminal, then the error within the conditional statement will be outputted there).

    The second line of code with the connect_simple() method is connecting a signal (event) to a "handler". However, in this case, you want the main_quit() method to be executed which, in effect, quits the active PHP-GTK application. If you don't have this line, it won't break your PHP-GTK application, but it will mean that closing the application window will not quit the application entirely - which is why this line of code is necessary. It terminates the main loop that otherwise keeps the application active. To make things as clear as possible - when an application window is destroyed (closed), the destroy signal is fired. However, the destroy signal is also emitted when any widget is destroyed. So in essence, what is happening here is the destroy signal, when emitted, will execute the main_quit() method which effectively quits the application.

    However, the last line - Gtk::main() is very important to have in your PHP-GTK application. If you don't have this in your PHP-GTK application, it will appear as if your application does not open because the application is opened momentarily then quits because the Gtk::main() method hadn't been executed to keep the application active. If you're writing your PHP-GTK application fully object-oriented, you'll have the Gtk::main() loop within the constructor method. Read more on object-oriented constructors.

    See the full list of tutorials in this section.