PHP-GTK2 Cookbook Forum
Welcome, Guest
Please Login or Register.    Lost Password?
Re:how to connect progresbarr and a task (1 viewing) (1) Guest
Go to bottom Post Reply Favoured: 0
TOPIC: Re:how to connect progresbarr and a task
#743
emrag (User)
Fresh Boarder
Posts: 4
graphgraph
User Offline Click here to see the profile of this user
how to connect progresbarr and a task 2 Months, 3 Weeks ago Karma: 0  
i'm working on a little script which for easily to soccer games' scores. i want run progressbar when getting game's score and just clear end. but i can't figure that how can i connect two or more functions to work together at same time.

here is my glade file:

Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
<!--Generated with glade3 3.4.3 on Mon Sep 01 15:59:52 2008 -->
<glade-interface>
  <widget class="GtkWindow" id="window1">
    <property name="width_request">200</property>
    <property name="height_request">200</property>
    <property name="visible">True</property>
    <property name="resizable">False</property>
    <child>
      <widget class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <property name="homogeneous">True</property>
        <child>
          <widget class="GtkProgressBar" id="p1">
            <property name="visible">True</property>
            <property name="text"></property>
          </widget>
          <packing>
            <property name="expand">False</property>
          </packing>
        </child>
        <child>
          <widget class="GtkLabel" id="label2">
            <property name="visible">True</property>
          </widget>
          <packing>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <widget class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="label" translatable="yes">click button for score</property>
          </widget>
          <packing>
            <property name="expand">False</property>
            <property name="position">2</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="button1">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="label" translatable="yes">button</property>
            <property name="response_id">0</property>
            <signal name="clicked" handler="clb"/>
          </widget>
          <packing>
            <property name="expand">False</property>
            <property name="position">3</property>
          </packing>
        </child>
      </widget>
    </child>
  </widget>
</glade-interface>
here is my phpw file:
Code:
<?php
$glade = new GladeXML(dirname(__FILE__) . '/aa.glade');
$window = $glade->get_widget('window1');
$window->connect_simple('destroy', array('Gtk', 'main_quit'));

$lb1 = $glade->get_widget('label1');
$lb2 = $glade->get_widget('label2');

$button = $glade->get_widget('button1');
$button->connect_simple('clicked', 'start_processing');
$button->connect_simple('clicked', 'clb');

$p1 = $glade->get_widget('p1');

Gtk::timeout_add(1000, 'onTimeout', $lb2);
Gtk::main();

function clb()
{
	global $lb1;
	
	$abc = file_get_contents('http://www.livescore.com/default.dll/Game?comp=norway1&game=232197');
	$regex = "'Stromsgodset \[(.*)\] Ham Kam'";
	preg_match($regex,$abc,$out);
		
	$lb1->set_text($out[0]);
}

function start_processing($button, $entry) 
{	
	global $p1;
	
	$date = date("H:i:s");
    $max = 30000;
    for ($i=0; $i<$max; ++$i) 
	{
        echo "$i ";
        if ($i%1000==0)
	{
            $percent_complete = number_format($i/$max*100, 0);
            $p1->set_fraction($i/$max);
            $p1->set_text($percent_complete.'% Complete');
            while (Gtk::events_pending()) {Gtk::main_iteration();}
        }
    }

    $p1->set_text("Last response: $date");
    $p1->set_fraction(0);
}

function onTimeout($lb2)
{
    $lb2->set_text(date("H:i:s"));
	
	return true;
}

?>
 
 
Last Edit: 2008/09/01 17:55 By emrag.
  The administrator has disabled public write access.
#749
kksou (Admin)
Admin
Posts: 391
graph
User Online Now Click here to see the profile of this user
Re:how to connect progresbarr and a task 2 Months, 2 Weeks ago Karma: 8  
Would you like to be a bit more specific as to:

1) Which are the two functions you're trying to connect?

2) What have you tried?

3) What's the problem you're facing?

4) Any error messages? If yes, what are these?

The more specific you are, the easier it is for other people to help you.

Regards,
/kksou
 
  The administrator has disabled public write access.
#755
emrag (User)
Fresh Boarder
Posts: 4
graphgraph
User Offline Click here to see the profile of this user
Re:how to connect progresbarr and a task 2 Months, 2 Weeks ago Karma: 0  
clb() and start_processing() which functions i try to run together.

if i run the code like that first run start_processing() and than clb(). so start_processing() have no functionality in there. i want to get progress bar working while clb() getting datas. when finished getting datas from clb() stop porgress bar.
 
  The administrator has disabled public write access.
#756
kksou (Admin)
Admin
Posts: 391
graph
User Online Now Click here to see the profile of this user
Re:how to connect progresbarr and a task 2 Months, 2 Weeks ago Karma: 8  
Please refer to the example:

Sample Code 68: How to display progress bar while processing long task - Part 2 using_idle_add?

Replace the code in:
Code:
function process_task() {
        echo "processing subtask $this->subtask_count: ";
        for ($i=0; $i<10; ++$i) {
            usleep(50000); // sleep for half a second
            print ".";
        }
        print "\n";

        ++$this->subtask_count;
        $percent_complete = $this->subtask_count/$this->max_task_count;
with your function clb()
Code:
function process_task() {

        # note: these are the codes from your function clb()
        # but you need you have to change the code below

        global $lb1;
	
	$abc = file_get_contents('http://www.livescore.com/default.dll
               /Game?comp=norway1&game=232197');
	$regex = "'Stromsgodset \[(.*)\] Ham Kam'";
	preg_match($regex,$abc,$out);
		
	$lb1->set_text($out[0]);


        ++$this->subtask_count;
        $percent_complete = $this->subtask_count/$this->max_task_count;
Note that you need to change the code of your original function clb(). I suppose your file "http://www.livescore.com/default.dll/Game" is huge, that's why you want to have a progress bar. If this is the case, then you need to change file_get_contents() and use fopen() with fread() instead. Read in chunks of, say, 4096 bytes. For each iteration, you update the progress bar. If you use file_get_contents(), the entire control is passed over to PHP-GTK, and you get no opportunity to update the progress bar.
    Just remember that the progress bar needs to be updated manually by you. So the key is to chop up your "big" task into smaller sub-tasks. For each sub-task, update the progress bar incrementally.
      If you don't chop up the task (e.g. if you use file_get_contents), you will only see the progress bar updated from 0 to 100% at the end of the long task.
        Hope this helps.
          Regards,
            /kksou
             
              The administrator has disabled public write access.
            #757
            emrag (User)
            Fresh Boarder
            Posts: 4
            graphgraph
            User Offline Click here to see the profile of this user
            Re:how to connect progresbarr and a task 2 Months, 2 Weeks ago Karma: 0  
            i looked that article but i missed something. thank you so much. i got it working now.
             
              The administrator has disabled public write access.
            Go to top Post Reply
            Powered by FireBoardget the latest posts directly to your desktop

            Blog - Forum - Privacy Policy - Contact Us
            Copyright © 2006-2008. kksou.com. All Rights Reserved