I'm trying to develop a GUI in wxPython, but I would need some help. Here's what I'd like to achieve. The app should have 3 frames, but the catch is that only one frame should be visible at a time. There should be buttons on each of these frames. These buttons should serve as a kind of menu and they should a( hide the currently visible frame; and b) show a different frame. I know the common approach is to just use one frame with multiple panels, but for some reason this approach doesn't work too good for me. The application must be completely accessible to screen reader users and it seems that in some cases showing and hiding panels is not enough. I'm a screen reader user myself and it appears to me that screen readers don't always realize that the contents of the frame has changed if you only show and hide panels. I'm guessing that showing different frames alltogether could solve the problem. I would be grateful for a little working example. I know some of the things I should use, but despite of this I was unable to come up with anything. Many thanks.
Asked
Active
Viewed 1,274 times
1 Answers
2
You can just hide a frame and show another one. Like this:
import wx
class Frame(wx.Frame):
def __init__(self, title):
wx.Frame.__init__(self, None, title=title, size=(350,200))
btn = wx.Button(self, label="switch")
btn.Bind(wx.EVT_BUTTON, self._OnButtonClick)
self.Bind(wx.EVT_CLOSE, self.OnClose)
def _OnButtonClick(self, event):
self.frame.Show()
self.Hide()
def OnClose(self, event):
self.frame.Destroy()
self.Destroy()
app = wx.App(redirect=True)
f1 = Frame("Frame1")
f2 = Frame("Frame2")
f1.frame = f2
f2.frame = f1
f1.Show()
app.MainLoop()

Selçuk Cihan
- 1,979
- 2
- 17
- 30
-
Thank you very much. It does the job nicely, but there's one minor thing. When I run the script and close the GUI the console remains open. If my understanding is correct this is because hidden frame still exists (only it's not showing). Any nice way how to close the hidden frames once the visible frame is closed? One script I came a cross here used the wx.Frame.SetTopWindow() method. Is this the right method to use in this scenario and if so where to add it? – Matej Golian Jul 11 '16 at 20:22
-
You can do it within onclose event, see [this answer](http://stackoverflow.com/a/10837130/4281182). You will call destroy on both frames (self and self.frame) – Selçuk Cihan Jul 11 '16 at 20:27
-
Thanks, that did it. – Matej Golian Jul 11 '16 at 20:44