DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Advancing Robot Vision and Control
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  • Using Python Libraries in Java

Trending

  • Code Reviews: Building an AI-Powered GitHub Integration
  • Using Java Stream Gatherers To Improve Stateful Operations
  • Chat With Your Knowledge Base: A Hands-On Java and LangChain4j Guide
  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  1. DZone
  2. Data Engineering
  3. Databases
  4. wxPython: Changing Custom Renderers for Columns/Rows

wxPython: Changing Custom Renderers for Columns/Rows

Here's a quick snippet to help show how to apply custom renderers in wxPython for columns and rows!

By 
Mike Driscoll user avatar
Mike Driscoll
·
Jan. 08, 19 · Code Snippet
Likes (3)
Comment
Save
Tweet
Share
4.8K Views

Join the DZone community and get the full member experience.

Join For Free

The wxPython GUI toolkit has a very rich and powerful Grid widget that I have written about previously on this blog. It allows you to create sheets of cells similar to those in Microsoft Excel.

There is also a neat Mixin that allows you to apply a custom renderer to the labels on the columns and rows of the grid.

Let's take a look at that and see how it works:

import wx
import wx.grid as grid
import wx.lib.mixins.gridlabelrenderer as glr

class MyGrid(grid.Grid, glr.GridWithLabelRenderersMixin):

    def __init__(self, *args, **kw):
        grid.Grid.__init__(self, *args, **kw)
        glr.GridWithLabelRenderersMixin.__init__(self)

class MyColLabelRenderer(glr.GridLabelRenderer):

    def __init__(self, bgcolor):
        self._bgcolor = bgcolor

    def Draw(self, grid, dc, rect, col):
        dc.SetBrush(wx.Brush(self._bgcolor))
        dc.SetPen(wx.TRANSPARENT_PEN)
        dc.DrawRectangle(rect)
        hAlign, vAlign = grid.GetColLabelAlignment()
        text = grid.GetColLabelValue(col)
        self.DrawBorder(grid, dc, rect)
        self.DrawText(grid, dc, rect, text, hAlign, vAlign)

class MyPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        grid = MyGrid(self, size=(100, 100))
        grid.CreateGrid(numRows=10, numCols=10)

        for col in range(0, 10, 3):
            grid.SetColLabelRenderer(
                col+0, MyColLabelRenderer('#e0ffe0'))
            grid.SetColLabelRenderer(
                col+1, MyColLabelRenderer('#e0e0ff'))
            grid.SetColLabelRenderer(
                col+2, MyColLabelRenderer('#ffe0e0'))

        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(grid, 1, wx.EXPAND)
        self.SetSizer(main_sizer)

class MyFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title='Custom Grid Renderers')
        panel = MyPanel(self)
        self.Show()

if __name__ == '__main__':
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()


Let's break this down a bit. You will notice at the top of the code that we need to import the Grid widget separately in wxPython. We also need to import a mixin called GridWithLabelRenderersMixin . We subclass the Grid class and add in the mixin and then initialize both.

Next we create a subclass of GridLabelRenderer , which is also from the mixin. This allows us to create a spacing Draw  method that will give us the ability to apply different colors or fonts to the labels in our Grid. In this case, I just made it so that we could change the color of the text in the labels.

The last piece of code that we are interested in is in the MyPanel  class where we actually instantiate the Grid and change the color of the background of the labels in the columns. Here is what the grid ended up looking like:

Custom Grid Column Renderers

Wrapping Up

The wxPython toolkit has dozens of pre-built widgets that you can use to create cross-platform user interfaces. The wxPython demo has a much more involved example than this article does that you might also find interesting. If you haven't given wxPython a try, you really should go get it. It is pip installable from PyPI and compatible with Python 3.

Label Cross platform Column (database) Python (language) Row (database) Interface (computing) Blog

Published at DZone with permission of Mike Driscoll, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Advancing Robot Vision and Control
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  • Using Python Libraries in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!