gtk/gtk.h
When trying to create an event (or what’s called handle a signal), you may get the following error:
尝试创建事件(或称为处理信号)时,可能会出现以下错误:
PHP Fatal error: Call to a member function remove() on a non-object in /home/user/Desktop/application.php on line 12
PHP Fatal error: Call to a member function remove() on a non-object in /home/user/Desktop/application.php on line 12
This error may come from the following code:
<?php $window=new GtkWindow(); $button=new GtkButton("Click here"); $button->connect_simple('clicked','clicked_function'); $window->add($button); $window->show_all(); $window->connect_simple('destroy',array('gtk','main_quit')); Gtk::main(); function clicked_function() { $window->remove($button); } ?>
<?php $window=new GtkWindow(); $button=new GtkButton("Click here"); $button->connect_simple('clicked','clicked_function'); $window->add($button); $window->show_all(); $window->connect_simple('destroy',array('gtk','main_quit')); Gtk::main(); function clicked_function() { $window->remove($button); } ?>
What’s wrong with the code, you may say? Well, let’s first understand what is happening.
您可能会说代码有什么问题? 好吧,让我们首先了解发生了什么。
$button->connect_simple('clicked','clicked_function')
– we connect a signal callback to theclicked
signal. The signal is emitted from the relevant GtkButton object when the end user clicks the relevant button. There is both aconnect
andconnect_simple
method for the GtkButton object. The difference between the two isconnect
returns the object it was called from (i.e. GtkButton for instance).$button->connect_simple('clicked','clicked_function')
–我们将信号回调连接到被clicked
信号。 当最终用户单击相关按钮时,信号将从相关的GtkButton对象发出。 GtkButton对象同时具有connect
和connect_simple
方法。 两者之间的区别是connect
返回从中调用它的对象(例如GtkButton)。We make the signal callback to
clicked_function
function. We then try and remove the button from GtkWindow.我们使信号回调到
clicked_function
函数。 然后,我们尝试从GtkWindow中删除该按钮。
The problem? Scope. Because Gtk::main
continues the application in a loop (which means it subsequently keeps looking out for events to occur – like this one), it means you can’t call an object from within a function (as it is technically not available), so we have to pass it through the optional parameters of connect_simple in order to use the GtkWindow remove
method to remove the GtkButton object. In theory, we actually have two options.
问题? 范围。 因为Gtk::main
在循环中继续应用程序(这意味着它随后一直在寻找发生的事件-像这样),所以这意味着您无法从函数内调用对象(从技术上讲,它不可用) ,因此我们必须将其传递给connect_simple的可选参数,以便使用GtkWindow remove
方法删除GtkButton对象。 从理论上讲,我们实际上有两种选择。
- We make the two objects (GtkWindow and GtkButton) global in scope (remember we declare scope within the function or where ever it usually isn’t available). 我们使两个对象(GtkWindow和GtkButton)在范围内全局化(记住,我们在函数内声明范围,或者在通常不可用的地方声明范围)。
- As I said, we pass the objects as optional parameters. 正如我所说,我们将对象作为可选参数传递。
Let’s show you both methods:
让我们向您展示两种方法:
<?php $window=new GtkWindow(); $button=new GtkButton("Click here"); $button->connect_simple('clicked','clicked_function'); $window->add($button); $window->show_all(); $window->connect_simple('destroy',array('gtk','main_quit')); Gtk::main(); function clicked_function() { global $window; global $button; $window->remove($button); // now works } ?>
<?php $window=new GtkWindow(); $button=new GtkButton("Click here"); $button->connect_simple('clicked','clicked_function'); $window->add($button); $window->show_all(); $window->connect_simple('destroy',array('gtk','main_quit')); Gtk::main(); function clicked_function() { global $window; global $button; $window->remove($button); // now works } ?>
<?php $window=new GtkWindow(); $button=new GtkButton("Click here"); $button->connect_simple('clicked','clicked_function',$window,$button); $window->add($button); $window->show_all(); $window->connect_simple('destroy',array('gtk','main_quit')); Gtk::main(); function clicked_function($window,$button) { $window->remove($button); } ?>
<?php $window=new GtkWindow(); $button=new GtkButton("Click here"); $button->connect_simple('clicked','clicked_function',$window,$button); $window->add($button); $window->show_all(); $window->connect_simple('destroy',array('gtk','main_quit')); Gtk::main(); function clicked_function($window,$button) { $window->remove($button); } ?>
And there you have it. That’s how you handle events in PHP-GTK!
那里有。 这就是您在PHP-GTK中处理事件的方式!
Similar articles
类似文章
How to install PHP-GTK on Ubuntu 12.04?
翻译自: https://www.eukhost.com/blog/webhosting/how-to-create-events-handle-signals-and-events-with-php-gtk/
gtk/gtk.h