Python TKinter multiple operations

I have two buttons on form and when I press on button it stays in pressed condition , and frame stays unresponsive condition until operation of button1 is finished , in my case I call new xterm windows that stays opened and with button2 I want to open new xterm but until I close xterm window from button1 command entire GUI is unresponsive. Why is that? Thanks , I started using TKinter 2 days ago so go easy on me :)

Upvotes: 4

Views: 613

Answers (2)

Lorenzo Bassetti
Lorenzo Bassetti

Reputation: 945

Another similar solution which helped me is to run a few

root.update()

inside of the loops/function that takes a lot of resources. This is more drastic than 'update_idletasks()' so be careful if it's compatible with your GUI settings and/or you general script structure.

'root' being of course the name of the window/form generated with Tkinter. Also, note that .update() does more than just "refreshing" the GUI.

Upvotes: 0

unutbu
unutbu

Reputation: 879501

Tkinter works in a single thread. So when you press the button, the callback command is apparently not returning for a long time. This causes the Tkinter GUI to freeze until the callback command returns.

If possible, the solution is to break the callback command into smaller pieces, perhaps a loop, and call update_idletasks() frequently enough to allow the Tkinter GUI to update.

If that is not possible, then the callback command should spawn a thread so its execution will not block the Tkinter main event loop.

Upvotes: 4

Related Questions