Matej Golian
Matej Golian

Reputation: 55

wxPython: Change Frame OnButtonPress

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.

Upvotes: 0

Views: 1400

Answers (1)

Selçuk Cihan
Selçuk Cihan

Reputation: 2034

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()

Upvotes: 2

Related Questions