How to set the header attributes (Phoenix)
Keywords : ListCtrl, Attributes, Header.
Demonstrating :
Tested py3.x, wx4.x and Win10.
Are you ready to use some samples ?
Test, modify, correct, complete, improve and share your discoveries !
Sample one
1 # sample_one.py
2
3 import wx
4
5 # class MyPanel
6 # class MyFrame
7 # class MyApp
8
9 #-------------------------------------------------------------------------------
10
11 class MyPanel(wx.Panel):
12 def __init__(self, parent):
13 super().__init__(parent)
14
15 # Define some rows of data
16 rows = [[355, 'Ted', 'Zingall'],
17 [1, 'ShipTo', 'Smith'],
18 [56, 'Henry', 'Jeff'],
19 [213, 'Antoine', 'Buzer'],
20 [20, 'Tony', 'Monza'],
21 [658, 'Robin', 'Hugh'],
22 [67, 'Ted', 'Jefferson'],
23 [153, 'Karol', 'Bertin'],
24 [6, 'Alice', 'Brice']]
25
26 #--------------
27
28 # Create a listCtrl
29 self.list = wx.ListCtrl(self, -1,
30 style = wx.LC_REPORT
31 | wx.LC_HRULES
32 | wx.LC_VRULES
33 | wx.LC_SINGLE_SEL)
34
35 self.list.AppendColumn('ID')
36 self.list.AppendColumn('First')
37 self.list.AppendColumn('Surname')
38
39 self.list.SetColumnWidth(0, 70)
40 self.list.SetColumnWidth(1, 70)
41 self.list.SetColumnWidth(2, 190)
42
43 self.list.SetForegroundColour(wx.BLACK)
44 self.list.SetBackgroundColour('#eceade')
45
46 #--------------
47
48 # Attributes (textcolor, backgroundcolor, font)
49 font_bold = wx.Font(wx.FontInfo(10).Bold())
50 head_txt_colr = wx.Colour('BLUE')
51 head_bac_colr = wx.Colour('DARK GREY')
52
53 # Currently it is implemented only for wxMSW and
54 # does nothing in the other ports
55 self.list.SetHeaderAttr(wx.ItemAttr(head_txt_colr,
56 head_bac_colr,
57 font_bold))
58
59 #--------------
60
61 for items in rows:
62 self.list.Append(items)
63
64 #--------------
65
66 layout = wx.BoxSizer(wx.VERTICAL)
67 layout.Add(self.list, 1, wx.EXPAND)
68 self.SetSizer(layout)
69 self.Layout()
70
71 #-------------------------------------------------------------------------------
72
73 class MyFrame(wx.Frame):
74 def __init__(self, parent, title=''):
75 super().__init__(parent, title=title)
76
77 self.SetIcon(wx.Icon('icons/wxwin.ico'))
78
79 self.SetSize((350, 240))
80
81 # Set the panel
82 self.panel = MyPanel(self)
83
84 #-------------------------------------------------------------------------------
85
86 class MyApp(wx.App):
87 def OnInit(self):
88
89 self.frame = MyFrame(None, title='wx.ListCtrl (SetHeaderAttr)')
90 self.frame.Show()
91
92 return True
93
94 #-------------------------------------------------------------------------------
95
96 if __name__ == "__main__":
97 app = MyApp(False)
98 app.MainLoop()
Sample two
1 # sample_two.py
2
3 import wx
4
5 # class MyPanel
6 # class MyFrame
7 # class MyApp
8
9 #-------------------------------------------------------------------------------
10
11 class MyPanel(wx.Panel):
12 def __init__(self, parent):
13 super().__init__(parent)
14
15 # Define some rows of data
16 data = [[1, 'Ford', 'Mustang'],
17 [2, 'Toyota', 'Yaris'],
18 [3, 'Renault', 'Clio'],
19 [4, 'Mercedes', 'Classe C'],
20 [5, 'Ferrari', '308'],
21 [6, 'Peugeot', '5008'],
22 [7, 'Bmw', 'Serie 2'],
23 [8, 'Fiat', 'Punto'],
24 [9, 'Nissan', 'Juke']]
25
26 #--------------
27
28 # Create a listCtrl
29 self.list = wx.ListCtrl(self, -1,
30 style = wx.LC_REPORT
31 | wx.LC_HRULES
32 | wx.LC_VRULES
33 | wx.LC_SINGLE_SEL)
34
35 self.list.AppendColumn('ID')
36 self.list.AppendColumn('Type')
37 self.list.AppendColumn('LastName')
38
39 self.list.SetBackgroundColour('#56f452')
40
41 #--------------
42
43 # Attributes (textcolor, backgroundcolor, font)
44 listfont = self.list.GetFont()
45 headfont = listfont.MakeBold()
46 headAttr = wx.ItemAttr((0,0,0), (240,240,240), headfont)
47
48 # Currently it is implemented only for wxMSW and
49 # does nothing in the other ports
50 self.list.SetHeaderAttr(headAttr)
51
52 #--------------
53
54 for items in data:
55 self.list.Append(items)
56
57 #--------------
58
59 layout = wx.BoxSizer(wx.VERTICAL)
60 layout.Add(self.list, 1, wx.EXPAND)
61 self.SetSizer(layout)
62 self.Layout()
63
64 #-------------------------------------------------------------------------------
65
66 class MyFrame(wx.Frame):
67 def __init__(self, parent, title=""):
68 super().__init__(parent, title=title)
69
70 self.SetIcon(wx.Icon('icons/wxwin.ico'))
71
72 self.SetSize((350, 240))
73
74 # Set the panel
75 self.panel = MyPanel(self)
76
77 #-------------------------------------------------------------------------------
78
79 class MyApp(wx.App):
80 def OnInit(self):
81
82 self.frame = MyFrame(None, title='wx.ListCtrl (SetHeaderAttr)')
83 self.frame.Show()
84 return True
85
86 #-------------------------------------------------------------------------------
87
88 if __name__ == "__main__":
89 app = MyApp(False)
90 app.MainLoop()
Download source
Additional Information
Link :
https://discuss.wxpython.org/t/listctrl-setheaderattr-method-seems-to-be-missing-in-4-1-1/35316
- - - - -
https://wiki.wxpython.org/TitleIndex
Thanks to
da-dada, the wxPython community...
About this page
Date(d/m/y) Person (bot) Comments :
01/05/21 - Ecco (Created page for wxPython Phoenix).
Comments
- blah, blah, blah....