wxPython: Freeze and Thaw

The wxPython library comes with a pair of handy methods called Freeze() and Thaw(). The call to Freeze() prevents the window from updating while it is frozen. This can be useful when you are adding or removing widgets and you want to reduce your UI from appearing to flicker. After you finish updating the UI, then you call the Thaw() method so that the user will be able to see the update.

Let’s take a look at a simple example.

import wx
class MyForm(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial", size=(500,500))
        self.btnNum = 1
        self.panel = wx.Panel(self, wx.ID_ANY)
        self.panel.Bind(wx.EVT_LEFT_DOWN, self.onClick)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
    def onClick(self, event):
        btn = wx.Button(self.panel, label="Button #%s" % self.btnNum)
        self.sizer.Add(btn, 0, wx.ALL, 5)
        self.btnNum += 1
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()

Here we have an instance of a wx.Frame that contains a panel. Every time the panel is left-clicked, we call the frame’s Freeze() method and add a button. Then we Thaw() it and the button appears. We keep track of how many buttons there are so we can keep the labels of the buttons different. I have used these methods when updating a ListCtrl or ObjectListView widget before and it’s worked great for those cases. I believe I’ve seen some people mention using it with the Grid widget too. You will probably have to play around with it a bit to see if these methods will help in your own case.

Related Articles

Print Friendly, PDF & Email