how to refresh grid on a notebook?

Hello,
After a long day trying to figure this out I still don't know how to
solve this problem, maybe some of you can help me out.
I am trying to refresh a pane of a notebook that contains a grid that
contains data from a MySQL database. Here is the code (sorry, it's
quite long):

#!/usr/bin/env python
import wx
import wx.grid
import getdata
# getdata.py is a module I wrote to connect to the MySQL database, it works

db = getdata.Eb_db("www.serpia.com", "gedrag5")

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.notebook_1 = wx.Notebook(self, -1, style=0)
        self.notebook_1_pane_2 = wx.Panel(self.notebook_1, -1)
        self.notebook_1_pane_1 = wx.Panel(self.notebook_1, -1)
        self.label_1 = wx.StaticText(self.notebook_1_pane_1, -1, "Input")
        self.text_ctrl_1 = wx.TextCtrl(self.notebook_1_pane_1, -1, "")
        self.button_1 = wx.Button(self.notebook_1_pane_1, -1, "submit")
        self.button_2 = wx.Button(self.notebook_1_pane_2, -1, "refresh")
        self.grid_1 = wx.grid.Grid(self.notebook_1_pane_2, -1, size=(1, 1))

        self.__set_properties()
        self.__do_layout()
        # create an event for button_2
        wx.EVT_BUTTON(self, self.button_2.GetId(), self.fillGrid)

    def __set_properties(self):
        self.SetTitle("frame_1")
        self.SetSize((400, 400))
        
    def fillGrid(self, event):
        # fill grid with data from database
        self.grid_1.CreateGrid(len(db.data), len(db.fields))
        index = 0
        for item in db.fields:
            self.grid_1.SetColLabelValue(index, item[0])
            index += 1
                
        for row in range(len(db.data)):
            for col in range(len(db.data[row])):
                value = db.data[row][col]
                self.grid_1.SetCellValue(row,col,value)
                
    def __do_layout(self):
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_3 = wx.GridSizer(2, 1, 0, 0)
        grid_sizer_2 = wx.GridSizer(2, 2, 0, 0)
        grid_sizer_2.Add(self.label_1, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_2.Add(self.text_ctrl_1, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_2.Add(self.button_1, 0, wx.FIXED_MINSIZE, 0)
        self.notebook_1_pane_1.SetAutoLayout(True)
        self.notebook_1_pane_1.SetSizer(grid_sizer_2)
        grid_sizer_2.Fit(self.notebook_1_pane_1)
        grid_sizer_2.SetSizeHints(self.notebook_1_pane_1)
        grid_sizer_3.Add(self.button_2, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_3.Add(self.grid_1, 1, wx.EXPAND, 0)
        self.notebook_1_pane_2.SetAutoLayout(True)
        self.notebook_1_pane_2.SetSizer(grid_sizer_3)
        grid_sizer_3.Fit(self.notebook_1_pane_2)
        grid_sizer_3.SetSizeHints(self.notebook_1_pane_2)
        self.notebook_1.AddPage(self.notebook_1_pane_1, "Output")
        self.notebook_1.AddPage(self.notebook_1_pane_2, "Input")
        sizer_1.Add(wx.NotebookSizer(self.notebook_1), 1, wx.EXPAND, 0)
        self.SetAutoLayout(True)
        self.SetSizer(sizer_1)
        self.Layout()

if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_1 = MyFrame(None, -1, "")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()

As you can see, I use "button_2" on the second pane of the notebook to
fill the grid. But what I really want to do is to click on the pane
itself to update the grid.
And there is another problem, when I use "button_2 ", it works only
once. I get the following error:
C++ assertion "wxAssertFailure" failed in
../src/generic/grid.cpp(4018): wxGrid::CreateGrid or wxGrid::SetTable
called more than once.
I think maybe I should destroy the grid first, but I don't know how to
do this properly. I 've tried several things.

I hope that I explained my problem well enough and that somebody can
give some clues on how to solve this.

Thanks,
Dimitri

···

--
Please visit dimitri's website: www.serpia.com

dimitri pater wrote:

Hello,
After a long day trying to figure this out I still don't know how to
solve this problem, maybe some of you can help me out.
I am trying to refresh a pane of a notebook that contains a grid that
contains data from a MySQL database. Here is the code

[...]

As you can see, I use "button_2" on the second pane of the notebook to
fill the grid. But what I really want to do is to click on the pane
itself to update the grid.
And there is another problem, when I use "button_2 ", it works only
once. I get the following error:
C++ assertion "wxAssertFailure" failed in
../src/generic/grid.cpp(4018): wxGrid::CreateGrid or wxGrid::SetTable
called more than once.

You should probably be deriving your own table class and using that instead of the default table you get with CreateGrid. That way you can more easily manage fetching the data from the db only as it is needed to be viewed, etc. See the samples in the demo, and also http://wiki.wxpython.org/index.cgi/wxGrid_20Manual and http://wiki.wxpython.org/index.cgi/wxPyGridTableBase

I think maybe I should destroy the grid first, but I don't know how to
do this properly.

This is possible too, but probably not the best solution. If you want to try it then you can call the grid's Destroy method and then create a new instance of wx.grid.Grid.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Thanks Robin!
I will check the links and do some research in deriving my own table class.

bye,
Dimitri

···

On Fri, 18 Mar 2005 17:49:21 -0800, Robin Dunn <robin@alldunn.com> wrote:

dimitri pater wrote:
> Hello,
> After a long day trying to figure this out I still don't know how to
> solve this problem, maybe some of you can help me out.
> I am trying to refresh a pane of a notebook that contains a grid that
> contains data from a MySQL database. Here is the code

[...]

>
> As you can see, I use "button_2" on the second pane of the notebook to
> fill the grid. But what I really want to do is to click on the pane
> itself to update the grid.
> And there is another problem, when I use "button_2 ", it works only
> once. I get the following error:
> C++ assertion "wxAssertFailure" failed in
> ../src/generic/grid.cpp(4018): wxGrid::CreateGrid or wxGrid::SetTable
> called more than once.

You should probably be deriving your own table class and using that
instead of the default table you get with CreateGrid. That way you can
more easily manage fetching the data from the db only as it is needed to
be viewed, etc. See the samples in the demo, and also
http://wiki.wxpython.org/index.cgi/wxGrid_20Manual and
http://wiki.wxpython.org/index.cgi/wxPyGridTableBase

> I think maybe I should destroy the grid first, but I don't know how to
> do this properly.

This is possible too, but probably not the best solution. If you want
to try it then you can call the grid's Destroy method and then create a
new instance of wx.grid.Grid.

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

--
Please visit dimitri's website: www.serpia.com

For anyone who is interested,
I wrote a separate function to create the layout of the grid on the
notebook and used:
wx.EVT_NOTEBOOK_PAGE_CHANGING(self, self.notebook_1.GetId(), self.fillGrid)
to trigger the event (no need to destroy the grid first :slight_smile:
it works!
Dimitri
(If somebody needs more details, I'd be more than happy to share them)

···

On Sat, 19 Mar 2005 21:19:01 +0100, dimitri pater <dimitri.pater@gmail.com> wrote:

Thanks Robin!
I will check the links and do some research in deriving my own table class.

bye,
Dimitri

On Fri, 18 Mar 2005 17:49:21 -0800, Robin Dunn <robin@alldunn.com> wrote:
> dimitri pater wrote:
> > Hello,
> > After a long day trying to figure this out I still don't know how to
> > solve this problem, maybe some of you can help me out.
> > I am trying to refresh a pane of a notebook that contains a grid that
> > contains data from a MySQL database. Here is the code
>
> [...]
>
> >
> > As you can see, I use "button_2" on the second pane of the notebook to
> > fill the grid. But what I really want to do is to click on the pane
> > itself to update the grid.
> > And there is another problem, when I use "button_2 ", it works only
> > once. I get the following error:
> > C++ assertion "wxAssertFailure" failed in
> > ../src/generic/grid.cpp(4018): wxGrid::CreateGrid or wxGrid::SetTable
> > called more than once.
>
> You should probably be deriving your own table class and using that
> instead of the default table you get with CreateGrid. That way you can
> more easily manage fetching the data from the db only as it is needed to
> be viewed, etc. See the samples in the demo, and also
> http://wiki.wxpython.org/index.cgi/wxGrid_20Manual and
> http://wiki.wxpython.org/index.cgi/wxPyGridTableBase
>
>
> > I think maybe I should destroy the grid first, but I don't know how to
> > do this properly.
>
> This is possible too, but probably not the best solution. If you want
> to try it then you can call the grid's Destroy method and then create a
> new instance of wx.grid.Grid.
>
> --
> Robin Dunn
> Software Craftsman
> http://wxPython.org Java give you jitters? Relax with wxPython!
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
> For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org
>
>

--
Please visit dimitri's website: www.serpia.com

--
Please visit dimitri's website: www.serpia.com