librelist archives

« back to archive

Refreshing Edit Box

Refreshing Edit Box

From:
Matthew Lee Kalsow
Date:
2011-08-02 @ 21:08
Hi All,

I am writing a GUI using Green Shoes that uses an edit box to display
information output by my main code.
What I am having trouble with is that the block of code can take minutes to
complete depending on what how many files it is scanning,
and the edit box only actually updates once the block is done.  Is there a
way to get the edit box to be "updated" or "refreshed" without having to
wait for that block of code to finish?
....
def buttonsubmit
....
    if alertsthrown == 0
        main
    end
end

Shoes.app width: 600, height: 300 do
    flow(width: 0.08, height: 30){@submit = button "Submit"}
....
    flow{ @box = edit_box width: 600, height: 240}
....
    @submit.click {buttonsubmit}
end

 In main called by button submit is the code that can take several minutes,
it is also where I am writing to the edit box.
What I would like to be able to do is send an update to the user saying how
far along it is without having to start and stop main.
Can anyone help?

Re: [shoes] Refreshing Edit Box

From:
Régis d'Aubarède
Date:
2011-08-02 @ 23:40
Hello,

For long-time traitment, you can use thread and invoke_in_shoes_mainloop:

def buttonsubmit()
  Thread.new {
     ...long time file traitments....
    invoke_in_shoes_mainloop { @box.text='done!' }
  }
end

Shoes.app do
   define_async_thread_invoker()
   .....
   flow{ @box = edit_box width: 600, height: 240}
   @submit.click {buttonsubmit}
end

With that, your gui do not freeze an you too can abort traitment
(Thread.kill...)

by

2011/8/2 Matthew Lee Kalsow <kalsma02@luther.edu>

> Hi All,
>
> I am writing a GUI using Green Shoes that uses an edit box to display
> information output by my main code.
> What I am having trouble with is that the block of code can take minutes to
> complete depending on what how many files it is scanning,
> and the edit box only actually updates once the block is done.  Is there a
> way to get the edit box to be "updated" or "refreshed" without having to
> wait for that block of code to finish?
> ....
> def buttonsubmit
> ....
>     if alertsthrown == 0
>         main
>     end
> end
>
> Shoes.app width: 600, height: 300 do
>     flow(width: 0.08, height: 30){@submit = button "Submit"}
> ....
>     flow{ @box = edit_box width: 600, height: 240}
> ....
>     @submit.click {buttonsubmit}
> end
>
>  In main called by button submit is the code that can take several minutes,
> it is also where I am writing to the edit box.
> What I would like to be able to do is send an update to the user saying how
> far along it is without having to start and stop main.
> Can anyone help?
>



-- 

- - - - - - - - - - - - - - - - -
 __   ___  __     __
|__) |__  / _` | /__`
|  \ |___ \__> | .__/

- - - - - - - - - - - - - - - - -

Re: [shoes] Refreshing Edit Box

From:
ashbb
Date:
2011-08-03 @ 11:27
Hi Matthew, J, Régis and folks,

Thank you for the interesting problem and nice solutions. :)

I wanted to try coding by myself. How about this one?

require 'green_shoes'
Shoes.app do
  button 'Do main' do
    window width: 200, height: 200 do
      para 'Wait a while'
      t = Thread.new{sleep 5}
      a = animate do
        unless t.alive?
          a.stop
          owner.update 'finished!'
          close
        end
      end
    end
  end

  @ed = edit_box

  def update msg
    @ed.text = msg
  end
end

PS. Before trying to run the above code, you need to add this patch:

https://github.com/ashbb/green_shoes/commit/3770c02fe25d18583b4d4dbf09519f17645b6b0d

Thanks, I could fix the bug. :-P

ashbb

Re: [shoes] Refreshing Edit Box

From:
J. Kaiden
Date:
2011-08-02 @ 23:20
hi Matthew,

  don't know if i've got a hammer and see every problem as a nail, but i'm
real keen on the observer pattern for notifying the Shoes app of changes -
maybe this example is way too simple for your needs, but hey, maybe not ;)

 ... i set up a timer (as i don't have scores of 100 gig files to scan) and
tell it to notify the Shoes app every once in a while, updating the edit_box
with #text=

###############
require 'observer'

class DumbExample < Shoes::Widget
include Observable
 def initialize
@start = Time.now.to_f
@finish = @start + 30.0
end
 def main
every(1.0){|c|
stop if @start > @finish
@start += 1.0
changed
notify_observers("progress: #{c}/30")
}
end

end

Shoes.app do
 @ebox = edit_box
 de = dumb_example
de.main
 def update(message)
@ebox.text = message
end
 de.add_observer(self)
end
###################

 may be way too simple - if not, hope it helps some...

  - j




On Tue, Aug 2, 2011 at 11:08 PM, Matthew Lee Kalsow <kalsma02@luther.edu>wrote:

> Hi All,
>
> I am writing a GUI using Green Shoes that uses an edit box to display
> information output by my main code.
> What I am having trouble with is that the block of code can take minutes to
> complete depending on what how many files it is scanning,
> and the edit box only actually updates once the block is done.  Is there a
> way to get the edit box to be "updated" or "refreshed" without having to
> wait for that block of code to finish?
> ....
> def buttonsubmit
> ....
>     if alertsthrown == 0
>         main
>     end
> end
>
> Shoes.app width: 600, height: 300 do
>     flow(width: 0.08, height: 30){@submit = button "Submit"}
> ....
>     flow{ @box = edit_box width: 600, height: 240}
> ....
>     @submit.click {buttonsubmit}
> end
>
>  In main called by button submit is the code that can take several minutes,
> it is also where I am writing to the edit box.
> What I would like to be able to do is send an update to the user saying how
> far along it is without having to start and stop main.
> Can anyone help?
>