Skip to content

Instantly share code, notes, and snippets.

@driscollis
Created November 16, 2017 14:47
Show Gist options
  • Save driscollis/969e58601d51e1907ec7832be576a3c5 to your computer and use it in GitHub Desktop.
Save driscollis/969e58601d51e1907ec7832be576a3c5 to your computer and use it in GitHub Desktop.
An ObjectListView demo with moveable items
import wx
from ObjectListView import ObjectListView, ColumnDefn
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
def __repr__(self):
return "<Book: {title}>".format(title=self.title)
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.current_selection = None
self.products = [Book("wxPython in Action", "Robin Dunn",
"1932394621", "Manning"),
Book("Hello World", "Warren and Carter Sande",
"1933988495", "Manning"),
Book("Core Python Programming", "Wesley Chun",
"0132269937", "Prentice Hall"),
Book("Python Programming for the Absolute Beginner",
"Michael Dawson", "1598631128",
"Course Technology"),
Book("Learning Python", "Mark Lutz",
"0596513984", "O'Reilly")
]
self.dataOlv = ObjectListView(self, wx.ID_ANY,
style=wx.LC_REPORT|wx.SUNKEN_BORDER)
self.setBooks()
# Allow the cell values to be edited when double-clicked
self.dataOlv.cellEditMode = ObjectListView.CELLEDIT_SINGLECLICK
# create up and down buttons
up_btn = wx.Button(self, wx.ID_ANY, "Up")
up_btn.Bind(wx.EVT_BUTTON, self.move_up)
down_btn = wx.Button(self, wx.ID_ANY, "Down")
down_btn.Bind(wx.EVT_BUTTON, self.move_down)
# Create some sizers
mainSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(self.dataOlv, 1, wx.ALL|wx.EXPAND, 5)
mainSizer.Add(up_btn, 0, wx.ALL|wx.CENTER, 5)
mainSizer.Add(down_btn, 0, wx.ALL|wx.CENTER, 5)
self.SetSizer(mainSizer)
def move_up(self, event):
"""
Move an item up the list
"""
self.current_selection = self.dataOlv.GetSelectedObject()
data = self.dataOlv.GetObjects()
if self.current_selection:
index = data.index(self.current_selection)
if index > 0:
new_index = index - 1
else:
new_index = len(data)-1
data.insert(new_index, data.pop(index))
self.products = data
self.setBooks()
self.dataOlv.Select(new_index)
def move_down(self, event):
"""
Move an item down the list
"""
self.current_selection = self.dataOlv.GetSelectedObject()
data = self.dataOlv.GetObjects()
if self.current_selection:
index = data.index(self.current_selection)
if index < len(data) - 1:
new_index = index + 1
else:
new_index = 0
data.insert(new_index, data.pop(index))
self.products = data
self.setBooks()
self.dataOlv.Select(new_index)
def setBooks(self):
self.dataOlv.SetColumns([
ColumnDefn("Title", "left", 220, "title"),
ColumnDefn("Author", "left", 200, "author"),
ColumnDefn("ISBN", "right", 100, "isbn"),
ColumnDefn("Mfg", "left", 180, "mfg")
])
self.dataOlv.SetObjects(self.products)
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)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment