避免在Python和wxPython中不必要的内存消耗



我在Windows 7操作系统上使用python v2.7和wxPython v3.0。在我的应用程序中,我有一个名为myPanel的面板。我在myPanel上有一个图像作为背景,图像名称是green.bmp, myPanel包含一个名为myButton的按钮。这个myButton也包含了一个名为blue.bmp的图像作为背景,线程只是改变了myButton上的图像。

为了演示目的,我在myButton上一次又一次地使用相同的图像。在我的现实问题中,我有不同的图像

Problem:在执行我的应用程序后,当我在任务管理器中看到内存消耗时,我观察到内存消耗不断增加。下面的代码有什么问题导致不必要的内存消耗?我怎样才能避免这种情况呢?

Code:代码中使用的图片可以从这里下载Green.bmp和Blue.bmp。代码片段如下所示:

import wx
from wx.lib.pubsub import setupkwargs
from wx.lib.pubsub import pub
from threading import Thread
import threading
import time
class gui(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, None, id, title, size=(500,400))
        myPanel = wx.Panel(self, -1, size=(300,200))
        image_file1 = 'green.bmp'
        image1 = wx.Image(image_file1, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        self.bitmap2 = wx.StaticBitmap(myPanel, -1, image1, (0, 0))
        pub.subscribe(self.addImage, 'Update')
    def addImage(self):
        myButton = wx.Button(self.bitmap2, -1, size =(30,30), pos=(20,20))
        image_file2 = 'blue.bmp'
        image2 = wx.Image(image_file2, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        self.bitmap1 = wx.StaticBitmap(myButton, -1, image2, (0, 0))
class myThread(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.start()
    def run(self):
        while True:
            time.sleep(2)
            wx.CallAfter(pub.sendMessage, 'Update')
if __name__=='__main__':
    app = wx.App()
    frame = gui(parent=None, id=-1, title="Test")
    frame.Show()
    myThread()
    app.MainLoop()

感谢您的宝贵时间。

您的问题在addmage。每次执行这行:

myButton = wx.Button(self.bitmap2, -1, size =(30,30), pos=(20,20))

您正在为self.bitmap2添加另一个子窗口。这个窗口与前面添加的窗口完全重叠,因此不明显有多个子窗口。要查看发生了什么,将这一行添加到addmage:

的底部
print len(self.bitmap2.GetChildren())

要解决这个问题,你应该在添加新按钮之前销毁所有子按钮。将它添加到addmage

的顶部
self.bitmap2.DestroyChildren()
但是,这种方法会破坏所有添加到bitmap2的窗口,所以要小心。如果您只想销毁按钮,则应该保留对它的引用。我已经修改了你的程序,使用如下方法:
import wx
from wx.lib.pubsub import setupkwargs
from wx.lib.pubsub import pub
from threading import Thread
import threading
import time
class gui(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, None, id, title, size=(500,400))
        myPanel = wx.Panel(self, -1, size=(300,200))
        image1 = wx.Image('green.bmp', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        self.bitmap2 = wx.StaticBitmap(myPanel, -1, image1, (0, 0))
        pub.subscribe(self.addImage, 'Update')
        self.myButton = None
    def addImage(self):
        # Don't keep adding children to bitmap2
        if self.myButton:
            self.myButton.Destroy()
        self.myButton = wx.Button(self.bitmap2, -1, size =(30,30), pos=(20,20))
        image2 = wx.Image('blue.bmp', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        self.bitmap1 = wx.StaticBitmap(self.myButton, -1, image2, (0, 0))

class myThread(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.start()
    def run(self):
        while True:
            time.sleep(2)
            wx.CallAfter(pub.sendMessage, 'Update')
if __name__=='__main__':
    app = wx.App()
    frame = gui(parent=None, id=-1, title="Test")
    frame.Show()
    myThread()
    app.MainLoop()

相关内容

最新更新