wxPython - ListCrtl and SQLite3
- by Dunwitch
I'm trying to get a SQLite3 DB to populate a wx.ListCrtl. I can get it to print to stdout/stderr without any problem. I just can't seem to figure out how to display the data in the DataWindow/DataList? I'm sure I've made some code mistakes, so any help is appreciated.
Main.py
import wx
import wx.lib.mixins.listctrl as listmix
from database import *
import sys
class DataWindow(wx.Frame):
def __init__(self, parent = None):
wx.Frame.__init__(self, parent, -1, 'DataList', size=(640,480))
self.win = DataList(self)
self.Center()
self.Show(True)
class DataList(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.ColumnSorterMixin):
def __init__(self, parent = DataWindow):
wx.ListCtrl.__init__( self, parent, -1, style=wx.LC_REPORT|wx.LC_VIRTUAL|wx.LC_HRULES|wx.LC_VRULES)
#building the columns
self.InsertColumn(0, "Location")
self.InsertColumn(1, "Address")
self.InsertColumn(2, "Subnet")
self.InsertColumn(3, "Gateway")
self.SetColumnWidth(0, 100)
self.SetColumnWidth(1, 150)
self.SetColumnWidth(2, 150)
self.SetColumnWidth(3, 150)
class MainWindow(wx.Frame):
def __init__(self, parent = None, id = -1, title = "MainWindow"):
wx.Frame.__init__(self, parent, id, title, size = (800,600),
style = wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER))
# StatusBar
self.CreateStatusBar()
# Filemenu
filemenu = wx.Menu()
# Filemenu - About
menuitem = filemenu.Append(-1, "&About", "Information about this application")
self.Bind(wx.EVT_MENU, self.onAbout, menuitem)
#Filemenu - Data
menuitem = filemenu.Append(-1, "&Data", "Get data")
self.Bind(wx.EVT_MENU, self.onData, menuitem)
# Filemenu - Seperator
filemenu.AppendSeparator()
#Filemenu - Exit
menuitem = filemenu.Append(-1, "&Exit", "Exit the application")
self.Bind(wx.EVT_MENU, self.onExit, menuitem)
# Menubar
menubar = wx.MenuBar()
menubar.Append(filemenu, "&File")
self.SetMenuBar(menubar)
# Show
self.Show(True)
self.Center()
def onAbout(self, event):
pass
def onData(self, event):
DataWindow(self)
callDb = Database()
sql = "SELECT rowid, address, subnet, gateway FROM pod1"
records = callDb.select(sql)
for v in records:
print "How do I get the records on the DataList?"
#print "%s%s%s" % (v[1],v[2],v[3])
#for v in records:
#DataList.InsertStringItem("%s") % (v[0], v[1], v[2])
def onExit(self, event):
self.Close()
self.Destroy()
def onSave(self, event):
pass
if __name__ == '__main__':
app = wx.App()
frame = MainWindow(None, -1)
frame.Show()
app.MainLoop()
database.py
import os
import sqlite3
class Database(object):
def __init__(self, db_file="data/data.sqlite"):
database_allready_exists = os.path.exists(db_file)
self.db = sqlite3.connect(db_file)
if not database_allready_exists:
self.setupDefaultData()
def select(self,sql):
cursor = self.db.cursor()
cursor.execute(sql)
records = cursor.fetchall()
cursor.close
return records
def insert(self,sql):
newID = 0
cursor = self.db.cursor()
cursor.execute(sql)
newID = cursor.lastrowid
self.db.commit()
cursor.close()
return newID
def save(self,sql):
cursor = self.db.cursor()
cursor.execute(sql)
self.db.commit()
cursor.close()
def setupDefaultData(self):
pass