2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

wxPythonのListCtrlのヘッダーの色を変える方法

2
Posted at

wxPythonで黒ベースのGUIにするのが目的。リストコントロールのヘッダーの色を変更します。wx.ListCtrlはヘッダーの色を変更することができませんでした。wx.Gridはヘッダーの色を変更することができるのですが、白い3Dの枠線を変更することができませんでした。そして、UltimateListCtrlを使えばできることが分かりました。

main.py
import wx
from wx.lib.agw import ultimatelistctrl as ULC

# ヘッダーレンダラー
class UltimateHeaderRenderer(object):
    def __init__(self, parent):
        super().__init__()

    def DrawHeaderButton(self, dc, rect, flags):
        # ヘッダカラー
        color = wx.Colour(40,40,200)
        dc.SetBrush(wx.Brush(color, wx.BRUSHSTYLE_SOLID))
        dc.SetBackgroundMode(wx.SOLID)
        dc.SetPen(wx.TRANSPARENT_PEN)

        # 仕切り線を消すため広げる
        rect.x -= 1
        rect.width += 1
        dc.DrawRectangle(rect)
        
        # 仕切り線
        dc.SetPen(wx.Pen('#FF0000',1))
        dc.DrawLine(rect.x, rect.y, rect.x, rect.y+rect.height)
        
        dc.SetPen(wx.TRANSPARENT_PEN)
        dc.SetBackgroundMode(wx.TRANSPARENT)  

    def GetForegroundColour(self):
        return wx.Colour(255,255,255)

# リストコントロール
class UltimateListCtrl(ULC.UltimateListCtrl):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, wx.ID_ANY, agwStyle=wx.LC_REPORT, style=wx.BORDER_NONE)
        self.SetBackgroundColour('#444444') 
        self.SetForegroundColour('#FFFFFF') 

    def InsertColumn(self, col, text, width):
        info = ULC.UltimateListItem()
        info._mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_FORMAT
        info._format = 0
        info._text = text
    
        # レンダラー 
        klass = UltimateHeaderRenderer(self)
        info.SetCustomRenderer(klass) 
        
        self.InsertColumnInfo(col, info)
        self.SetColumnWidth(col, width)

class MainFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(400,200))
        p = wx.Panel(self, -1)
        
        self.list = UltimateListCtrl(p)
        self.list.InsertColumn(0, text='Path', width=180)
        self.list.InsertColumn(1, text='Date', width=100)  
        self.list.InsertColumn(2, text='Size', width=-3) #-3=FULL  
        
        self.list.InsertStringItem(0, 'AAAA')
        self.list.SetStringItem(0, 1, '2018/1/1')
        self.list.SetStringItem(0, 2, '1000')

        self.list.InsertStringItem(1, 'BBBB')
        self.list.SetStringItem(1, 1, '2018/2/2')
        self.list.SetStringItem(1, 2, '2000')

        sizer = wx.FlexGridSizer(rows=1, cols=1, vgap=1, hgap=1)
        sizer.Add(self.list, 0, wx.EXPAND, 0)
        sizer.AddGrowableCol(0)
        sizer.AddGrowableRow(0)
        p.SetSizer(sizer)
       
if __name__ == '__main__':
    app = wx.App(False)
    main = MainFrame(None, -1, 'UltimateListCtrl')
    main.Show(True)
    app.MainLoop()           
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?