未加星标

wxPython: Changing Custom Renderers for Columns / Rows

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二04 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

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:


wxPython: Changing Custom Renderers for Columns / Rows
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.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

分页:12
转载请注明
本文标题:wxPython: Changing Custom Renderers for Columns / Rows
本站链接:https://www.codesec.net/view/628127.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(39)