开发者

Tcl/Tk - how to make a window vanish on hitting OK button when OK runs a simulation

开发者 https://www.devze.com 2023-02-28 05:55 出处:网络
I have simple window in Tcl/Tk which on hitting OK button runs simulation. I am using Linux. The window is destroyed when the开发者_JAVA技巧 simulation finishes. The problem is window lingers on while

I have simple window in Tcl/Tk which on hitting OK button runs simulation. I am using Linux. The window is destroyed when the开发者_JAVA技巧 simulation finishes. The problem is window lingers on while the simulation is running. I want the window to vanish after I hit OK button.

I tried using wm withdraw .mywindow but it makes the area where the window was displayed (containing OK button) to be white.

I found update while googling but it is said to be harmful.


If you do wm withdraw .mywindow, the window won't actually vanish until the event loop is entered, because it is the event loop that handles redrawing the screen.

You have a couple of choices to solve your problem. For one, you can call update idletasks. That is a variation on update that just handles "idle" tasks such as painting the screen, but not tasks like responding to buttons and other user-generated events. So, solution one is to do:

wm withdraw .mywindow
update idletasks
run_simulation

By the way, the reason update is harmful is because it essentially starts a new event loop -- another infinite loop. If during that event loop an event comes in that causes the same code to run again, you start a third, and a fourth, and so on. As a general rule, nested infinite loops are never a good thing. Remember: tcl is single threaded so those event loops don't run in parallel.

The other solution is to enter the event loop naturally, and schedule your simulation to run once all other events have been processed. Do do this, start your simulation by using the after command. Using after will place an event in the event queue. When the event loop gets to that event your simulation will begin.

For example:

wm withdraw .mywindow
after idle run_simulation

When the above code exits -- assuming it was called as a result of an event such as pressing a button or key -- the event loop will be re-entered, any pending events will be processed, then your run_simulation command will run.

By the way -- if you have a GUI that needs to be responsive while your simulation is running, you should read Keep a GUI alive during a long calculation on the tcler's wiki. There's a lot to read there which makes the problem seem harder than it is, but it's not as complicated as it might first seem.

0

精彩评论

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