| | |
Data View
![]() |
•
•
Join Date: Dec 2006
Posts: 25
Reputation:
Solved Threads: 0
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
This is the code i put in.
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.
Python Syntax (Toggle Plain Text)
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.
Python Syntax (Toggle Plain Text)
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 ...
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!
python Syntax (Toggle Plain Text)
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()
Last edited by vegaseat; Jul 17th, 2007 at 5:00 pm.
May 'the Google' be with you!
•
•
Join Date: Dec 2006
Posts: 25
Reputation:
Solved Threads: 0
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)
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()
Last edited by vegaseat; Jul 18th, 2007 at 8:33 am. Reason: changed [\code] to [/code]
![]() |
Similar Threads
- How can I refresh DataBase without closing Form (Visual Basic 4 / 5 / 6)
- refresh datagrid causes error if oper scrolled off right side of grid (VB.NET)
- How can I refresh DataBase without closing Form Attached with my Coding (Visual Basic 4 / 5 / 6)
- Make your own javascript maze. (JavaScript / DHTML / AJAX)
- Need help pls (Database Design)
- Transfer from *regular* "phpbb 2.0.11" to "phpbb XTREME 1.7" possible? (Growing an Online Community)
Other Threads in the Python Forum
- Previous Thread: Paste a image into another
- Next Thread: If stdin is empty?
| Thread Tools | Search this Thread |
alarm app assignment avogadro beginner bluetooth character cipher cmd code customdialog cx-freeze data decimals dictionary directory dynamic error examples exe file float format function generator getvalue gnu graphics gui halp homework http ideas images import input ip itunes java keycontrol leftmouse line linux list lists loop maintain maze millimeter module mouse number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh string strings sudokusolver terminal text threading time tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo vigenere web webservice wikipedia wxpython xlib






