NGB
NGB

Reputation: 430

Update wxPython grid in real time

I am extracting data from a huge log and then updating the entry in wxPython grid. with the code I have I can see the window only after operation is complete, it takes 5 mins to complete the operation. Is there any way to update and see the data on grid from time = 0 itself. I tried:

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

Upvotes: 1

Views: 1788

Answers (1)

Rolf of Saxony
Rolf of Saxony

Reputation: 22448

Use wx.Yield() to hand control back to the main loop.
Here I use divmod to get every 1000th iteration and call wx.Yield.
I also use MoveCursorDownBlock to allow you to visually follow the updating grid.
You may wish to remove or amend this, as it will slow down the execution of your program.

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

Upvotes: 1

Related Questions