实时更新wxPython grid

2024-04-25 00:50:44 发布

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

我从一个巨大的日志中提取数据,然后更新wxPython网格中的条目。用我的代码,我只能在操作完成后才能看到窗口,完成操作需要5分钟。有什么方法可以更新和查看时间=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()

Tags: the数据importself网格countcolumnframe
1条回答
网友
1楼 · 发布于 2024-04-25 00:50:44

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

相关问题 更多 >