开发者

wxpython: How to fill the inside of a checkbox in a listctrl object with a given rgb color?

I have this chart. In this chart I have a bunch of countries. I am trying to create the legends inside a listctrl object by changing the color of a non-functional checkbox.

Is there any wxpython function to change such attribute?

开发者_运维百科

Thanks


I don't think CheckBox will work for that. Changing the background color will have a different effect on different platforms, and I'm not sure any of them change only the box part of the widget. ListCtrl is also pretty limited.

If there's nothing built-in to do the job, you could try something like this using ScrolledPanel and StaticText:

import wx.lib.scrolledpanel as sp

SAMPLE_DATA = [('Antarctica',   'Green'),
               ('Afghanistan',  'Maroon'),
               ('Belguim',      'Blue'),
               ('Canada',       'Red'),
               ('India',        'Sea Green'),
               ('Mexico',       'Grey'),
               ('Mongolia',     'Black'),
               ('New Zealand',  'Orange'),
               ('Turkey',       'Purple'),]

class Legend(sp.ScrolledPanel):
    def __init__(self, parent, id, data=SAMPLE_DATA):
        sp.ScrolledPanel.__init__(self, parent, id)

        self.sizer = wx.BoxSizer(wx.VERTICAL)

        for d in data:
            item  = d[0]
            color = d[1]

            linesizer = wx.BoxSizer(wx.HORIZONTAL)
            box = wx.StaticText(self, wx.ID_ANY, ' ', size=(10,10))
            box.SetBackgroundColour(color)
            text = wx.StaticText(self, wx.ID_ANY, item)
            linesizer.Add(box,  0, flag=wx.EXPAND|wx.ALL, border=2)
            linesizer.Add(text, 1, flag=wx.EXPAND)
            self.sizer.Add(linesizer, 0, wx.EXPAND)

        self.SetSizer(self.sizer)
        self.sizer.Fit(self)
        self.SetupScrolling(scroll_y=True)


Take a look at wxRendererNative() in the wx demo, you should be able to use a DC in an OnPaint event and repalce the rendered images with one of your choice, in this case a different coloured checkbox. You would need to make the images yourself or figure out a way to mask the default ones with the colour of your choice (never tried this myself), but it should be doable one way or another.

you can also check out: http://wiki.wxpython.org/CreatingCustomControls for an example of a custom checkbox control. how this will apply within a list, I am not sure. but it might give you an idea of what needs to be done to accomplish a custom checkbox.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜