A
A
Acidmind2013-11-26 07:59:26
Ruby on Rails
Acidmind, 2013-11-26 07:59:26

Data output in WxRuby

When the button is pressed, the print method is launched, which prints 01234 to the interpreter and to the text field. Data is output to the interpreter one by one every second, and all at once to the text field only after the program ends. Please tell me how to achieve that the data appears in the text field as well as in the interpreter, as the loop is executed.

def print
 5.times{| i |
    # Вывод данных в интерпретатор
    p i
    # Вывод данных в текстовое поле
    text.value +=  i.to_s
    sleep 1
  }
end

Answer the question

In order to leave comments, you need to log in

2 answer(s)
_
_ _, 2013-11-26
@Acidmind

You have a tough cycle, and even with a slip inside.
Any framework like WxRuby is event based. And any interaction with the interface is handled in the global event loop.
If you imagine what is happening in your case, then when processing the button click event, your loop is launched, which captures control and gives it back only after passing through the entire loop, after which control returns back to the main event loop and the text element is updated.
In order for this particular solution to work, instead of sleep, you need to make a loop that will spin for one second and inside which event processing will be performed ( http://wxruby.rubyforge.org/doc/app.html#App_dispatch )
You can try

def print
 Thread.new do
   5.times{| i |
      # Вывод данных в интерпретатор
      p i
      # Вывод данных в текстовое поле
      text.value +=  i.to_s
      sleep 1
    }
  end
end

PS Well, the tag is strange, of course

A
Acidmind, 2013-11-26
@Acidmind

It helped to use instead
of this one.
But at the same time, while the program is running, the interface hangs even when you click on it.
But using streams, as @AMar4enko advised, solved all the problems!
The solution with threads worked after adding the timer class to the description:

timer = Wx::Timer.new(self, Wx::ID_ANY)
evt_timer(timer.id) {Thread.pass}
timer.start(100)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question