Data View

Reply

Join Date: Dec 2006
Posts: 25
Reputation: wandie is an unknown quantity at this point 
Solved Threads: 0
wandie wandie is offline Offline
Light Poster

Data View

 
0
  #1
Jul 17th, 2007
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
  1. Traceback (most recent call last):
  2. File "C:\Update\data.py", line 7, in <module>
  3. class MyFrame(wx.Frame):
  4. File "C:\Update\data.py", line 27, in MyFrame
  5. self.grid_1.SetColLabelValue(index, item[0])
  6. NameError: name 'self' is not defined

This is the code i put in.

  1. import wx
  2. import wx.grid
  3. import getdata
  4. db = getdata.Eb_db()
  5. class MyFrame(wx.Frame):
  6.  
  7. def __init__(self, *args, **kwds):
  8. # begin wxGlade: MyFrame.__init__
  9. kwds["style"] = wx.DEFAULT_FRAME_STYLE
  10. wx.Frame.__init__(self, *args, **kwds)
  11. self.grid_1 = wx.grid.Grid(self, -1, size=(1, 1))
  12. self.__do_layout()
  13. # end wxGlade
  14. def __set_properties(self):
  15. # begin wxGlade: MyFrame.__set_properties
  16. self.SetTitle("frame_1")
  17. self.SetSize((400, 400))
  18. # end wxGlade
  19. self.grid_1.CreateGrid(len(db.data),len(db.fields))
  20.  
  21. for item in db.fields:
  22. index = 0
  23. self.grid_1.SetColLabelValue(index, item[0])
  24. index += 1
  25. for row in range(len(db.data)):
  26. for col in range(len(db.data[row])):
  27. values = db.data[row][col]
  28. self.grid_1.SetCellValue(row,col,str(values))
  29. def __do_layout(self):
  30. # begin wxGlade: MyFrame.__do_layout
  31. sizer_1 =wx.BoxSizer(wx.VERTICAL)
  32. sizer_1.Add(self.grid_1, 1, wx.EXPAND, 0)
  33. self.SetAutoLayout(True)
  34. self.SetSizer(sizer_1)
  35. self.Layout()
  36. # end wxGlade
  37. # end of class MyFrame
  38. if __name__ == "__main__":
  39. app = wx.PySimpleApp(0)
  40. wx.InitAllImageHandlers()
  41. frame_1 = MyFrame(None, -1, "")
  42. app.SetTopWindow(frame_1)
  43. frame_1.Show()
  44. 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,009
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Data View

 
0
  #2
Jul 17th, 2007
Don't wedge your for loops detween two function defines! Rewrite your code like this ...
  1. import wx
  2. import wx.grid
  3. import getdata
  4.  
  5. db = getdata.Eb_db()
  6.  
  7. class MyFrame(wx.Frame):
  8. def __init__(self, *args, **kwds):
  9. # begin wxGlade: MyFrame.__init__
  10. kwds["style"] = wx.DEFAULT_FRAME_STYLE
  11. wx.Frame.__init__(self, *args, **kwds)
  12. self.grid_1 = wx.grid.Grid(self, -1, size=(1, 1))
  13. self.__do_layout()
  14. # end wxGlade
  15.  
  16. for item in db.fields:
  17. index = 0
  18. self.grid_1.SetColLabelValue(index, item[0])
  19. index += 1
  20. for row in range(len(db.data)):
  21. for col in range(len(db.data[row])):
  22. values = db.data[row][col]
  23. self.grid_1.SetCellValue(row,col,str(values))
  24.  
  25. def __set_properties(self):
  26. # begin wxGlade: MyFrame.__set_properties
  27. self.SetTitle("frame_1")
  28. self.SetSize((400, 400))
  29. # end wxGlade
  30. self.grid_1.CreateGrid(len(db.data),len(db.fields))
  31.  
  32. def __do_layout(self):
  33. # begin wxGlade: MyFrame.__do_layout
  34. sizer_1 =wx.BoxSizer(wx.VERTICAL)
  35. sizer_1.Add(self.grid_1, 1, wx.EXPAND, 0)
  36. self.SetAutoLayout(True)
  37. self.SetSizer(sizer_1)
  38. self.Layout()
  39. # end wxGlade
  40. # end of class MyFrame
  41.  
  42. if __name__ == "__main__":
  43. app = wx.PySimpleApp(0)
  44. wx.InitAllImageHandlers()
  45. frame_1 = MyFrame(None, -1, "")
  46. app.SetTopWindow(frame_1)
  47. frame_1.Show()
  48. 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!
Last edited by vegaseat; Jul 17th, 2007 at 5:00 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 25
Reputation: wandie is an unknown quantity at this point 
Solved Threads: 0
wandie wandie is offline Offline
Light Poster

Re: Data View

 
0
  #3
Jul 18th, 2007
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

  1. import MySQLdb
  2.  
  3. class Eb_db:
  4. def __init__(self):
  5. try:
  6. connection = MySQLdb.connect(host="localhost",
  7. user="root", passwd="scott", db="phonebook" )
  8. cursor = connection.cursor()
  9. cursor.execute( "SELECT * FROM details " )
  10. except MySQLdb.OperationalError, message:
  11. errorMessage = "Error %d:\n%s" % (message[ 0 ], message[ 1 ] )
  12. return
  13. else:
  14. self.data = cursor.fetchall()
  15. self.fields = cursor.description
  16. cursor.close()
  17. connection.close()
Last edited by vegaseat; Jul 18th, 2007 at 8:33 am. Reason: changed [\code] to [/code]
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,009
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Data View

 
0
  #4
Jul 18th, 2007
In your earlier code you don't seem to ever use method __set_properties()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC