开发者

tk eventspending?

How do you refresh the tk gui while doing som开发者_运维百科e computation inside of a button call back routine?

The routine takes a long time, and the user wants to see some progress.

Need some way to repaint the screen / look for the cancel button.

in gtk its like this:

/* computation going on */
...
    while (gtk_events_pending ())
      gtk_main_iteration ();
...
/* computation continued */


The equivalent to that fragment of GTK code is the update method (of any widget). Internally, it pretty much does exactly the same thing (i.e., it processes events until there's nothing more to do but doesn't wait around for anything to come after that).

Be aware that update introduces the potential for reentrant calls to your callbacks; if an event happens twice in quick succession (ever so easy to occur in GUI code) then it's possible to end up running things inside themselves. You might get away with it most of the time, but it's all too easy to get into bad trouble. Introducing an interlock of some kind (e.g., disabling a button while processing its command, which is also quite a good visual metaphor) is a good way out of this.


I might be missing the point, but why would't you use thread for that? You won't be able to join it in a callback, but you still should be able to generate an event.

TkButton.new :text => 'Hard routine',
             :command => proc { Thread.new { hard_routine } }.pack
Tk.mainloop

doing the heavy things in your callback is considered a bad practice since it blocks the entire event loop so your gui freezes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜