2

I want to handle the MAXIMIZE event. When maximizing it works just fine, but when I want to restore it does not even set the EVT_MAXIMIZE, even though the documentation says that this event is called when maximizing and restoring. I found that this is supposed to be a mistake in the documentation here is the link, but I still don't know which event is set when I restore the maximization.

Here is my .Bind method,. which is in MyFrame __init__:

self.Bind(wx.EVT_MAXIMIZE, self.onMaximize, self)

And here is the onMaximize function:

def onMaximize(self, event):

    print "Maximized"                            # Just for checking
    self.fig_panel.fig.tight_layout(w_pad=2.0)
    event.Skip()

Any ideas how the handle the maximization restore event.

TaZz
  • 652
  • 7
  • 20
user3176500
  • 389
  • 2
  • 6
  • 15

2 Answers2

1

The answer is right there in the link you provide.

"To find out when the frame is restored you can hook into the EVT_SIZE event since it will be sent at least once during the un-maximization process. "

self.Bind(wx.EVT_SIZE, self.OnResizeWindow)

def OnResizeWindow(self, event):
    if self.IsMaximized():
            self.maximized = 1
    else:
            self.maximized = 0
    event.Skip() 
Jerry_Y
  • 1,724
  • 12
  • 9
  • Hi Jerry_Y. I saw the answer, but am not sure I understand it. So I completely omit the EVT_MAXIMIZE ?. Can I also use the EVT_PAINT to do this? – user3176500 Feb 18 '14 at 08:40
  • EVT_SIZE and EVT_MAXIMIZE can be replaced by the event EVT_PAINT, which is generated when a window is redrawn. So the handling function stays the same, only the event has to be changed to EVT_PAINT. Is there a problem to that kind of solution ? Should I have any doubts about it ? – user3176500 Feb 18 '14 at 08:52
  • @user3176500 Yes, you can use EVT_PAINT. But I think EVT_SIZE is better because it narrow down to resize operation. – Jerry_Y Feb 18 '14 at 09:04
  • If I use EVT_SIZE and have the handling function as I described in the question, then resizing the frame and maximizing it works, but restoring from the maximized window does not. The function is called, but the self.fig_panel.fig.tight_layout(w_pad=2.0) is not executed properly. Why is that ? With the EVT_PAINT it works just fine. – user3176500 Feb 18 '14 at 09:34
  • what do you mean not executed properly? – Jerry_Y Feb 18 '14 at 09:50
  • well, I have matplotlib figures in the mainframe.fig_panel. When I resize or maximize the tight_layout is executed, and the figs still look in order. But when I restore from the maximization, it looks that the tight_layout is not executed and so the the figs are not back to the original size, there is overlapping of the labels, ... My main goal so to preserve figures neat when the main frame is resized, maximized or restored from the maximization. I know that some code would be helpful, but it's already quite a big project, at least for my standards as it is my first gui. – user3176500 Feb 18 '14 at 10:14
  • have no idea, sorry about that. – Jerry_Y Feb 18 '14 at 10:36
0

I finally solved it using a wx.CallAfter inside my on_resize function bound to wx.EVT_SIZE:

def on_resize(self, evt):
    wx.CallAfter(self.fig_panel.fig.tight_layout, w_pad=2.0)
    evt.Skip()

If you have multiple functions to call when the window is resized, you can do something like this:

def on_resize(self, evt):
    wx.CallAfter(self.on_resize_after)
    evt.Skip()

def on_resize_after(self):
    self.fig_panel.fig.tight_layout(w_pad=2.0)
    # add more layout adjustment functions here

This works perfectly on wxPython Phoenix 3.0.3 on Gtk. It responds to maximizing/restoring without having to bind the function to wx.EVT_MAXIMIZE.

iSWORD
  • 768
  • 15
  • 22