Tuesday, April 29, 2025
HomePythonANN: ObjectListView3 for wxPython - Mouse Vs Python

ANN: ObjectListView3 for wxPython – Mouse Vs Python


ObjectListView is a third-party wxPython widget that wraps the wx.ListCtrl. I’ve used it for over 10 years in fairly a number of completely different GUI purposes as a result of it really works a lot nicer than wx.ListCtrl does. Sadly, ObjectListView was by no means built-in into wxPython core like another superb third-party packages have been, and so it has change into damaged over the previous couple of years such you can’t use it with the newest model of Python / wxPython.

So, I made a decision to fork the challenge, as its defects have been easy sufficient that I may resolve them rapidly. The brand new model of ObjectListView is now known as ObjectListView3.

You may get the brand new model of ObjectListview right here:

Be aware that this model of ObjectListView works with Python 3.11+ and wxPython 4.2+. It could work with earlier variations as nicely.

Set up

When you’d like to put in ObjectListView3, you need to use Python’s pip to take action like this:

python -m pip set up ObjectListView3

Now let’s see how an instance of utilizing ObjectListView!

Pattern Utilization

The next is an software that begins out by itemizing two books in an ObjectListView3 widget. While you press the “Replace OLV” button, it’ll add three extra books to the ObjectListView3 widget.

This demonstrates the best way to create the ObjectListview3 widget and replace it after the applying is operating:

import wx
from ObjectListView3 import ObjectListView, ColumnDefn


class Ebook(object):
    """
    Mannequin of the Ebook object

    Incorporates the next attributes:
    'ISBN', 'Creator', 'Producer', 'Title'
    """

    def __init__(self, title, writer, isbn, mfg):
        self.isbn = isbn
        self.writer = writer
        self.mfg = mfg
        self.title = title


class MainPanel(wx.Panel):
    def __init__(self, dad or mum):
        tremendous().__init__(dad or mum=dad or mum, id=wx.ID_ANY)
        self.merchandise = [
            Book("wxPython in Action", "Robin Dunn", "1932394621", "Manning"),
            Book("Hello World", "Warren and Carter Sande", "1933988495", "Manning"),
        ]

        self.dataOlv = ObjectListView(
            self, wx.ID_ANY, model=wx.LC_REPORT | wx.SUNKEN_BORDER
        )
        self.setBooks()

        # Permit the cell values to be edited when double-clicked
        self.dataOlv.cellEditMode = ObjectListView.CELLEDIT_SINGLECLICK

        # create an replace button
        updateBtn = wx.Button(self, wx.ID_ANY, "Replace OLV")
        updateBtn.Bind(wx.EVT_BUTTON, self.updateControl)

        # 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)
        self.SetSizer(mainSizer)

    def updateControl(self, occasion):
        """
        Replace the ObjectListView
        """
        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",
            },
        ]
        information = self.merchandise + product_dict
        self.dataOlv.SetObjects(information)

    def setBooks(self, information=None):
        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.merchandise)


class MainFrame(wx.Body):
    def __init__(self):
        wx.Body.__init__(
            self,
            dad or mum=None,
            id=wx.ID_ANY,
            title="ObjectListView Demo",
            measurement=(800, 600),
        )
        panel = MainPanel(self)


class OLVDemoApp(wx.App):
    def __init__(self, redirect=False, filename=None):
        tremendous().__init__(redirect, filename)

    def OnInit(self):
        # create body right here
        body = MainFrame()
        body.Present()
        return True


def primary():
    """
    Run the demo
    """
    app = OLVDemoApp()
    app.MainLoop()


if __name__ == "__main__":
    primary()

While you initially run this code, you will note the next software:

While you press the “Replace OLV” button, the applying will replace to appear like the next:

Updated ObjectListView3 Demo app

Wrapping Up

The ObjectListView3 widget makes working with tabular information straightforward in wxPython. You additionally get good modifying, sorting and extra from this widget through completely different mixins or types. Give ObjectListView3 a strive once you create your subsequent wxPython GUI software.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments