wxPython: How to update window size as the window resizes in real-time
Question: I would like to be able to obtain the window size of an app and pass it to
other modules in an application, and when the window size updates (say, if a
user resizes the window), the updated window size also gets passed to other
modules.

For example, I tried something like the code below where I tried to store the
window size in `self.size` so that it could be used in `foo()`. However, this
code would give me an error message saying `'TestPanel' object has no
attribute 'size`. I wonder if there is a way to do what I want to accomplish.

    
    
    import wx
    class TestPanel(wx.Panel):
    
        def __init__(self, parent):
            wx.Panel.__init__(self, parent, -1)
            self.Bind(wx.EVT_SIZE, self.OnSize, self)
            self.foo()
    
        def Resize(self):
            self.size = self.GetSize()
    
        def OnSize(self, event):
            self.Resize()
    
        def foo(self):
            print(self.size)
    
    if __name__ == '__main__':
        app = wx.App(False)
        f = wx.Frame(None, -1)
        TestPanel(f)
        f.Show()
        app.MainLoop()
    


Answer: You need to first find out what gave you that error message in the first
place. With your code as is, do realize that in the `__init__` method, the
`size` attribute was not set anywhere before its `foo` was called, giving you
that error.

What you want to do is to delay the calling of `foo` to your handler for
`EVT_SIZE`, in this case `OnSize`. The event will be called when the window
becomes visible as it will be resized to the default size (thus setting
`self.size`). You could then simplify what you want to do to:

    
    
    class TestPanel(wx.Panel):
    
        def __init__(self, parent):
            wx.Panel.__init__(self, parent, -1)
            self.Bind(wx.EVT_SIZE, self.OnSize, self)
    
        def OnSize(self, event):
            self.size = self.GetSize()
            self.foo()
    
        def foo(self):
            print(self.size)
    

Override `foo` to call into the other window, or whatever.

