gtk/gtk.h_如何使用PHP-GTK创建事件/处理信号

gtk/gtk.h

PHP-GTK Logo

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:

wordpress hosting

该错误可能来自以下代码:

<?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 the clicked signal. The signal is emitted from the relevant GtkButton object when the end user clicks the relevant button. There is both a connect and connect_simple method for the GtkButton object. The difference between the two is connect returns the object it was called from (i.e. GtkButton for instance).

    $button->connect_simple('clicked','clicked_function') –我们将信号回调连接到被clicked信号。 当最终用户单击相关按钮时,信号将从相关的GtkButton对象发出。 GtkButton对象同时具有connectconnect_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中处理事件的方式!

Dedicated Hosting

Similar articles

类似文章

How to install PHP-GTK on Ubuntu 12.04?

如何在Ubuntu 12.04上安装PHP-GTK?

翻译自: https://www.eukhost.com/blog/webhosting/how-to-create-events-handle-signals-and-events-with-php-gtk/

gtk/gtk.h

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值