gtkProgressBar在RGtk2中 [英] gtkProgressBar in RGtk2
问题描述
我正在尝试为我为R脚本创建的一个小接口添加一个 gtkProgressBar
(使用 RGtk2
如果我做一些简单的事情,比如:
for(I in 1:50)
{
gtkProgressBarSetFraction(progress,i / 50)
Sys.sleep(1)
}
一切顺利,酒吧每秒更新。
然而,当我走到我的实际代码中,我有一个循环,在其中我做了类似于(b在1:1000中)的
$ b $
gtkProgressBarSetFraction(progress,i / 1000)
#在这里做一些繁重的计算
}
这里的问题是界面冻结,进度条只在循环结束时更新,因此完全无法使用它...
我在这里错过了什么吗?我怎样才能定期唤醒界面以便刷新?
谢谢
nico
编辑:好的,我解决了这个问题,但我仍然不明白发生了什么。我在 gtkProgressBarSetFraction
之后添加了一个 Sys.sleep
调用,现在界面更新愉快。为了减少浪费时间,我只做了 Sys.sleep(0.0001)
(所以对于1000个周期,我只需要0.1-1s的计算时间,这是可以接受的)。任何人都可以解释为什么会发生这种情况?
处理一个事件: gtkMainIterationDo(FALSE) code>。处理所有未决事件:
while(gtkEventsPending())gtkMainIteration()
。
R和Gtk事件循环相互作用的方式 - 在每一点上,R或Gtk都在控制之中,并且需要手动将控制权交给另一个控制。 Sys.sleep
是这样做的一种方式,而这些RGtk2特定功能是另一种方式。
I am trying to add a gtkProgressBar
to a little interface I created for an R script (using the RGtk2
package).
If I do something simple, as:
for (i in 1:50)
{
gtkProgressBarSetFraction(progress, i/50)
Sys.sleep(1)
}
everything runs smoothly and the bar is updated every second.
However, when I go to my actual code, I have a loop in which I do something like
for(i in 1:1000)
{
gtkProgressBarSetFraction(progress, i/1000)
#do some heavy computation here
}
The problem here is that the interface "freezes" and the progress bar is only updated at the end of the loop, therefore defeating completely its use...
Am I missing something here? How can I periodically "wake up" the interface so that it refreshes?
Thank you nico
EDIT: OK, I solved the problem, but I still don't understand what is going on. I added a Sys.sleep
call after the gtkProgressBarSetFraction
and now the interface updates happily. To reduce "wasted time" I just did Sys.sleep(0.0001)
(so for 1000 cycles I would only have ~0.1-1s more computing time, which is acceptable). Anyone could explain why is this happening?
To process one event: gtkMainIterationDo(FALSE)
. To process all pending events: while(gtkEventsPending()) gtkMainIteration()
.
This code is needed because of the way that the R and Gtk event loops interact - at every point, either R or Gtk is in control, and needs to manually hand off control to the other. Sys.sleep
is one way of doing that, and these RGtk2 specific functions are another.
这篇关于gtkProgressBar在RGtk2中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!