Forcing repaint of window/control

Hi,

I'm trying to find out how I can hide/show parts of a dialog, using panels as suggested by Robin Dunn.

Below is a slightly modified version of the demo of using the layout contrainers (wxPython 2.2.5). In my modified version, the size of panel B is set to 1 / some size which is incremented whenever a button is pushed. In addition I hide the panel. By changing the size of the panel, the panel below (C) will readjust its size. I'm also hiding the panel, so that controls on the panel should not be reachable by using the tab-key.

Well, I'm trying to make the whole system repaint, however I'm not able to get that to work. If I resize the window manually between pressing a button, the correct layout will be shown.

Does anyone know how I force the repaint (guess it is quite easy).

Nikolai

···

#
#
#

from wxPython.wx import *

class TestLayoutConstraints(wxPanel):
    def __init__(self, parent):
        wxPanel.__init__(self, parent, -1)
        self.SetAutoLayout(true)
        EVT_BUTTON(self, 100, self.OnButton)

        self.SetBackgroundColour(wxNamedColour("MEDIUM ORCHID"))

        self.panelA = wxWindow(self, -1, wxDefaultPosition, wxDefaultSize,
                               wxSUNKEN_BORDER)
        self.panelA.SetBackgroundColour(wxBLUE)
        txt = wxStaticText(self.panelA, -1,
                           "Resize the window and see\n"
                           "what happens... Notice that\n"
                           "there is no OnSize handler.",
                           wxPoint(5,5), wxSize(-1, 50))
        txt.SetBackgroundColour(wxBLUE)
        txt.SetForegroundColour(wxWHITE)
        lc = wxLayoutConstraints()
        lc.top.SameAs(self, wxTop, 10)
        lc.left.SameAs(self, wxLeft, 10)
        lc.bottom.SameAs(self, wxBottom, 10)
        lc.right.PercentOf(self, wxRight, 50)
        self.panelA.SetConstraints(lc)

        #self.panelB = wxWindow(self, -1, wxDefaultPosition, wxDefaultSize,
        self.panelB = wxWindow(self, -1, wxDefaultPosition, wxSize(-1, 33),
                               wxSIMPLE_BORDER)
        self.panelB.SetBackgroundColour(wxRED)
        lc = wxLayoutConstraints()
        lc.top.SameAs(self, wxTop, 10)
        lc.right.SameAs(self, wxRight, 10)
        lc.bottom.AsIs()
        #lc.bottom.PercentOf(self, wxBottom, 30)
        lc.left.RightOf(self.panelA, 10)
        self.panelB.SetConstraints(lc)

        self.panelC = wxWindow(self, -1, wxDefaultPosition, wxDefaultSize,
                               wxSIMPLE_BORDER)
        self.panelC.SetBackgroundColour(wxWHITE)
        lc = wxLayoutConstraints()
        lc.top.Below(self.panelB, 10)
        lc.right.SameAs(self, wxRight, 10)
        lc.bottom.SameAs(self, wxBottom, 10)
        lc.left.RightOf(self.panelA, 10)
        self.panelC.SetConstraints(lc)

        b = wxButton(self.panelA, 100, ' Panel A ')
        lc = wxLayoutConstraints()
        lc.centreX.SameAs (self.panelA, wxCentreX)
        lc.centreY.SameAs (self.panelA, wxCentreY)
        lc.height.AsIs ()
        lc.width.PercentOf (self.panelA, wxWidth, 50)
        b.SetConstraints(lc);

        b = wxButton(self.panelB, 100, ' Panel B ')
        lc = wxLayoutConstraints()
        lc.top.SameAs (self.panelB, wxTop, 15)
        lc.right.SameAs (self.panelB, wxRight, 4)
        lc.height.AsIs ()
        lc.width.AsIs ()
        b.SetConstraints(lc);

        self.panelD = wxWindow(self.panelC, -1, wxDefaultPosition, wxDefaultSize,
                            wxSIMPLE_BORDER)
        self.panelD.SetBackgroundColour(wxGREEN)
        wxStaticText(self.panelD, -1, "Panel D", wxPoint(4, 4)).SetBackgroundColour(wxGREEN)

        b = wxButton(self.panelC, 100, ' Panel C ')
        lc = wxLayoutConstraints()
        lc.top.Below (self.panelD)
        lc.left.RightOf (self.panelD)
        lc.height.AsIs ()
        lc.width.AsIs ()
        b.SetConstraints(lc);

        lc = wxLayoutConstraints()
        lc.bottom.PercentOf (self.panelC, wxHeight, 50)
        lc.right.PercentOf (self.panelC, wxWidth, 50)
        lc.height.SameAs (b, wxHeight)
        lc.width.SameAs (b, wxWidth)
        self.panelD.SetConstraints(lc);
        self.msz = 150
        self.on = true
        self.parent = parent

    def OnButton(self, event):
        wxBell()
        if self.on:
            self.panelB.SetDimensions(-1, -1, -1, self.msz, wxSIZE_USE_EXISTING)
            self.panelB.Show(true)
        else:
            self.panelB.SetDimensions(-1, -1, -1, 1, wxSIZE_USE_EXISTING)
            self.panelB.Show(false)
        self.on = not self.on
        self.msz += 5
        self.parent.Refresh()
        
if __name__ == '__main__':

    class MainFrame(wxFrame):
        def __init__(self):
            wxFrame.__init__(self, NULL, -1, "Testing...")

            self.CreateStatusBar()
            mainmenu = wxMenuBar()
            menu = wxMenu()
            menu.Append(200, 'E&xit', 'Get the heck outta here!')
            mainmenu.Append(menu, "&File")
            self.SetMenuBar(mainmenu)
            EVT_MENU(self, 200, self.OnExit)
            self.panel = TestLayoutConstraints(self)
            self.SetSize(wxSize(400, 380))
            EVT_CLOSE(self, self.OnCloseWindow)

        def OnCloseWindow(self, event):
            self.Destroy()

        def OnExit(self, event):
            self.Close(true)

    class TestApp(wxApp):
        def OnInit(self):
            frame = MainFrame()
            frame.Show(true)
            self.SetTopWindow(frame)
            return true

    app = TestApp(0)
    app.MainLoop()

Well, I'm trying to make the whole system repaint, however I'm not
able to get that to work. If I resize the window manually between
pressing a button, the correct layout will be shown.

Does anyone know how I force the repaint (guess it is quite easy).

It doesn't need repainted, because you don't see any of the frame window
except the caption and borders. Everything else is child windows. What it
needs is for the auto layout algorithm to be run (which normally happens
when the window is resized.) Relplace your self.parent.Refresh() with
self.Layout()

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!