wxPython - ListCrtl和SQLite3

时间:2010-03-29 09:01:57

标签: wxpython

我正在尝试使用SQLite3数据库来填充wx.ListCrtl。我可以毫无问题地将它打印到stdout / stderr。我似乎无法弄清楚如何在DataWindow / DataList中显示数据?我确信我已经犯了一些代码错误,所以感谢任何帮助。

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

1 个答案:

答案 0 :(得分:1)

我是这样做的。我甚至不确定为什么,我只是从演示中复制代码(大致),文档不是很清楚:

    count = 0
    for v in records:
        index = DataList.InsertStringItem(sys.maxint, str(count + 1))

        DataList.SetStringItem(index, 0, v['rowid'])
        DataList.SetStringItem(index, 1, v['address'])
        count += 1

该数字是对用于设置字符串的列的引用。真的不确定指数,说实话