doupingdiao3546 2016-08-04 00:12
浏览 123
已采纳

PHP GTK刷新GUI

I've been trying to make a very simple PHP application using php-gtk. The program does some processing and outputs the status of that process. The problem is that the application doesn't launch until the process is finished. I read that the line while (Gtk::events_pending()) {Gtk::main_iteration();} allows the main loop to continue while processing but it doesn't work for me. Here's the code:

<?php
if(!class_exists('gtk')){
    exit('php-gtk2!!');
}

$wnd = new GtkWindow();
$wnd->set_size_request(400, 200);
$wnd->set_title('test');
$wnd->connect_simple('destroy', array('gtk', 'main_quit'));

$lbl = new GtkLabel('1/3');

function processing($lbl){
    while (Gtk::events_pending()) {Gtk::main_iteration();}
    sleep(2);
    $lbl->set_text('2/3');
    sleep(2);
    $lbl->set_text('3/3');
}

processing($lbl);

$wnd->add($lbl);
$wnd->show_all();
Gtk::main();

?> I tried placing that line everywhere on the code and I'm not sure why it doesn't work. Any help would be really appreciated. Thank you in advance!

(Note: the sleep function is only to simulate some heavy processing)

  • 写回答

1条回答 默认 最新

  • dongqiaochi2711 2016-08-05 17:03
    关注

    Since you are trying to do work at the same time as your GUI is running, you will need to use a second thread, communicating from that thread to the GUI thread to get GUI updates. To do this, use the gdk_threads_add_idle() or g_idle_add() functions. Do not call GTK+ functions directly from the other thread!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题