I want the panel to start with a red color, then turn blue after 2 seconds and say "Finish".
But with wxPython
time.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?
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 ()
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.
wx.CallLaterThis function can be executed in a few seconds.
--self.TimeSleep (2) + wx.CallLater (2000, self.OnStart)
- python about iterative processing with specified numbers
- i want to add processing to the python library
- parallel processing using python multiprocessingpool and multiprocessingqueue does not work well
- python:i can't install wxpython
- python - avoid processing when duplicated
- python iterative processing num is not defend
- python - while syntax processing
- i want to speed up the processing of [python] for
- where to write timesleep in python scraping
- processing using the len function when an integer value is obtained from python standard input
- python - about range and int type processing
- python 3x - update processing and multi-process with pyqtgraph
- python - problem that processing ends without scraping error
- i want to execute processing like imfill in matlab with python
- python - nested, nested dictionary processing
- python - improved efficiency of loop processing in pandas data frames
- python 3x - i want to perform the same processing for each folder under the specified folder
- python - about multiple processing and loop processing in discordpy
- python - timesleep doesn't work
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- i want to call a child component method from a parent in vuejs
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- xcode - pod install [!] no `podfile 'found in the project directory