我从一个巨大的日志中提取数据,然后更新wxPython网格中的条目。使用我的代码我只能在操作完成后才能看到窗口,完成操作需要5分钟。有没有办法从time = 0本身更新并查看网格上的数据。 我试过了:
import wx
import wx.grid as gridlib
class MyForm(wx.Frame):
def __init__(self):
##
# constructor to create the basic frame
wx.Frame.__init__(self, None, wx.ID_ANY, "Tool")
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
self.grid = gridlib.Grid(panel)
rows = 4
column = 600000
self.grid.CreateGrid(column, rows)
self.count = 0
# change a couple column labels
self.grid.SetColLabelValue(0, "Timestamp")
self.grid.SetColLabelValue(1, "CMD")
self.grid.SetColLabelValue(2, "Address")
self.grid.SetColLabelValue(3, "Data")
# Few More operations to calculate CMD,Timestamp field
for i in range(10**5):
self.count += 1
self.grid.SetCellValue(self.count,1,'CMD4')
self.grid.SetCellValue(self.count,0,str(self.count))
self.grid.SetCellValue(self.count, 2, "Extracted Address")
self.grid.SetCellValue(self.count, 3, "Extracted Data")
# change the row labels
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.grid, 1, wx.EXPAND, 5)
panel.SetSizer(sizer)
if __name__ == "__main__":
app = wx.App()
frame = MyForm()
frame.Show()
app.MainLoop()
答案 0 :(得分:1)
使用wx.Yield()
将控制权交回主回路
在这里,我使用divmod
获取每1000次迭代并调用wx.Yield
我还使用MoveCursorDownBlock
来直观地跟踪更新网格
您可能希望删除或修改此项,因为这会降低程序的执行速度。
import wx
import wx.grid as gridlib
class MyForm(wx.Frame):
def __init__(self):
##
# constructor to create the basic frame
wx.Frame.__init__(self, None, wx.ID_ANY, "Tool")
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
self.grid = gridlib.Grid(panel)
rows = 4
column = 100001
self.grid.CreateGrid(column, rows)
self.count = 0
# change a couple column labels
self.grid.SetColLabelValue(0, "Timestamp")
self.grid.SetColLabelValue(1, "CMD")
self.grid.SetColLabelValue(2, "Address")
self.grid.SetColLabelValue(3, "Data")
# Few More operations to calculate CMD,Timestamp field
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.grid, 1, wx.EXPAND, 5)
panel.SetSizer(sizer)
self.Show()
for i in range(10**5):
self.count += 1
self.grid.SetCellValue(self.count,1,'CMD4')
self.grid.SetCellValue(self.count,0,str(self.count))
self.grid.SetCellValue(self.count, 2, "Extracted Address")
self.grid.SetCellValue(self.count, 3, "Extracted Data")
quo,rem = divmod(self.count,1000)
if rem == 0:
self.grid.MoveCursorDownBlock(expandSelection=False)
wx.Yield()
if __name__ == "__main__":
app = wx.App()
frame = MyForm()
app.MainLoop()