wxPython and thread memory problems?

plumpy321 at yahoo.com plumpy321 at yahoo.com
Fri Feb 18 04:07:05 EST 2005


Hi,

       I took an example from wxPython with the IE web browser and
created a refresh button to automatically refresh a web page in 5
second intervals.  But I notice that the memory utilization in Python
keeps increasing over time.  Can anyone tell me why this is happening?
Here is my code:

============================================================================

import wx, re
import time, thread
from urlparse import *

if wx.Platform == '__WXMSW__':
    import  wx.lib.iewin    as  iewin

refresh = 0

#----------------------------------------------------------------------

class TestPanel(wx.Panel):
    def __init__(self, parent, frame=None):
        wx.Panel.__init__(
            self, parent, -1,
            style = wx.TAB_TRAVERSAL |
                    wx.CLIP_CHILDREN |
                    wx.NO_FULL_REPAINT_ON_RESIZE
            )

        self.current = "http://www.google.com"
        self.frame = frame

        if frame:
            self.titleBase = frame.GetTitle()

        sizer = wx.BoxSizer(wx.VERTICAL)
        btnSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.ie = iewin.IEHtmlWindow(self, -1, style =
wx.NO_FULL_REPAINT_ON_RESIZE)

        btn = wx.Button(self, -1, "Open", style=wx.BU_EXACTFIT)
        self.Bind(wx.EVT_BUTTON, self.OnOpenButton, btn)
        btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)

        btn = wx.Button(self, -1, "Home", style=wx.BU_EXACTFIT)
        self.Bind(wx.EVT_BUTTON, self.OnHomeButton, btn)
        btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)

        btn = wx.Button(self, -1, "<--", style=wx.BU_EXACTFIT)
        self.Bind(wx.EVT_BUTTON, self.OnPrevPageButton, btn)
        btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)

        btn = wx.Button(self, -1, "-->", style=wx.BU_EXACTFIT)
        self.Bind(wx.EVT_BUTTON, self.OnNextPageButton, btn)
        btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)

        btn = wx.Button(self, -1, "Stop", style=wx.BU_EXACTFIT)
        self.Bind(wx.EVT_BUTTON, self.OnStopButton, btn)
        btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)

        btn = wx.Button(self, -1, "Search", style=wx.BU_EXACTFIT)
        self.Bind(wx.EVT_BUTTON, self.OnSearchPageButton, btn)
        btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)

        btn = wx.Button(self, -1, "Refresh", style=wx.BU_EXACTFIT)
        self.Bind(wx.EVT_BUTTON, self.OnRefreshPageButton, btn)
        btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)

        btn = wx.Button(self, -1, "AutoRefresh", style=wx.BU_EXACTFIT)
        self.Bind(wx.EVT_BUTTON, self.OnAutoRefreshPageButton, btn)
        btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)

        txt = wx.StaticText(self, -1, "Location:")
        btnSizer.Add(txt, 0, wx.CENTER|wx.ALL, 2)

        self.location = wx.TextCtrl(self, -1, "",
style=wx.TE_PROCESS_ENTER)

        self.location.Bind(wx.EVT_CHAR, self.OnLocationReturn)
        btnSizer.Add(self.location, 1, wx.EXPAND|wx.ALL, 2)

        sizer.Add(btnSizer, 0, wx.EXPAND)
        sizer.Add(self.ie, 1, wx.EXPAND)

        self.ie.LoadUrl(self.current)

        self.SetSizer(sizer)
        # Since this is a wxWindow we have to call Layout ourselves
        self.Bind(wx.EVT_SIZE, self.OnSize)

        # Hook up the event handlers for the IE window
        self.Bind(iewin.EVT_DocumentComplete, self.OnDocumentComplete,
self.ie)

        thread.start_new_thread(self.refresh_url, (5,))

    def OnSize(self, evt):
        self.Layout()

    def OnLocationReturn(self, evt):
        if evt.KeyCode() == wx.WXK_RETURN:
            self.ie.Navigate(self.location.GetValue(),
                 iewin.NAV_NoReadFromCache | iewin.NAV_NoWriteToCache)
        else:
            evt.Skip()

    def OnOpenButton(self, event):
        dlg = wx.TextEntryDialog(self, "Open Location",
                                "Enter a full URL or local path",
                                self.current, wx.OK|wx.CANCEL)
        dlg.CentreOnParent()

        if dlg.ShowModal() == wx.ID_OK:
            self.current = dlg.GetValue()
            self.ie.Navigate(self.current,
                 iewin.NAV_NoReadFromCache | iewin.NAV_NoWriteToCache)

        dlg.Destroy()

    def OnHomeButton(self, event):
        self.ie.GoHome()    ## ET Phone Home!

    def OnPrevPageButton(self, event):
        self.ie.GoBack()

    def OnNextPageButton(self, event):
        self.ie.GoForward()

    def OnStopButton(self, evt):
        self.ie.Stop()

    def OnSearchPageButton(self, evt):
        self.ie.GoSearch()

    def OnRefreshPageButton(self, evt):
        self.ie.RefreshPage(iewin.REFRESH_COMPLETELY)

    def OnAutoRefreshPageButton(self, evt):
        global refresh
        refresh = not refresh

    def refresh_url(self, delay):
        global refresh
        while (1):
            time.sleep(delay)
            if refresh:
                self.ie.Navigate(self.ie._get_LocationURL(),
                    iewin.NAV_NoReadFromCache |
iewin.NAV_NoWriteToCache)

    def OnDocumentComplete(self, evt):
        self.current = evt.URL
        self.location.SetValue(self.current)

class Frame(wx.Frame):
    def __init__(self, parent=None, id=-1, title='Title',
                 pos=wx.DefaultPosition, size=(640, 480)):
        wx.Frame.__init__(self, parent, id, title, pos, size)

        self.attr = {}
        self.attr['panel'] = TestPanel(self)

class App(wx.App):
    def OnInit(self):
        self.attr = {}

        self.attr['frame'] = Frame(title='My Window')
        self.attr['frame'].Show()
        self.SetTopWindow(self.attr['frame'])

        return True

def main():
    app = App()
    app.MainLoop()

if __name__ == '__main__':
    main()


#----------------------------------------------------------------------




More information about the Python-list mailing list