views:

420

answers:

2

How do I make any wxPython widget (like wx.Panel or wx.Button) automatically expand to fill its parent window?

+2  A: 

The short answer: use a sizer with a proportion of 1 and the wx.Expand tag.

So here I am in the init of a panel

sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.canvas, 1, wx.EXPAND)
self.SetSizer(sizer)
Jim Carroll
I repeat my comment to John: What if there is already a sizer with stuff in it assigned to the parent window, and I want to create another wx.Panel that will expand on the same window, hiding the other stuff?
cool-RR
cool-RR: you can try adding a child, but without using sizers... just use some absolute coordinates that fit in your parent.If that doesn't work, you can swap the panel for the other contents using Reparent()
Jim Carroll
I'm sorry, I didn't understand exactly what you want me to do. What do you mean absolute coordinates? The wx.Panel should be placed in (0,0), but its size should change as the size of the wx.Frame changes. And I didn't understand your suggestion about Reparent() either: In any case the wx.Fram is always the parent of everyone else.
cool-RR
Ok... I looked at some code that did just this. You can switch the window contents from one set of widgets to another by doing a window.DestroyChildren() then adding the controls that you do want visible. Another option is to use one of the 'notebook' or 'paging' controls to show one thing or another in the same area. It's like a Tab bar, but with hidden tabs.
Jim Carroll
No, use sizer.Hide() and sizer.Show()Create two panels, the main one and the one that pops up over the main one. To make the popup show, call sizer.Hide(main) and sizer.Show(popup) and then sizer.Layout()
FogleBird
@FogleBird: Are you saying that in the main wx.Frame I should create a sizer and add the two panels to it?
cool-RR
Yes. Only one will be shown at any given time.
FogleBird
+1  A: 

this shows how you can expand child panel with frame resize it also show how you can switch two panels, one containing cntrls and one containing help I think this solves all your problems

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