DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   Data View (http://www.daniweb.com/forums/thread83719.html)

wandie Jul 17th, 2007 6:27 am
Data View
 
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.

vegaseat Jul 17th, 2007 4:56 pm
Re: Data View
 
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!

wandie Jul 18th, 2007 4:03 am
Re: Data View
 
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()

vegaseat Jul 18th, 2007 8:41 am
Re: Data View
 
In your earlier code you don't seem to ever use method __set_properties()


All times are GMT -4. The time now is 1:27 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC