944,033 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 2397
  • Python RSS
Jul 17th, 2007
0

Data View

Expand Post »
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
Python Syntax (Toggle Plain Text)
  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.

Python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
wandie is offline Offline
25 posts
since Dec 2006
Jul 17th, 2007
0

Re: Data View

Don't wedge your for loops detween two function defines! Rewrite your code like this ...
python Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jul 18th, 2007
0

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

python Syntax (Toggle Plain Text)
  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]
Reputation Points: 10
Solved Threads: 0
Light Poster
wandie is offline Offline
25 posts
since Dec 2006
Jul 18th, 2007
0

Re: Data View

In your earlier code you don't seem to ever use method __set_properties()
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Paste a image into another
Next Thread in Python Forum Timeline: If stdin is empty?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC