Created
November 18, 2013 06:52
-
-
Save cwebber314/7523670 to your computer and use it in GitHub Desktop.
object list view demo with added image. I can't get the AddNamedImages() to work so I use the AddImages() method.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##################################################################### | |
# olvDemo.py | |
# | |
# Created 12/2009 by Mike Driscoll | |
# | |
# Based on example code from source and | |
# http://objectlistview.sourceforge.net/python/cellEditing.html | |
# | |
##################################################################### | |
import wx | |
from ObjectListView import ObjectListView, ColumnDefn | |
import images | |
import images2 | |
######################################################################## | |
class Book(object): | |
""" | |
Model of the Book object | |
Contains the following attributes: | |
'ISBN', 'Author', 'Manufacturer', 'Title' | |
""" | |
#---------------------------------------------------------------------- | |
def __init__(self, title, author, isbn, mfg): | |
self.isbn = isbn | |
self.author = author | |
self.mfg = mfg | |
self.title = title | |
######################################################################## | |
class MainPanel(wx.Panel): | |
#---------------------------------------------------------------------- | |
def __init__(self, parent): | |
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY) | |
self.products = [Book("wxPython in Action", "Robin Dunn", | |
"1932394621", "Manning"), | |
Book("Hello World", "Warren and Carter Sande", | |
"1933988495", "Manning") | |
] | |
self.dataOlv = ObjectListView(self, wx.ID_ANY, style=wx.LC_REPORT|wx.SUNKEN_BORDER) | |
self.foo = self.dataOlv.AddImages(images.bullet_green.GetBitmap(), 0) | |
self.setBooks() | |
# Allow the cell values to be edited when double-clicked | |
self.dataOlv.cellEditMode = ObjectListView.CELLEDIT_SINGLECLICK | |
# create an update button | |
updateBtn = wx.Button(self, wx.ID_ANY, "Update OLV") | |
updateBtn.Bind(wx.EVT_BUTTON, self.updateControl) | |
# create an info button | |
infoBtn = wx.Button(self, label="Get Info") | |
infoBtn.Bind(wx.EVT_BUTTON, self.getRowInfo) | |
# Create some sizers | |
mainSizer = wx.BoxSizer(wx.VERTICAL) | |
mainSizer.Add(self.dataOlv, 1, wx.ALL|wx.EXPAND, 5) | |
mainSizer.Add(updateBtn, 0, wx.ALL|wx.CENTER, 5) | |
mainSizer.Add(infoBtn, 0, wx.ALL|wx.CENTER, 5) | |
self.SetSizer(mainSizer) | |
#---------------------------------------------------------------------- | |
def getStatusImage(self, item): | |
return self.foo | |
def updateControl(self, event): | |
""" | |
""" | |
print "updating..." | |
product_dict = [{"title":"Core Python Programming", "author":"Wesley Chun", | |
"isbn":"0132269937", "mfg":"Prentice Hall"}, | |
{"title":"Python Programming for the Absolute Beginner", | |
"author":"Michael Dawson", "isbn":"1598631128", | |
"mfg":"Course Technology"}, | |
{"title":"Learning Python", "author":"Mark Lutz", | |
"isbn":"0596513984", "mfg":"O'Reilly"} | |
] | |
#self.setBooks(product_dict) | |
data = self.products + product_dict | |
self.dataOlv.SetObjects(data) | |
#---------------------------------------------------------------------- | |
def setBooks(self, data=None): | |
self.dataOlv.SetColumns([ | |
ColumnDefn("Title", "left", 220, "title"), | |
ColumnDefn("", "left", 120, "status", imageGetter=self.getStatusImage, fixedWidth=16), | |
ColumnDefn("Author", "left", 200, "author"), | |
ColumnDefn("ISBN", "right", 100, "isbn"), | |
ColumnDefn("Mfg", "left", 180, "mfg") | |
]) | |
self.dataOlv.SetObjects(self.products) | |
#---------------------------------------------------------------------- | |
def getRowInfo(self, event): | |
"""""" | |
rowObj = self.dataOlv.GetSelectedObject() | |
print rowObj.author | |
print rowObj.title | |
######################################################################## | |
class MainFrame(wx.Frame): | |
#---------------------------------------------------------------------- | |
def __init__(self): | |
wx.Frame.__init__(self, parent=None, id=wx.ID_ANY, | |
title="ObjectListView Demo", size=(800,600)) | |
panel = MainPanel(self) | |
######################################################################## | |
class GenApp(wx.App): | |
#---------------------------------------------------------------------- | |
def __init__(self, redirect=False, filename=None): | |
wx.App.__init__(self, redirect, filename) | |
#---------------------------------------------------------------------- | |
def OnInit(self): | |
# create frame here | |
frame = MainFrame() | |
frame.Show() | |
return True | |
#---------------------------------------------------------------------- | |
def main(): | |
""" | |
Run the demo | |
""" | |
app = GenApp() | |
app.MainLoop() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment