Python Forum
[Tkinter] How to deal with code that blocks the mainloop, freezing the gui
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to deal with code that blocks the mainloop, freezing the gui
#2
If you just want to delay something, the first argument to after is how long to wait in millisecs before calling the passed in callback.
import tkinter as tk
import time

class MainFrame(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.label = tk.Label(self, text='Player 1 turn')
        self.label.pack()
        self.button = tk.Button(
            self, text='Player 1 Move', command=self.on_button)
        self.button.pack(pady=15)
        self.pack()

    def on_button(self):
        print('Button clicked')
        self.label['text'] = 'player 2 thinking'
        self.button['state'] = 'disabled'
        self.after(3000, self.delayed_player_2)


    def delayed_player_2(self):
        self.label['text'] = 'player 2 moving'
        self.after(1000, self.player_2_finsihed)

    def player_2_finsihed(self):
        self.label['text'] = 'player 1 turn'
        self.button['state'] = 'normal'


if __name__ == '__main__':
    app = tk.Tk()
    main_frame = MainFrame()
    app.mainloop()
Reply


Messages In This Thread
RE: How to deal with code that blocks the mainloop, freezing the gui - by Yoriz - May-04-2019, 02:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [WxPython] How to deal with code that blocks the mainloop, freezing the gui Yoriz 1 9,663 May-06-2019, 12:17 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020