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

2024-07-05 10:46:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Windows7OS上使用PythonV2.7和WXPythonV3.0。在我的应用程序中,我有一个名为myPanel的面板。我在myPanel上有一个图像作为背景,图像名为green.bmpmyPanel包含一个名为myButton的按钮。这个myButton还包含一个名为blue.bmp的图像作为背景,线程只需更改myButton上的图像。在

为了演示,我在myButton上反复使用相同的图像。在现实世界中,我有不同的形象。

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

代码:代码中使用的图像可以从这里Green.bmpBlue.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()

谢谢你的时间。在


Tags: 内存代码from图像imageimportselfinit
1条回答
网友
1楼 · 发布于 2024-07-05 10:46:12

你的问题在addImage中。每次执行此行时:

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

您正在将另一个子窗口添加到自身位图2. 该窗口与添加的前一个窗口完全重叠,因此不明显您有多个子窗口。要查看发生的情况,请将以下行添加到addImage的底部:

^{pr2}$

要解决此问题,应在添加新按钮之前销毁所有子项。将此添加到addImage的顶部

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()

相关问题 更多 >