I got this error when i was running a tuturial on how to view data using a grid in wx. I downloaded the modules for wx. I wandering if anyone can help me. this is the error i got

Traceback (most recent call last):
  File "C:\Update\data.py", line 7, in <module>
    class MyFrame(wx.Frame):
  File "C:\Update\data.py", line 27, in MyFrame
    self.grid_1.SetColLabelValue(index, item[0])
NameError: name 'self' is not defined

This is the code i put in.

import wx   
import wx.grid   
import getdata
db = getdata.Eb_db()
class MyFrame(wx.Frame):
 
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.grid_1 = wx.grid.Grid(self, -1, size=(1, 1))
        self.__do_layout()
        # end wxGlade   
    def __set_properties(self):   
       # begin wxGlade: MyFrame.__set_properties   
       self.SetTitle("frame_1")   
       self.SetSize((400, 400))   
      # end wxGlade
       self.grid_1.CreateGrid(len(db.data),len(db.fields))
 
    for item in db.fields:
        index = 0 
        self.grid_1.SetColLabelValue(index, item[0])
        index += 1
    for row in range(len(db.data)):
        for col in range(len(db.data[row])):
            values = db.data[row][col]
            self.grid_1.SetCellValue(row,col,str(values))
    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        sizer_1 =wx.BoxSizer(wx.VERTICAL)
        sizer_1.Add(self.grid_1, 1, wx.EXPAND, 0)
        self.SetAutoLayout(True)
        self.SetSizer(sizer_1)
        self.Layout()
        # end wxGlade
        # end of class MyFrame
if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_1 = MyFrame(None, -1, "")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()

got the example on this site: http://www.serpia.org/mysql. If you know any other tutorials i can try to view data from mysql database.

Don't wedge your for loops detween two function defines! Rewrite your code like this ...

import wx
import wx.grid
import getdata
 
db = getdata.Eb_db()
 
class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.grid_1 = wx.grid.Grid(self, -1, size=(1, 1))
        self.__do_layout()
        # end wxGlade
 
        for item in db.fields:
            index = 0
            self.grid_1.SetColLabelValue(index, item[0])
            index += 1
        for row in range(len(db.data)):
            for col in range(len(db.data[row])):
                values = db.data[row][col]
                self.grid_1.SetCellValue(row,col,str(values))
 
    def __set_properties(self):
       # begin wxGlade: MyFrame.__set_properties
       self.SetTitle("frame_1")
       self.SetSize((400, 400))
      # end wxGlade
       self.grid_1.CreateGrid(len(db.data),len(db.fields))
 
    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        sizer_1 =wx.BoxSizer(wx.VERTICAL)
        sizer_1.Add(self.grid_1, 1, wx.EXPAND, 0)
        self.SetAutoLayout(True)
        self.SetSizer(sizer_1)
        self.Layout()
        # end wxGlade
        # end of class MyFrame
 
if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_1 = MyFrame(None, -1, "")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()

The way you had it, Python looks at them as global to the class and self hasn't been declared yet. I don't have getdata, so I can't test it!

Thank you for the code it works. Well it displays the window but Its not getting the data from the getdata.py here is the code.For the getdata

import MySQLdb
 
class Eb_db:
    def __init__(self):
        try:
            connection = MySQLdb.connect(host="localhost",
            user="root", passwd="scott", db="phonebook" )
            cursor = connection.cursor()
            cursor.execute( "SELECT * FROM details " )
        except MySQLdb.OperationalError, message:
            errorMessage = "Error %d:\n%s" % (message[ 0 ], message[ 1 ] )
            return
        else:
            self.data = cursor.fetchall() 
            self.fields = cursor.description 
            cursor.close() 
            connection.close()
This article has been dead for over six months. Start a new discussion instead.