tk 活动支出?
在按钮回调例程中进行一些计算时如何刷新 tk gui?
该例程需要很长时间,用户希望看到一些进展。
需要某种方法来重新绘制屏幕/寻找取消按钮。
在gtk中是这样的:
/* computation going on */
...
while (gtk_events_pending ())
gtk_main_iteration ();
...
/* computation continued */
How do you refresh the tk gui while doing some 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 */
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与该 GTK 代码片段等效的是(任何小部件的)
update
方法。在内部,它几乎做完全相同的事情(即,它处理事件,直到没有更多事情可做,但不会等待此后发生任何事情)。请注意,
update
引入了对回调进行重入调用的可能性;如果一个事件快速连续发生两次(在 GUI 代码中很容易发生),那么最终可能会在它们内部运行事物。大多数时候你可能会侥幸逃脱,但很容易陷入困境。引入某种互锁(例如,在处理命令时禁用按钮,这也是一个很好的视觉隐喻)是解决这个问题的好方法。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.我可能没有抓住重点,但你为什么不使用线程呢?您将无法将其加入回调中,但您仍然应该能够生成事件。
在回调中做繁重的事情被认为是一种不好的做法,因为它会阻塞整个事件循环,因此您的 gui 会冻结。
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.
doing the heavy things in your callback is considered a bad practice since it blocks the entire event loop so your gui freezes.