Hello. I have a program that displays MySQL data in a wxGrid and I want to make the grid to refresh and show the new changes when the user hits a Refresh button. I've been trying to use Refresh() but it doesn't seem to do the trick. Can anyone help me with this?
Here is the code for the frame. There are actually 3 wxGrid here but for now, im trying to make wxGrid
gridusers
to do refresh but to no avail. The last part defines the function of how the Refresh button should work but it won't work.
#Boa:Frame:mainframeimportwximportwx.gridfromwx.lib.anchorsimportLayoutAnchorsimportMySQLdbimportadduserdiagimportsaveconversiondiagimportconfirmloaddiagimportgetuserinfoimportgridsimportloadimportprepdfuncfromdatetimeimportdatetime#DATABASE CONNECTION#db=MySQLdb.connect("localhost","root","carla","prepaiddatabase")#open database connectioncursor=db.cursor()#prepare a cursor object using cursor() method####################defcreate(parent):returnmainframe(parent)[wxID_MAINFRAME,wxID_MAINFRAMEBUTTONADD,wxID_MAINFRAMEBUTTONCANCEL,wxID_MAINFRAMEBUTTONCHANGE,wxID_MAINFRAMEBUTTONCHANGESTATUS,wxID_MAINFRAMEBUTTONDEL,wxID_MAINFRAMEBUTTONLOAD,wxID_MAINFRAMEBUTTONREFRESH,wxID_MAINFRAMEBUTTONSAVE,wxID_MAINFRAMEBUTTONSEARCH,wxID_MAINFRAMEBUTTONTURNOFFALL,wxID_MAINFRAMEBUTTONTURNONALL,wxID_MAINFRAMECOMBOBOXDEVICE,wxID_MAINFRAMECOMBOBOXPMD,wxID_MAINFRAMEGRIDDEVICES,wxID_MAINFRAMEGRIDLOAD,wxID_MAINFRAMEGRIDUSERS,wxID_MAINFRAMEKWHTEXTCTRL,wxID_MAINFRAMEMAINPANEL,wxID_MAINFRAMENOTEBOOK1,wxID_MAINFRAMEPANELDEVICES,wxID_MAINFRAMEPANELLOAD1,wxID_MAINFRAMEPANELUSERS,wxID_MAINFRAMEPESOTEXTCTRL,wxID_MAINFRAMESEARCHCTRL1,wxID_MAINFRAMESTATICLINE1,wxID_MAINFRAMESTATICLINE2,wxID_MAINFRAMESTATICLINE3,wxID_MAINFRAMESTATICLINE4,wxID_MAINFRAMESTATICLINE5,wxID_MAINFRAMESTATICTEXT1,wxID_MAINFRAMESTATICTEXT10,wxID_MAINFRAMESTATICTEXT11,wxID_MAINFRAMESTATICTEXT12,wxID_MAINFRAMESTATICTEXT13,wxID_MAINFRAMESTATICTEXT2,wxID_MAINFRAMESTATICTEXT3,wxID_MAINFRAMESTATICTEXT4,wxID_MAINFRAMESTATICTEXT5,wxID_MAINFRAMESTATICTEXT6,wxID_MAINFRAMESTATICTEXT7,wxID_MAINFRAMESTATICTEXT8,wxID_MAINFRAMESTATICTEXT9,wxID_MAINFRAMETEXTCTRLENTEREDLOAD,wxID_MAINFRAMETEXTSTATUSSTR,]=[wx.NewId()for_init_ctrlsinrange(45)]classmainframe(wx.Frame):def_init_coll_boxsizeruser_Items(self,parent):# generated method, don't editparent.AddSizer(self.flexuser1,0,border=20,flag=wx.ALL|wx.EXPAND)parent.AddWindow(self.staticLine3,0,border=10,flag=wx.EXPAND|wx.ALL)parent.AddWindow(self.gridusers,1,border=20,flag=wx.EXPAND|wx.ALL)parent.AddWindow(self.buttonrefresh,0,border=20,flag=wx.RIGHT|wx.BOTTOM|wx.ALIGN_RIGHT)def_init_coll_flexload2_Items(self,parent):# generated method, don't editparent.AddWindow(self.buttonchange,0,border=0,flag=0)parent.AddWindow(self.buttonsave,0,border=0,flag=0)parent.AddWindow(self.buttoncancel,0,border=0,flag=0)def_init_coll_flexload3_Items(self,parent):# generated method, don't editparent.AddWindow(self.staticText9,0,border=0,flag=wx.ALIGN_CENTER_VERTICAL)parent.AddWindow(self.comboboxpmd,0,border=5,flag=wx.RIGHT)parent.AddWindow(self.staticText7,0,border=0,flag=wx.ALIGN_CENTER_VERTICAL)parent.AddWindow(self.textctrlenteredload,0,border=0,flag=0)parent.AddWindow(self.staticText8,0,border=0,flag=wx.ALIGN_CENTER_VERTICAL)parent.AddWindow(self.buttonload,0,border=0,flag=wx.EXPAND)def_init_coll_flexload1_Items(self,parent):# generated method, don't editparent.AddWindow(self.staticText1,0,border=10,flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL)parent.AddWindow(self.pesotextctrl,0,border=0,flag=wx.EXPAND|wx.ALIGN_CENTER_VERTICAL)parent.AddWindow(self.staticText2,0,border=0,flag=wx.ALIGN_CENTER_VERTICAL)parent.AddWindow(self.staticText3,0,border=10,flag=wx.RIGHT|wx.LEFT|wx.ALIGN_CENTER_VERTICAL)parent.AddWindow(self.kwhtextctrl,0,border=0,flag=wx.ALIGN_CENTER_VERTICAL|wx.EXPAND)parent.AddWindow(self.staticText4,0,border=0,flag=wx.ALIGN_CENTER_VERTICAL)def_init_coll_boxsizermain_Items(self,parent):# generated method, don't editparent.AddWindow(self.notebook1,1,border=0,flag=wx.EXPAND)def_init_coll_flexuser1_Items(self,parent):# generated method, don't editparent.AddWindow(self.buttonadd,0,border=0,flag=0)parent.AddWindow(self.buttondel,0,border=0,flag=0)parent.AddSpacer(wx.Size(8,8),border=0,flag=0)parent.AddWindow(self.searchCtrl1,0,border=0,flag=wx.EXPAND)parent.AddWindow(self.buttonsearch,0,border=0,flag=0)def_init_coll_boxsizerload_Items(self,parent):# generated method, don't editparent.AddSizer(self.flexload1,0,border=20,flag=wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND)parent.AddSizer(self.flexload2,0,border=20,flag=wx.ALIGN_CENTER_HORIZONTAL)parent.AddWindow(self.staticLine2,0,border=10,flag=wx.EXPAND|wx.ALL)parent.AddSizer(self.flexload3,0,border=20,flag=wx.RIGHT|wx.EXPAND|wx.TOP|wx.LEFT)parent.AddWindow(self.staticLine1,0,border=10,flag=wx.ALL|wx.EXPAND)parent.AddWindow(self.gridload,1,border=20,flag=wx.BOTTOM|wx.EXPAND|wx.RIGHT|wx.LEFT)def_init_coll_flexuser1_Growables(self,parent):# generated method, don't editparent.AddGrowableCol(2)parent.AddGrowableCol(3)def_init_coll_flexdevices1_Items(self,parent):# generated method, don't editparent.AddWindow(self.staticText11,0,border=20,flag=wx.ALIGN_CENTER_VERTICAL|wx.LEFT)parent.AddWindow(self.comboboxdevice,0,border=0,flag=0)parent.AddWindow(self.textstatusstr,0,border=0,flag=wx.EXPAND)def_init_coll_boxsizerdevices_Items(self,parent):# generated method, don't editparent.AddWindow(self.staticText10,0,border=20,flag=wx.LEFT|wx.TOP)parent.AddSpacer(wx.Size(8,8),border=0,flag=0)parent.AddSizer(self.flexdevices1,0,border=20,flag=wx.RIGHT|wx.LEFT|wx.EXPAND)parent.AddWindow(self.buttonchangestatus,0,border=10,flag=wx.ALL|wx.ALIGN_CENTER_HORIZONTAL)parent.AddWindow(self.staticLine4,0,border=10,flag=wx.ALL|wx.EXPAND)parent.AddWindow(self.staticText12,0,border=20,flag=wx.LEFT)parent.AddSizer(self.flexdevices2,0,border=0,flag=wx.ALIGN_CENTER_HORIZONTAL)parent.AddWindow(self.staticLine5,0,border=10,flag=wx.EXPAND|wx.ALL)parent.AddWindow(self.staticText13,0,border=20,flag=wx.LEFT)parent.AddWindow(self.griddevices,1,border=20,flag=wx.EXPAND|wx.ALL)def_init_coll_flexdevices2_Items(self,parent):# generated method, don't editparent.AddWindow(self.buttonturnonall,0,border=0,flag=0)parent.AddWindow(self.buttonturnoffall,0,border=0,flag=0)def_init_coll_notebook1_Pages(self,parent):# generated method, don't editparent.AddPage(imageId=-1,page=self.panelusers,select=True,text=u'Users')parent.AddPage(imageId=-1,page=self.panelload1,select=False,text=u'Load')parent.AddPage(imageId=-1,page=self.paneldevices,select=False,text=u'Devices')def_init_sizers(self):# generated method, don't editself.boxsizermain=wx.BoxSizer(orient=wx.VERTICAL)self.boxsizeruser=wx.BoxSizer(orient=wx.VERTICAL)self.flexuser1=wx.FlexGridSizer(cols=5,hgap=10,rows=1,vgap=0)self.flexuser1.SetFlexibleDirection(wx.HORIZONTAL)self.boxsizerload=wx.BoxSizer(orient=wx.VERTICAL)self.flexload1=wx.FlexGridSizer(cols=0,hgap=10,rows=1,vgap=0)self.flexload2=wx.FlexGridSizer(cols=0,hgap=20,rows=1,vgap=0)self.flexload3=wx.FlexGridSizer(cols=0,hgap=10,rows=1,vgap=0)self.boxsizerdevices=wx.BoxSizer(orient=wx.VERTICAL)self.flexdevices1=wx.FlexGridSizer(cols=0,hgap=10,rows=1,vgap=0)self.flexdevices2=wx.FlexGridSizer(cols=0,hgap=15,rows=1,vgap=0)self._init_coll_boxsizermain_Items(self.boxsizermain)self._init_coll_boxsizeruser_Items(self.boxsizeruser)self._init_coll_flexuser1_Items(self.flexuser1)self._init_coll_flexuser1_Growables(self.flexuser1)self._init_coll_boxsizerload_Items(self.boxsizerload)self._init_coll_flexload1_Items(self.flexload1)self._init_coll_flexload2_Items(self.flexload2)self._init_coll_flexload3_Items(self.flexload3)self._init_coll_boxsizerdevices_Items(self.boxsizerdevices)self._init_coll_flexdevices1_Items(self.flexdevices1)self._init_coll_flexdevices2_Items(self.flexdevices2)self.panelusers.SetSizer(self.boxsizeruser)self.panelload1.SetSizer(self.boxsizerload)self.mainpanel.SetSizer(self.boxsizermain)self.paneldevices.SetSizer(self.boxsizerdevices)def_init_ctrls(self,prnt):# generated method, don't editwx.Frame.__init__(self,id=wxID_MAINFRAME,name=u'mainframe',parent=prnt,pos=wx.Point(712,164),size=wx.Size(600,480),style=wx.SYSTEM_MENU|wx.MINIMIZE_BOX|wx.MAXIMIZE_BOX|wx.CAPTION|wx.CLIP_CHILDREN|wx.CLOSE_BOX,title=u'Prepaid Electricity')self.SetClientSize(wx.Size(584,442))self.mainpanel=wx.Panel(id=wxID_MAINFRAMEMAINPANEL,name=u'mainpanel',parent=self,pos=wx.Point(0,0),size=wx.Size(584,442),style=wx.TAB_TRAVERSAL)self.notebook1=wx.Notebook(id=wxID_MAINFRAMENOTEBOOK1,name='notebook1',parent=self.mainpanel,pos=wx.Point(0,0),size=wx.Size(584,442),style=0)self.panelusers=wx.Panel(id=wxID_MAINFRAMEPANELUSERS,name=u'panelusers',parent=self.notebook1,pos=wx.Point(0,0),size=wx.Size(576,416),style=wx.TAB_TRAVERSAL)self.paneldevices=wx.Panel(id=wxID_MAINFRAMEPANELDEVICES,name=u'paneldevices',parent=self.notebook1,pos=wx.Point(0,0),size=wx.Size(576,416),style=wx.TAB_TRAVERSAL)self.buttonadd=wx.Button(id=wxID_MAINFRAMEBUTTONADD,label=u'Add User',name=u'buttonadd',parent=self.panelusers,pos=wx.Point(20,20),size=wx.Size(75,23),style=0)self.buttonadd.Bind(wx.EVT_BUTTON,self.OnButtonaddButton,id=wxID_MAINFRAMEBUTTONADD)self.buttondel=wx.Button(id=wxID_MAINFRAMEBUTTONDEL,label=u'Delete User',name=u'buttondel',parent=self.panelusers,pos=wx.Point(105,20),size=wx.Size(75,23),style=0)self.buttonsearch=wx.Button(id=wxID_MAINFRAMEBUTTONSEARCH,label=u'Search',name=u'buttonsearch',parent=self.panelusers,pos=wx.Point(480,20),size=wx.Size(75,23),style=0)self.searchCtrl1=wx.SearchCtrl(id=wxID_MAINFRAMESEARCHCTRL1,name='searchCtrl1',parent=self.panelusers,pos=wx.Point(208,20),size=wx.Size(262,23),style=wx.TE_PROCESS_ENTER,value=u'')self.searchCtrl1.SetHelpText(u"Enter user's name")self.searchCtrl1.SetToolTipString(u"Enter user's name")self.searchCtrl1.SetDescriptiveText(u"Search user...")self.searchCtrl1.Bind(wx.EVT_TEXT_ENTER,self.OnSearchCtrl1TextEnter,id=wxID_MAINFRAMESEARCHCTRL1)self.gridusers=wx.grid.Grid(id=wxID_MAINFRAMEGRIDUSERS,name=u'gridusers',parent=self.panelusers,pos=wx.Point(20,105),size=wx.Size(536,248),style=0)self.gridusers.Enable(True)self.gridusers.EnableEditing(False)self.gridusers.SetToolTipString(u'Subscriber Database')self.gridusers.SetLabel(u'gridusers')self.gridusers.SetHelpText(u'')self.gridusers.SetGridLineColour(wx.Colour(0,192,192))self.gridusers.SetTable(tableBaseuser)self.panelload1=wx.Panel(id=wxID_MAINFRAMEPANELLOAD1,name=u'panelload1',parent=self.notebook1,pos=wx.Point(0,0),size=wx.Size(576,416),style=wx.TAB_TRAVERSAL)self.staticText1=wx.StaticText(id=wxID_MAINFRAMESTATICTEXT1,label=u'Current Load Conversion: ',name='staticText1',parent=self.panelload1,pos=wx.Point(20,24),size=wx.Size(132,13),style=0)self.staticText1.SetToolTipString(u'This will convert how much killowatt-hour is in P1 of load.')self.pesotextctrl=wx.TextCtrl(id=wxID_MAINFRAMEPESOTEXTCTRL,name=u'pesotextctrl',parent=self.panelload1,pos=wx.Point(172,20),size=wx.Size(100,21),style=0,value=u'')self.pesotextctrl.SetEditable(False)self.buttonchange=wx.Button(id=wxID_MAINFRAMEBUTTONCHANGE,label=u'Change',name=u'buttonchange',parent=self.panelload1,pos=wx.Point(155,61),size=wx.Size(75,23),style=0)self.buttonchange.SetToolTipString(u'Press to change values above.')self.buttonchange.Bind(wx.EVT_BUTTON,self.OnButtonchangeButton,id=wxID_MAINFRAMEBUTTONCHANGE)self.buttonsave=wx.Button(id=wxID_MAINFRAMEBUTTONSAVE,label=u'Save',name=u'buttonsave',parent=self.panelload1,pos=wx.Point(250,61),size=wx.Size(75,23),style=0)self.buttonsave.SetToolTipString(u'Saves the current values above.')self.buttonsave.Bind(wx.EVT_BUTTON,self.OnButtonsaveButton,id=wxID_MAINFRAMEBUTTONSAVE)self.buttoncancel=wx.Button(id=wxID_MAINFRAMEBUTTONCANCEL,label=u'Cancel',name=u'buttoncancel',parent=self.panelload1,pos=wx.Point(345,61),size=wx.Size(75,23),style=0)self.buttoncancel.SetToolTipString(u'Press to cancel changes.')self.buttoncancel.Bind(wx.EVT_BUTTON,self.OnButtoncancelButton,id=wxID_MAINFRAMEBUTTONCANCEL)self.staticText2=wx.StaticText(id=wxID_MAINFRAMESTATICTEXT2,label=u'Php',name='staticText2',parent=self.panelload1,pos=wx.Point(282,24),size=wx.Size(19,13),style=0)self.staticText3=wx.StaticText(id=wxID_MAINFRAMESTATICTEXT3,label=u':',name='staticText3',parent=self.panelload1,pos=wx.Point(321,24),size=wx.Size(4,13),style=0)self.kwhtextctrl=wx.TextCtrl(id=wxID_MAINFRAMEKWHTEXTCTRL,name=u'kwhtextctrl',parent=self.panelload1,pos=wx.Point(345,20),size=wx.Size(100,21),style=0,value=u'')self.kwhtextctrl.SetEditable(False)self.staticText4=wx.StaticText(id=wxID_MAINFRAMESTATICTEXT4,label=u'kWh',name='staticText4',parent=self.panelload1,pos=wx.Point(455,24),size=wx.Size(22,13),style=0)self.staticText5=wx.StaticText(id=wxID_MAINFRAMESTATICTEXT5,label=u'shoooooooooooooooooooowwwwwwwww up',name='staticText5',parent=self.panelload1,pos=wx.Point(-32768,104),size=wx.Size(219,13),style=0)self.staticText6=wx.StaticText(id=wxID_MAINFRAMESTATICTEXT6,label='staticText6',name='staticText6',parent=self.panelload1,pos=wx.Point(-32768,104),size=wx.Size(55,13),style=0)self.staticText7=wx.StaticText(id=wxID_MAINFRAMESTATICTEXT7,label=u'Enter Load:',name='staticText7',parent=self.panelload1,pos=wx.Point(275,131),size=wx.Size(57,13),style=0)self.textctrlenteredload=wx.TextCtrl(id=wxID_MAINFRAMETEXTCTRLENTEREDLOAD,name=u'textctrlenteredload',parent=self.panelload1,pos=wx.Point(342,126),size=wx.Size(100,21),style=0,value=u'')self.staticText8=wx.StaticText(id=wxID_MAINFRAMESTATICTEXT8,label=u'Php',name='staticText8',parent=self.panelload1,pos=wx.Point(452,131),size=wx.Size(19,13),style=0)self.staticText9=wx.StaticText(id=wxID_MAINFRAMESTATICTEXT9,label=u'Choose device ID:',name='staticText9',parent=self.panelload1,pos=wx.Point(20,131),size=wx.Size(100,13),style=0)self.staticLine2=wx.StaticLine(id=wxID_MAINFRAMESTATICLINE2,name='staticLine2',parent=self.panelload1,pos=wx.Point(10,94),size=wx.Size(556,2),style=0)self.staticLine3=wx.StaticLine(id=wxID_MAINFRAMESTATICLINE3,name='staticLine3',parent=self.panelusers,pos=wx.Point(10,73),size=wx.Size(556,2),style=0)self.staticLine1=wx.StaticLine(id=wxID_MAINFRAMESTATICLINE1,name='staticLine1',parent=self.panelload1,pos=wx.Point(10,159),size=wx.Size(556,2),style=0)self.buttonload=wx.Button(id=wxID_MAINFRAMEBUTTONLOAD,label=u'LOAD',name=u'buttonload',parent=self.panelload1,pos=wx.Point(481,126),size=wx.Size(54,23),style=0)self.buttonload.Bind(wx.EVT_BUTTON,self.OnButtonloadButton,id=wxID_MAINFRAMEBUTTONLOAD)self.comboboxpmd=wx.ComboBox(choices=["1","must","change","this"],id=wxID_MAINFRAMECOMBOBOXPMD,name=u'comboboxpmd',parent=self.panelload1,pos=wx.Point(130,126),size=wx.Size(130,21),style=0,value=u'')self.comboboxpmd.SetLabel(u'')self.gridload=wx.grid.Grid(id=wxID_MAINFRAMEGRIDLOAD,name=u'gridload',parent=self.panelload1,pos=wx.Point(20,171),size=wx.Size(536,225),style=0)self.gridload.EnableEditing(False)self.staticText10=wx.StaticText(id=wxID_MAINFRAMESTATICTEXT10,label=u'Individual Device Status Controls',name='staticText10',parent=self.paneldevices,pos=wx.Point(20,20),size=wx.Size(159,13),style=0)self.staticText11=wx.StaticText(id=wxID_MAINFRAMESTATICTEXT11,label=u'Choose device number: ',name='staticText11',parent=self.paneldevices,pos=wx.Point(40,45),size=wx.Size(117,13),style=0)self.comboboxdevice=wx.ComboBox(choices=[],id=wxID_MAINFRAMECOMBOBOXDEVICE,name=u'comboboxdevice',parent=self.paneldevices,pos=wx.Point(167,41),size=wx.Size(130,21),style=0,value=u'')self.comboboxdevice.SetLabel(u'')self.comboboxdevice.Bind(wx.EVT_TEXT,self.OnComboboxdeviceText,id=wxID_MAINFRAMECOMBOBOXDEVICE)self.comboboxdevice.Bind(wx.EVT_COMBOBOX,self.OnComboboxdeviceCombobox,id=wxID_MAINFRAMECOMBOBOXDEVICE)self.textstatusstr=wx.TextCtrl(id=wxID_MAINFRAMETEXTSTATUSSTR,name=u'textstatusstr',parent=self.paneldevices,pos=wx.Point(307,41),size=wx.Size(249,21),style=0,value=u'')self.textstatusstr.SetEditable(False)self.buttonchangestatus=wx.Button(id=wxID_MAINFRAMEBUTTONCHANGESTATUS,label=u'Change status of selected device',name=u'buttonchangestatus',parent=self.paneldevices,pos=wx.Point(198,72),size=wx.Size(179,23),style=0)self.buttonchangestatus.Bind(wx.EVT_BUTTON,self.OnButtonchangestatusButton,id=wxID_MAINFRAMEBUTTONCHANGESTATUS)self.staticLine4=wx.StaticLine(id=wxID_MAINFRAMESTATICLINE4,name='staticLine4',parent=self.paneldevices,pos=wx.Point(10,115),size=wx.Size(556,2),style=0)self.staticText12=wx.StaticText(id=wxID_MAINFRAMESTATICTEXT12,label=u'Overall Device Status Control',name='staticText12',parent=self.paneldevices,pos=wx.Point(20,127),size=wx.Size(142,13),style=0)self.buttonturnonall=wx.Button(id=wxID_MAINFRAMEBUTTONTURNONALL,label=u'Turn ON all devices',name=u'buttonturnonall',parent=self.paneldevices,pos=wx.Point(169,140),size=wx.Size(111,23),style=0)self.buttonturnonall.Bind(wx.EVT_BUTTON,self.OnButtonturnonallButton,id=wxID_MAINFRAMEBUTTONTURNONALL)self.buttonturnoffall=wx.Button(id=wxID_MAINFRAMEBUTTONTURNOFFALL,label=u'Turn OFF all devices',name=u'buttonturnoffall',parent=self.paneldevices,pos=wx.Point(295,140),size=wx.Size(111,23),style=0)self.buttonturnoffall.Bind(wx.EVT_BUTTON,self.OnButtonturnoffallButton,id=wxID_MAINFRAMEBUTTONTURNOFFALL)self.staticLine5=wx.StaticLine(id=wxID_MAINFRAMESTATICLINE5,name='staticLine5',parent=self.paneldevices,pos=wx.Point(10,173),size=wx.Size(556,2),style=0)self.staticText13=wx.StaticText(id=wxID_MAINFRAMESTATICTEXT13,label=u'Device Details',name='staticText13',parent=self.paneldevices,pos=wx.Point(20,185),size=wx.Size(68,13),style=0)self.griddevices=wx.grid.Grid(id=wxID_MAINFRAMEGRIDDEVICES,name=u'griddevices',parent=self.paneldevices,pos=wx.Point(20,218),size=wx.Size(536,178),style=0)self.griddevices.EnableEditing(False)self.buttonrefresh=wx.Button(id=wxID_MAINFRAMEBUTTONREFRESH,label=u'Refresh',name=u'buttonrefresh',parent=self.panelusers,pos=wx.Point(481,373),size=wx.Size(75,23),style=0)self.buttonrefresh.Bind(wx.EVT_BUTTON,self.OnButtonrefreshButton,id=wxID_MAINFRAMEBUTTONREFRESH)self._init_coll_notebook1_Pages(self.notebook1)self._init_sizers()def__init__(self,parent):self._init_ctrls(parent)defOnButtonaddButton(self,event):adduserdiag.callingdiag()defOnSearchCtrl1TextEnter(self,event):# XXX try to display the datakeyword=self.searchCtrl1.GetValue()statement="SELECT accntNo, FName, LName FROM subscriber WHERE FName LIKE '%%%s%%';"%(keyword)cursor.execute(statement)results=cursor.fetchall()#event.Skip()defOnButtonchangeButton(self,event):globalpesooldpesoold=self.pesotextctrl.GetValue()globalkwholdkwhold=self.kwhtextctrl.GetValue()self.pesotextctrl.SetEditable(1)self.kwhtextctrl.SetEditable(1)self.pesotextctrl.SetInsertionPoint(0)defOnButtoncancelButton(self,event):globalpesooldglobalkwholdself.pesotextctrl.SetEditable(0)self.kwhtextctrl.SetEditable(0)self.pesotextctrl.SetValue(pesoold)self.kwhtextctrl.SetValue(kwhold)defOnButtonsaveButton(self,event):PHP=float(self.pesotextctrl.GetValue())KWH=float(self.kwhtextctrl.GetValue())prepdfunc.saveconversion(KWH,PHP)self.pesotextctrl.SetValue(prepdfunc.getconversionphp())self.kwhtextctrl.SetValue(prepdfunc.getconversionkwh())self.pesotextctrl.SetEditable(0)self.kwhtextctrl.SetEditable(0)defOnButtonloadButton(self,event):loadphp=self.textctrlenteredload.GetValue()pmdid=self.comboboxpmd.GetValue()load.newload(pmdid,loadphp)defOnComboboxdeviceText(self,event):deviceid=self.comboboxdevice.GetValue()statusstr=prepdfunc.getstringforguidevices(deviceid)self.textstatusstr.SetEditable(1)self.textstatusstr.SetValue(statusstr)self.textstatusstr.SetEditable(0)defOnComboboxdeviceCombobox(self,event):deviceid=self.comboboxdevice.GetValue()statusstr=prepdfunc.getstringforguidevices(deviceid)self.textstatusstr.SetEditable(1)self.textstatusstr.SetValue(statusstr)self.textstatusstr.SetEditable(0)defOnButtonchangestatusButton(self,event):deviceid=self.comboboxdevice.GetValue()prepdfunc.changestatus(deviceid)statusstr=prepdfunc.getstringforguidevices(deviceid)self.textstatusstr.SetEditable(1)self.textstatusstr.SetValue(statusstr)self.textstatusstr.SetEditable(0)defOnButtonturnonallButton(self,event):prepdfunc.turnonall()defOnButtonturnoffallButton(self,event):prepdfunc.turnoffall()defOnButtonrefreshButton(self,event):self.gridusers.EnableEditing(True)self.gridload.EnableEditing(True)self.griddevices.EnableEditing(True)tableBaseuser=grids.GenericTable(grids.datausers(),grids.usersrowlabels(),grids.userscollabels())tableBaseload=grids.GenericTable(grids.dataload(),grids.loadrowlabels(),grids.loadcollabels())tableBasedevices=grids.GenericTable(grids.datadevices(),grids.devicesrowlabels(),grids.devicescollabels())self.gridusers.SetTable(tableBaseuser)self.gridload.SetTable(tableBaseload)self.griddevices.SetTable(tableBasedevices)self.gridusers.EnableEditing(False)self.gridload.EnableEditing(False)self.griddevices.EnableEditing(False)self.Refresh()tableBaseuser=grids.GenericTable(grids.datausers(),grids.usersrowlabels(),grids.userscollabels())tableBaseload=grids.GenericTable(grids.dataload(),grids.loadrowlabels(),grids.loadcollabels())tableBasedevices=grids.GenericTable(grids.datadevices(),grids.devicesrowlabels(),grids.devicescollabels())
Thank you in advance for the advices you will give.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Haven't looked in detail at the code but self.Refresh() is just refreshing the window and its children from a ui point of view, i.e. the pixels to be drawn on the screen.
With a wx.grid you want to use wxPyGridTableBase and after it is changed with the new data you need to tell the grid, have a look at http://wiki.wxpython.org/UpdatingGridData and there are some other pages related to wx.Grid you should probably look at.
Werner
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I don't really quite understand it.
I'm new to wxGrid and wxGridTableBase and from how I look at it, wxGrid is somehow like a container for a table or something (I don't know how to put it into another term) and wxGridTableBase is the table to be placed in the wxGrid. Is this correct?
O_O?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
hmm. when i checked my methods, it seems that the method i created for getting my data from the database return the same data before there were changes made… this only happens when changes are made through my dialogbox. i don't know why this is happening though.
but when i tried with with changes that are not from the dialogbox i created, it seems that the method i used to get the data will now get the new data.
can anyone help me??
this is my code for the dialogbox
importwximportgridsfromwx.lib.anchorsimportLayoutAnchorsfromdatetimeimportdatetimeimportMySQLdb#DATABASE CONNECTION#db=MySQLdb.connect("localhost","root","carla","prepaiddatabase")#open database connectioncursor=db.cursor()#prepare a cursor object using cursor() method####################defcreate(parent):returnDialog1(parent)[wxID_DIALOG1,wxID_DIALOG1BUTTONADD,wxID_DIALOG1BUTTONCANCEL,wxID_DIALOG1STATICTEXT1,wxID_DIALOG1STATICTEXT2,wxID_DIALOG1STATICTEXT3,wxID_DIALOG1STATICTEXT4,wxID_DIALOG1STATICTEXT5,wxID_DIALOG1STATICTEXT6,wxID_DIALOG1STATICTEXT7,wxID_DIALOG1STATICTEXT8,wxID_DIALOG1STATICTEXT9,wxID_DIALOG1TEXTCTRLCONTACTNO,wxID_DIALOG1TEXTCTRLDOORNO,wxID_DIALOG1TEXTCTRLFNAME,wxID_DIALOG1TEXTCTRLLNAME,wxID_DIALOG1TEXTCTRLMNAME,wxID_DIALOG1TEXTCTRLPASS,wxID_DIALOG1TEXTCTRLPMDID,wxID_DIALOG1TEXTCTRLTHRESHOLD,]=[wx.NewId()for_init_ctrlsinrange(20)]classDialog1(wx.Dialog):def_init_coll_flexGridSizer1_Items(self,parent):# generated method, don't editparent.AddWindow(self.staticText1,0,border=0,flag=wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)parent.AddWindow(self.textctrlfname,0,border=0,flag=wx.ALIGN_LEFT|wx.EXPAND)parent.AddWindow(self.staticText2,0,border=0,flag=wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)parent.AddWindow(self.textctrlmname,0,border=0,flag=wx.ALIGN_LEFT|wx.EXPAND)parent.AddWindow(self.staticText3,0,border=0,flag=wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)parent.AddWindow(self.textctrllname,0,border=0,flag=wx.ALIGN_LEFT|wx.EXPAND)parent.AddWindow(self.staticText4,0,border=0,flag=wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)parent.AddWindow(self.textctrldoorno,0,border=0,flag=wx.ALIGN_LEFT|wx.EXPAND)parent.AddWindow(self.staticText5,0,border=0,flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT)parent.AddWindow(self.textctrlcontactno,0,border=0,flag=wx.ALIGN_LEFT|wx.EXPAND)parent.AddWindow(self.staticText9,0,border=0,flag=wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)parent.AddWindow(self.textctrlpass,0,border=0,flag=wx.EXPAND)parent.AddWindow(self.staticText6,0,border=0,flag=wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)parent.AddWindow(self.textctrlpmdid,0,border=0,flag=wx.EXPAND)parent.AddWindow(self.staticText8,0,border=0,flag=wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)parent.AddWindow(self.textctrlthreshold,0,border=0,flag=wx.EXPAND)def_init_coll_gridSizer1_Items(self,parent):# generated method, don't editparent.AddWindow(self.buttonadd,0,border=10,flag=wx.ALL|wx.ALIGN_RIGHT)parent.AddWindow(self.buttoncancel,0,border=10,flag=wx.ALIGN_LEFT|wx.ALL)def_init_coll_flexGridSizer1_Growables(self,parent):# generated method, don't editparent.AddGrowableCol(0)parent.AddGrowableCol(1)def_init_coll_boxSizer1_Items(self,parent):# generated method, don't editparent.AddWindow(self.staticText7,0,border=20,flag=wx.RIGHT|wx.LEFT|wx.TOP)parent.AddSizer(self.flexGridSizer1,0,border=20,flag=wx.EXPAND|wx.ALIGN_LEFT|wx.ALL)parent.AddSizer(self.gridSizer1,0,border=0,flag=wx.EXPAND|wx.ALIGN_CENTER)def_init_sizers(self):# generated method, don't editself.boxSizer1=wx.BoxSizer(orient=wx.VERTICAL)self.flexGridSizer1=wx.FlexGridSizer(cols=2,hgap=10,rows=6,vgap=5)self.flexGridSizer1.SetFlexibleDirection(wx.HORIZONTAL)self.gridSizer1=wx.GridSizer(cols=0,hgap=0,rows=1,vgap=0)self._init_coll_boxSizer1_Items(self.boxSizer1)self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1)self._init_coll_gridSizer1_Items(self.gridSizer1)self.SetSizer(self.boxSizer1)def_init_ctrls(self,prnt):# generated method, don't editwx.Dialog.__init__(self,id=wxID_DIALOG1,name='',parent=prnt,pos=wx.Point(508,268),size=wx.Size(450,360),style=wx.DEFAULT_DIALOG_STYLE,title=u'Add New User')self.SetClientSize(wx.Size(434,322))self.textctrlfname=wx.TextCtrl(id=wxID_DIALOG1TEXTCTRLFNAME,name=u'textctrlfname',parent=self,pos=wx.Point(147,53),size=wx.Size(267,21),style=0,value=u'')self.textctrlfname.SetMaxLength(45)self.textctrlmname=wx.TextCtrl(id=wxID_DIALOG1TEXTCTRLMNAME,name=u'textctrlmname',parent=self,pos=wx.Point(147,79),size=wx.Size(267,21),style=0,value=u'')self.textctrlmname.SetMaxLength(45)self.textctrllname=wx.TextCtrl(id=wxID_DIALOG1TEXTCTRLLNAME,name=u'textctrllname',parent=self,pos=wx.Point(147,105),size=wx.Size(267,21),style=0,value=u'')self.textctrllname.SetMaxLength(45)self.textctrldoorno=wx.TextCtrl(id=wxID_DIALOG1TEXTCTRLDOORNO,name=u'textctrldoorno',parent=self,pos=wx.Point(147,131),size=wx.Size(267,21),style=0,value=u'')self.textctrldoorno.SetToolTipString(u'textctrldoorno')self.textctrldoorno.SetMaxLength(5)self.textctrlcontactno=wx.TextCtrl(id=wxID_DIALOG1TEXTCTRLCONTACTNO,name=u'textctrlcontactno',parent=self,pos=wx.Point(147,157),size=wx.Size(267,21),style=0,value=u'')self.textctrlcontactno.SetMaxLength(11)self.textctrlpass=wx.TextCtrl(id=wxID_DIALOG1TEXTCTRLPASS,name=u'textctrlpass',parent=self,pos=wx.Point(147,183),size=wx.Size(267,21),style=0,value=u'Default password is 1234')self.textctrlpmdid=wx.TextCtrl(id=wxID_DIALOG1TEXTCTRLPMDID,name=u'textctrlpmdid',parent=self,pos=wx.Point(147,209),size=wx.Size(267,21),style=0,value=u'')self.textctrlpmdid.SetMaxLength(11)self.textctrlthreshold=wx.TextCtrl(id=wxID_DIALOG1TEXTCTRLTHRESHOLD,name=u'textctrlthreshold',parent=self,pos=wx.Point(147,235),size=wx.Size(267,21),style=0,value=u'')self.textctrlthreshold.SetToolTipString(u'textctrlthreshold')self.textctrlthreshold.SetMaxLength(20)self.textctrlthreshold.SetHelpText(u'')self.buttonadd=wx.Button(id=wxID_DIALOG1BUTTONADD,label=u'Add',name=u'buttonadd',parent=self,pos=wx.Point(132,286),size=wx.Size(75,23),style=0)self.buttonadd.Bind(wx.EVT_BUTTON,self.OnButtonaddButton,id=wxID_DIALOG1BUTTONADD)self.buttoncancel=wx.Button(id=wxID_DIALOG1BUTTONCANCEL,label=u'Cancel',name=u'buttoncancel',parent=self,pos=wx.Point(227,286),size=wx.Size(75,23),style=0)self.buttoncancel.Bind(wx.EVT_BUTTON,self.OnButtoncancelButton,id=wxID_DIALOG1BUTTONCANCEL)self.staticText6=wx.StaticText(id=wxID_DIALOG1STATICTEXT6,label=u'Device ID:',name='staticText6',parent=self,pos=wx.Point(86,213),size=wx.Size(51,13),style=0)self.staticText7=wx.StaticText(id=wxID_DIALOG1STATICTEXT7,label=u'Please fill up the following details.',name='staticText7',parent=self,pos=wx.Point(20,20),size=wx.Size(162,13),style=0)self.staticText8=wx.StaticText(id=wxID_DIALOG1STATICTEXT8,label=u'Threshold Level in KWH:',name='staticText8',parent=self,pos=wx.Point(20,239),size=wx.Size(117,13),style=0)self.staticText9=wx.StaticText(id=wxID_DIALOG1STATICTEXT9,label=u'Password:',name='staticText9',parent=self,pos=wx.Point(86,187),size=wx.Size(51,13),style=0)self.staticText1=wx.StaticText(id=wxID_DIALOG1STATICTEXT1,label=u'First Name:',name='staticText1',parent=self,pos=wx.Point(82,57),size=wx.Size(55,13),style=0)self.staticText2=wx.StaticText(id=wxID_DIALOG1STATICTEXT2,label=u'Middle Name:',name='staticText2',parent=self,pos=wx.Point(72,83),size=wx.Size(65,13),style=0)self.staticText3=wx.StaticText(id=wxID_DIALOG1STATICTEXT3,label=u'Last Name:',name='staticText3',parent=self,pos=wx.Point(82,109),size=wx.Size(55,13),style=0)self.staticText4=wx.StaticText(id=wxID_DIALOG1STATICTEXT4,label=u'Door Number:',name='staticText4',parent=self,pos=wx.Point(69,135),size=wx.Size(68,13),style=0)self.staticText5=wx.StaticText(id=wxID_DIALOG1STATICTEXT5,label=u'Contact Number:',name='staticText5',parent=self,pos=wx.Point(54,161),size=wx.Size(83,13),style=0)self._init_sizers()def__init__(self,parent):self._init_ctrls(parent)defOnButtoncancelButton(self,event):dlg.Destroy()defOnButtonaddButton(self,event):FName=self.textctrlfname.GetValue()MName=self.textctrlmname.GetValue()LName=self.textctrllname.GetValue()DoorNo=self.textctrldoorno.GetValue()ContactNo=self.textctrlcontactno.GetValue()DateSubscribed=getdate()DeviceID=self.textctrlpmdid.GetValue()Threshold=self.textctrlthreshold.GetValue()Password=self.textctrlpass.GetValue()Password=verifypass(Password)inserttosubscriber(Password,FName,MName,LName,DoorNo,ContactNo,DateSubscribed,DeviceID)insertthreshold(DeviceID,Threshold)insertdefaultstatus(DeviceID)dlg.Hide()defgetdate():date=datetime.now()date=str(date.strftime("%Y-%m-%d"))returndatedefinserttosubscriber(Password,FName,MName,LName,DoorNo,ContactNo,DateSubscribed,DeviceID):data=grids.datausers()printdatastatement="INSERT subscriber(Password, FName, MName, LName, DoorNo, ContactNo, DateSubscribed, DeviceID) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')"%(Password,FName,MName,LName,DoorNo,ContactNo,DateSubscribed,DeviceID)try:cursor.execute(statement)db.commit()except:db.rollback()data=grids.datausers()printdatareturndefinsertthreshold(DeviceID,Threshold):#Get accntNostatement="SELECT accntNo FROM subscriber WHERE DeviceID = '%s' ORDER BY accntNo DESC"%(DeviceID)cursor.execute(statement)accntNo=cursor.fetchone()accntNo=accntNo[0]#Verify if there is existing record in devicedtls with DeviceID registeredstatement="SELECT DeviceID FROM devicedtls WHERE DeviceID = '%s'"%(DeviceID)cursor.execute(statement)result=cursor.fetchall()if(result==()):RemBal=0statement="INSERT devicedtls (DeviceID, accntNo, Threshold, RemBal) VALUES ('%s', '%s', '%s', '%s')"%(DeviceID,accntNo,Threshold,RemBal)try:cursor.execute(statement)db.commit()except:db.rollback()else:statement="UPDATE devicedtls SET accntNo = '%s', Threshold = '%s', RemBal = '0' WHERE DeviceID = '%s'"%(accntNo,Threshold,DeviceID)try:cursor.execute(statement)db.commit()except:db.rollback()returndefinsertdefaultstatus(DeviceID):#Verify if there is existing recordstatement="SELECT DeviceID FROM devicestatus WHERE DeviceID = '%s'"%(DeviceID)cursor.execute(statement)result=cursor.fetchall()if(result==()):statement="INSERT devicestatus (DeviceID, Status) VALUES ('%s', 'off')"%(DeviceID)try:cursor.execute(statement)db.commit()except:db.rollback()else:statement="UPDATE devicestatus SET Status='off' WHERE DeviceID = '%s'"%(DeviceID)try:cursor.execute(statement)db.commit()except:db.rollback()returndefverifypass(Password):Password1=str(Password[0:7])Password1=str.lower(Password1)if(Password1=="default"):Password1='1234'Password=Password1else:Password=PasswordreturnPasswordif__name__=='__main__':app=wx.PySimpleApp()dlg=create(None)try:dlg.ShowModal()finally:dlg.Destroy()app.MainLoop()
and this is the code for the method for getting the data from my table in mysql:
def datausers():
statement = "SELECT * FROM subscriber;"
cursor.execute(statement)
data = cursor.fetchall()
return data
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello. I have a program that displays MySQL data in a wxGrid and I want to make the grid to refresh and show the new changes when the user hits a Refresh button. I've been trying to use Refresh() but it doesn't seem to do the trick. Can anyone help me with this?
Here is the code for the frame. There are actually 3 wxGrid here but for now, im trying to make wxGrid
to do refresh but to no avail. The last part defines the function of how the Refresh button should work but it won't work.
Thank you in advance for the advices you will give.
Hi,
Haven't looked in detail at the code but self.Refresh() is just refreshing the window and its children from a ui point of view, i.e. the pixels to be drawn on the screen.
With a wx.grid you want to use wxPyGridTableBase and after it is changed with the new data you need to tell the grid, have a look at http://wiki.wxpython.org/UpdatingGridData and there are some other pages related to wx.Grid you should probably look at.
Werner
I don't really quite understand it.
I'm new to wxGrid and wxGridTableBase and from how I look at it, wxGrid is somehow like a container for a table or something (I don't know how to put it into another term) and wxGridTableBase is the table to be placed in the wxGrid. Is this correct?
O_O?
hmm. when i checked my methods, it seems that the method i created for getting my data from the database return the same data before there were changes made… this only happens when changes are made through my dialogbox. i don't know why this is happening though.
but when i tried with with changes that are not from the dialogbox i created, it seems that the method i used to get the data will now get the new data.
can anyone help me??
this is my code for the dialogbox
and this is the code for the method for getting the data from my table in mysql:
nevermind the post above (the 4th post)… i already fixed it. it seems that the methods should be in the same module… :)
what is the difference between wxGridTableBase and wxPyGridTableBase??
Hmmm, I didn't follow what the link said but I used setTable again in the refresh button and added ForceRefresh() and now it seems to work just fine.
Hi,
Whenever there is a wxPysomething version you should use that, it is wrapped differently and allows better control/interaction from wxPython.
This is a wxPython question you might get much better answers on this type of stuff on the wxPython-user list.
Werner
Thank you very much. :)