how to refresh grid on a notebook?

D

dimitri pater

Hello,
To be honest, I also posted this on the wxPython mailing list. But I
thought maybe some of you on the python list can help me...

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top