简体   繁体   English

如何使tkinter Canvas仅按需更新?

[英]How to make tkinter Canvas update only on-demand?

I'm writing a graphics program in Python and I would like to know how to make a Canvas update only on-demand; 我正在用Python编写图形程序,我想知道如何仅按需进行Canvas更新。 that is, stop a canvas from updating every run of the event loop and instead update only when I tell it to. 也就是说,停止画布更新事件循环的每次运行,而仅在我告诉它时更新。

I want to do this because in my program I have a separate thread that reads graphics data from standard input to prevent blocking the event loop (given that there's no reliable, portable way to poll standard input in Python, and polling sucks anyway), but I want the screen to be updated only at intervals of a certain amount of time, not whenever the separate thread starts reading input. 我想这样做是因为在我的程序中,我有一个单独的线程可以从标准输入中读取图形数据,以防止阻塞事件循环(假设没有可靠,可移植的方式来轮询Python中的标准输入,而且轮询仍然很烂),但是我希望仅在一定时间间隔内更新屏幕,而不是每当独立线程开始读取输入时才更新屏幕。

You can't pause the update of the canvas without pausing the entire GUI. 您必须暂停整个GUI,才能暂停画布的更新。

A simple solution would be for you to not draw to the canvas until you're ready for the update. 一个简单的解决方案是让您在准备好更新之前不要绘制到画布上。 Instead of calling canvas commands, push those commands onto a queue. 无需调用画布命令,而是将这些命令推入队列。 When you're ready to refresh the display, iterate over the commands and run them. 准备刷新显示时,请遍历命令并运行它们。

You could also do your own double-buffering, where you have two canvases. 您也可以做自己的双缓冲,其中有两个画布。 The one you are actively drawing would be behind the visible one. 您正在积极绘画的那个将在可见的那个后面。 When you are ready to display the results, swap the stacking order of the canvases. 准备显示结果时,请交换画布的堆叠顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM