对于使用wxPython和wx.richtext.RichTextCtrl的应用程序,内存泄漏存在问题。该应用程序与此类似-在窗框上的简单数字时钟。为了保持代码的整洁,我使用另一个模块来更新“文本模块”(ModuleClock.py)。
知道它为什么会这样的人吗?
我也尝试过:
Main.py
for(j=0;
ModuleClock.py
import wx
import wx.richtext # as rt
import ModuleClock #
from datetime import datetime
import os
import psutil
class MainWindow(wx.Frame):
counter = 0
lastcall = datetime.now()
def __init__(self, parent):
wx.Frame.__init__(self, parent, size=(600, 500), pos=(0, 0))
self.panel = wx.Panel(self)
self.panel.SetBackgroundColour('Black')
self.clock = wx.richtext.RichTextCtrl(self.panel, wx.ID_ANY, value="", size = (600,150), style=wx.VSCROLL | wx.HSCROLL | wx.NO_BORDER)
self.clock.SetBackgroundColour('Black')
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.update, self.timer)
self.timer.Start(100)
self.Show()
def update(self, event):
now = datetime.now()
self.counter = self.counter + 1
ModuleClock.update(self.clock)
# Check memory use by process every 30 seconds
if (now - self.lastcall).seconds > 30:
process = psutil.Process(os.getpid())
print('memory use =', process.memory_info().rss/1000, 'kb')
self.lastcall = now
app = wx.App(False)
frame = MainWindow(None)
app.MainLoop()
命令行输出
from datetime import datetime
def update(rto):
now = datetime.now()
# default is black text
rto.Clear()
rto.BeginFontSize(16)
rto.BeginTextColour("White")
rto.WriteText(now.strftime("%A %d %B\n"))
rto.BeginFontSize(36)
rto.WriteText(now.strftime("%H:%M:%S"))
答案 0 :(得分:1)
在ModuleClock
中,您永远不会取消对文本颜色或字体大小的更改,即,每个Begin
都有一个End
将ModuleClock
更改为:
from datetime import datetime
def update(rto):
now = datetime.now()
# default is black text
rto.Clear()
rto.BeginFontSize(16)
rto.WriteText(now.strftime("%A %d %B\n"))
rto.EndFontSize()
rto.BeginFontSize(36)
rto.WriteText(now.strftime("%H:%M:%S"))
rto.EndFontSize()
并在self.clock.BeginTextColour("White")
中的self.clock.SetBackgroundColour('Black')
之后添加Main.py
行