How do you force refresh of a wx.Panel?

30,611

Solution 1

You need to call the Update method.

Solution 2

I had a StaticBitmap that, similarly, wouldn't update by any of these techniques (including the Update suggested in the accepted answer).

I found that calling .Hide() and .Show() on the Panel was enough to refresh the image. I suspect that the same would be true if I had run the functions against a lower-level object like the StaticBitmap.

Solution 3

You could put the mutable part of your panel on subpanels, e.g. like this:

def MakeButtonPanels(self):
    self.buttonPanel1 = wx.Panel(self)
    self.Add(self.buttonPanel1, 0, wxALL|wxALIGN_LEFT, 5)
    # ... make the three buttons and the button sizer on buttonPanel1

    self.buttonPanel2 = wx.Panel(self)
    self.Add(self.buttonPanel2, 0, wxALL|wxALIGN_LEFT, 5)
    # ... make the loading label and its sizer on buttonPanel2

    self.buttonPanel2.Show(False) # hide it by default

def HideButtons(self):
    self.buttonPanel1.Show(False)
    self.buttonPanel2.Show(True)
    self.Layout()
Share:
30,611
Fry
Author by

Fry

What's the universal code of time travel doing on Fry's butt? Well, it had to be somewhere

Updated on February 22, 2022

Comments

  • Fry
    Fry about 2 years

    I am trying to modify the controls of a Panel, have it update, then continue on with code execution. The problem seems to be that the Panel is waiting for Idle before it will refresh itself. I've tried refresh of course as well as GetSizer().Layout() and even sent a resize event to the frame using the SendSizeEvent() method, but to no avail. I'm at a loss here, I find it difficult to believe there is no way to force a redrawing of this panel. Here is the code that changes the controls:

    def HideButtons(self):
            self.newButton.Show(False)
            self.openButton.Show(False)
            self.exitButton.Show(False)
            self.buttonSizer.Detach(self.newButton)
            self.buttonSizer.Detach(self.openButton)
            self.buttonSizer.Detach(self.exitButton)
            loadingLabel = wx.StaticText(self.splashImage, wx.ID_ANY, "Loading...", style=wx.ALIGN_LEFT)
            loadingLabel.SetBackgroundColour(wx.WHITE)
            self.buttonSizer.Add(loadingLabel)
            self.GetSizer().Layout()
            self.splashImage.Refresh()
    

    Has anybody else encountered anything like this? And how did you resolve it if so?

    • Dana the Sane
      Dana the Sane over 14 years
      Have you tried self.Show()?
  • virtualnobi
    virtualnobi over 10 years
    Update () alone did not help in my case (StaticBitmaps in a GridSizer on a Panel), but the docs said Refresh () would trigger an unconditional repaint - which it did when followed by Update ()
  • CJ Harries
    CJ Harries about 6 years
    Phoenix changed the doc link structure. Update has moved. This is exactly what I was looking. Thanks!