What is the simplest way of monitoring when a wxPython frame has been resized?
python, wxpython, wxwidgets
Solution
You could handle `EVT_IDLE` which is triggered when the event queue is empty:
wx.IdleEvent: This class is used for EVT_IDLE events, which are generated and sent when the application becomes idle. In other words, the when the event queue becomes empty then idle events are sent to all windows (by default) and as long as none of them call RequestMore then there are no more idle events until after the system event queue has some normal events and then becomes empty again.
The process of resizing or moving a window should keep the event queue jammed so it won't become empty (and trigger the idle event) until the resizing/moving is done.
Set a dirty flag in `EVT_SIZE` and check it in the `EVT_IDLE` handler. If the flag is set, save the new size and reset the flag:
import wx
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None)
self.resized = False # the dirty flag
self.Bind(wx.EVT_SIZE,self.OnSize)
self.Bind(wx.EVT_IDLE,self.OnIdle)
def OnSize(self,event):
self.resized = True # set dirty
def OnIdle(self,event):
if self.resized:
# take action if the dirty flag is set
print "New size:", self.GetSize()
self.resized = False # reset the flag
app = wx.PySimpleApp()
frame = Frame().Show()
app.MainLoop()
`EVT_SIZE` may also be triggered when restoring a minimized window (the window size remains the same). If you want to cut down on unnecessary saves, you may want to check if the size is actually different before you save it to the config (you could keep track of it in a variable containing the last saved size).
You may want to add `EVT_MOVE` to keep track of the window position.
Problem
I want to know when a frame has been resized, so I can save the size and remember it the next time the application launches. Here is my `on_resize` method: ``` def on_resize(self, event): logic.config_set('main_frame_size', (event.Size.width, event.Size.height)) event.Skip() ``` And it's bound like this: ``` self.Bind(wx.EVT_SIZE, self.on_resize) ``` The problem is performance. For safety, my logic module saves the config file every time a setting changes, and writing the config file every time the resize event fires is way too performance taxing. What would be the best/easiest way of monitoring for when the user is done resizing the frame? Update My `config_set` function: ``` def config_set(key, value): """Set a value to the config file.""" vprint(2, 'Setting config value: "{}": "{}"'.format(key, value)) config[key] = value # Save the config file. with open(config_file_path, 'w') as f: pickle.dump(config, f) ```