WxPython: a good way to overlay wx.Panel on an existing wx.Panel

I have a wx.Frame in which there is a main wx.Panel with several widgets inside it. I need one button to call the "help panel". This help panel is likely to be wx.Panel, and I want it to overlay the entire main wx.Panel (not including the wx.Frame menu bar). The help button should have some sort of close button that will make it disappear again.

What is a good way to achieve this? I looked at wx.Notebook, but did not find a way to make it not show tabs.

Note that I don’t want to destroy and recreate the help panel every time the user closes and opens it: I just want this to be hidden.

+5
source share
1 answer

There are several ways.

a) you can create a custom child panel and make it the same size and position at 0.0 among all child widgets. no need to destroy it; just show / hide it; it also resizes with the parent frame

b) wx.PopupWindow or a derived class pops up and places and sorts it in the right place

to suggest in a) here is an example where all the controls are placed in the panel using sizer, since a separate cntrl is created, which can be shown / hidden from the button, but you can create a custom cntrl that is hidden click <close>

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)

        self.panel = wx.Panel(self)

        # create controls
        self.cntrlPanel = wx.Panel(self.panel)
        stc1 = wx.StaticText(self.cntrlPanel, label="wow it works")
        stc2 = wx.StaticText(self.cntrlPanel, label="yes it works")
        btn = wx.Button(self.cntrlPanel, label="help?")
        btn.Bind(wx.EVT_BUTTON, self._onShowHelp)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(stc1)
        sizer.Add(stc2)
        sizer.Add(btn)
        self.cntrlPanel.SetSizer(sizer)


        # create help panel
        self.helpPanel = wx.Panel(self.panel)
        self.stcHelp = wx.StaticText(self.helpPanel, label="help help help\n"*8)
        btn = wx.Button(self.helpPanel, label="close[x]")
        btn.Bind(wx.EVT_BUTTON, self._onShowCntrls)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.stcHelp)
        sizer.Add(btn)
        self.helpPanel.SetSizer(sizer)
        self.helpPanel.Hide()
        self.helpPanel.Raise()
        self.helpPanel.SetBackgroundColour((240,250,240))
        self.Bind(wx.EVT_SIZE, self._onSize)

        self._onShowCntrls(None)

    def _onShowHelp(self, event):
        self.helpPanel.SetPosition((0,0))
        self.helpPanel.Show()
        self.cntrlPanel.Hide()

    def _onShowCntrls(self, event):
        self.cntrlPanel.SetPosition((0,0))
        self.helpPanel.Hide()
        self.cntrlPanel.Show()

    def _onSize(self, event):
        event.Skip()
        self.helpPanel.SetSize(self.GetClientSizeTuple())
        self.cntrlPanel.SetSize(self.GetClientSizeTuple())

app = wx.PySimpleApp()
frame = MyFrame()
frame.Show()
app.SetTopWindow(frame)
app.MainLoop()
+11
source

All Articles