I want to achieve

I want the panel to start with a red color, then turn blue after 2 seconds and say "Finish".
But with wxPythontime.sleep (2)If i execute such processing, it will be in a state of no response.
(In this source code, the window opens after 2 seconds)
What can I do to avoid this unresponsive state?

Source code
import wx
import time
import threading
import ctypes
class MyFrame (wx.Frame):
    def TimeSleep (self, value = 0):
        time.sleep (value)
        self.OnStart ()
    def __init __ (self):
        wx.Frame.__init__ (self, None, -1, "title")
        self.panel = wx.Panel (self, -1)
        self.panel.SetBackgroundColor ("red")
        self.Refresh ()
        self.TimeSleep (2)
    def OnStart (self):
        self.panel.SetBackgroundColor ("blue")
        lbl = wx.StaticText (self.panel, -1, "Finish")
        txt = wx.TextCtrl (self.panel, -1, "aaa", pos = (0,20))
        self.Refresh ()
app = wx.App (False)
f = MyFrame ()
f.Show (True)
app.MainLoop ()

Python 3.8
Windows 10

  • Answer # 1

    Depending on the application, the timer function of the GUI library is usually used.

    The reason why time.sleep makes it unresponsive
    GUI libraries etc. are in a loop process called "event loop"
    Processing such as drawing and input events
    If processing does not return to the event loop, it will be in a non-responsive state.

    Functions that block processing like time.sleep
    It cannot be used inside a function called from the event loop (because it becomes unresponsive).

    There is a solution such as using another thread, but
    It is not safe to operate the GUI directly from another thread.
    (For details, see Exclusive processing and inter-thread communication.)

    For simple use, use the timer function provided by the event loop.

    For wxwx.CallLaterThis function can be executed in a few seconds.

    --self.TimeSleep (2)
    + wx.CallLater (2000, self.OnStart)