How to create a list control (Phoenix)
Keywords : ListCtrl, Select, Insert, Update, Delete, Sort, Highlighted row, Icon, Bitmap, ListCtrlAutoWidthMixin, ListRowHighlighter, ColumnSorterMixin, wx.TheClipboard.
Contents
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 os
4 import sys
5 import wx
6
7 # class MyFrame
8 # class MyListCtrl
9 # class MyApp
10
11 #---------------------------------------------------------------------------
12
13 data = [
14 ['Noel', 'Louise'],
15 ['Martin', 'Justine'],
16 ['Antoine', 'Eloise'],
17 ['Jenifer', 'Marguerite'],
18 ['Marc', 'Sophie'],
19 ['Etienne', 'Edith'],
20 ['Jhon', 'Doe']
21 ]
22
23 #---------------------------------------------------------------------------
24
25 class MyFrame(wx.Frame):
26 def __init__(self, title):
27 wx.Frame.__init__(self, None, -1,
28 title,
29 size=(420, 200),
30 pos=(400, 448))
31
32 #------------
33
34 # Return icons folder.
35 self.icons_dir = wx.GetApp().GetIconsDir()
36
37 #------------
38
39 # Simplified init method.
40 self.SetProperties()
41 self.CreateCtrls()
42
43 #-----------------------------------------------------------------------
44
45 def SetProperties(self):
46 """
47 ...
48 """
49
50 self.SetMinSize((420, 200))
51
52 #------------
53
54 frameIcon = wx.Icon(os.path.join(self.icons_dir,
55 "wxwin.ico"),
56 type=wx.BITMAP_TYPE_ICO)
57 self.SetIcon(frameIcon)
58
59
60 def CreateCtrls(self):
61 """
62 ...
63 """
64
65 self.listCtrl = MyListCtrl(self)
66
67 #---------------------------------------------------------------------------
68
69 class MyListCtrl(wx.ListCtrl):
70 def __init__(self, parent):
71 wx.ListCtrl.__init__(self, parent, -1,
72 style=wx.LC_REPORT |
73 wx.LC_HRULES |
74 wx.LC_VRULES)
75
76 #------------
77
78 self.SetBackgroundColour("white")
79
80 #------------
81
82 # Add some columns.
83 columns = ["Firstname", "Surname"]
84
85 for col, text in enumerate(columns):
86 self.InsertColumn(col, text)
87
88 #------------
89
90 # Set the width of the columns.
91 self.SetColumnWidth(0, 100) # (0, -2)
92 self.SetColumnWidth(1, 300) # (1, -2)
93
94 #------------
95
96 # Add the rows.
97 for item in data:
98 index = self.InsertItem(self.GetItemCount(), item[0])
99 for col, text in enumerate(item[1:]):
100 self.SetItem(index, col+1, text)
101
102 #------------
103
104 self.items = data
105 print("Items :", self.items)
106
107 #---------------------------------------------------------------------------
108
109 class MyApp(wx.App):
110 def OnInit(self):
111
112 #------------
113
114 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
115
116 #------------
117
118 frame = MyFrame("Sample one")
119 self.SetTopWindow(frame)
120 frame.Show(True)
121
122 return True
123
124 #-----------------------------------------------------------------------
125
126 def GetInstallDir(self):
127 """
128 Return the installation directory for my application.
129 """
130
131 return self.installDir
132
133
134 def GetIconsDir(self):
135 """
136 Return the icons directory for my application.
137 """
138
139 icons_dir = os.path.join(self.installDir, "icons")
140 return icons_dir
141
142 #---------------------------------------------------------------------------
143
144 def main():
145 app = MyApp(False)
146 app.MainLoop()
147
148 #---------------------------------------------------------------------------
149
150 if __name__ == "__main__" :
151 main()
Sample two
1 # sample_two.py
2
3 import os
4 import sys
5 import wx
6 import wx.lib.mixins.listctrl as listmix
7
8 # class MyFrame
9 # class MyListCtrl
10 # class MyApp
11
12 #---------------------------------------------------------------------------
13
14 data = [
15 ['Noel', 'Louise'],
16 ['Martin', 'Justine'],
17 ['Antoine', 'Eloise'],
18 ['Jenifer', 'Marguerite'],
19 ['Marc', 'Sophie'],
20 ['Etienne', 'Edith'],
21 ['Jhon', 'Doe']
22 ]
23
24 #---------------------------------------------------------------------------
25
26 class MyFrame(wx.Frame):
27 def __init__(self, title):
28 wx.Frame.__init__(self, None, -1,
29 title,
30 size=(420, 200),
31 pos=(400, 448))
32
33 #------------
34
35 # Return icons folder.
36 self.icons_dir = wx.GetApp().GetIconsDir()
37
38 #------------
39
40 # Simplified init method.
41 self.SetProperties()
42 self.CreateCtrls()
43
44 #-----------------------------------------------------------------------
45
46 def SetProperties(self):
47 """
48 ...
49 """
50
51 self.SetMinSize((420, 200))
52
53 #------------
54
55 frameIcon = wx.Icon(os.path.join(self.icons_dir,
56 "wxwin.ico"),
57 type=wx.BITMAP_TYPE_ICO)
58 self.SetIcon(frameIcon)
59
60
61 def CreateCtrls(self):
62 """
63 ...
64 """
65
66 self.listCtrl = MyListCtrl(self)
67
68 #---------------------------------------------------------------------------
69
70 class MyListCtrl(wx.ListCtrl,
71 listmix.ListCtrlAutoWidthMixin):
72 def __init__(self, parent):
73 wx.ListCtrl.__init__(self, parent, -1,
74 style=wx.LC_REPORT |
75 wx.LC_HRULES |
76 wx.LC_VRULES)
77
78 #------------
79
80 # Initialize the listCtrl auto width.
81 listmix.ListCtrlAutoWidthMixin.__init__(self)
82
83 #------------
84
85 self.SetBackgroundColour("#d4f9d7")
86
87 #------------
88
89 # Add some columns.
90 columns = ["Firstname", "Surname"]
91
92 for col, text in enumerate(columns):
93 self.InsertColumn(col, text)
94
95 #------------
96
97 # Set the width of the columns.
98 self.SetColumnWidth(0, 100) # (0, -2)
99 self.SetColumnWidth(1, 300) # (1, -2)
100
101 #------------
102
103 # Add the rows.
104 for item in data:
105 index = self.InsertItem(self.GetItemCount(), item[0])
106 for col, text in enumerate(item[1:]):
107 self.SetItem(index, col+1, text)
108
109 #------------
110
111 self.items = data
112 print("Items :", self.items)
113
114 #---------------------------------------------------------------------------
115
116 class MyApp(wx.App):
117 def OnInit(self):
118
119 #------------
120
121 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
122
123 #------------
124
125 frame = MyFrame("Sample two (ListCtrlAutoWidthMixin)")
126 self.SetTopWindow(frame)
127 frame.Show(True)
128
129 return True
130
131 #-----------------------------------------------------------------------
132
133 def GetInstallDir(self):
134 """
135 Return the installation directory for my application.
136 """
137
138 return self.installDir
139
140
141 def GetIconsDir(self):
142 """
143 Return the icons directory for my application.
144 """
145
146 icons_dir = os.path.join(self.installDir, "icons")
147 return icons_dir
148
149 #---------------------------------------------------------------------------
150
151 def main():
152 app = MyApp(False)
153 app.MainLoop()
154
155 #---------------------------------------------------------------------------
156
157 if __name__ == "__main__" :
158 main()
Sample three
1 # sample_three.py
2
3 import os
4 import sys
5 import wx
6 import wx.lib.mixins.listctrl as listmix
7
8 # class MyFrame
9 # class MyListCtrl
10 # class MyApp
11
12 #---------------------------------------------------------------------------
13
14 data = [
15 ['Noel', 'Louise'],
16 ['Martin', 'Justine'],
17 ['Antoine', 'Eloise'],
18 ['Jenifer', 'Marguerite'],
19 ['Marc', 'Sophie'],
20 ['Etienne', 'Edith'],
21 ['Jhon', 'Doe']
22 ]
23
24 #---------------------------------------------------------------------------
25
26 class MyFrame(wx.Frame):
27 def __init__(self, title):
28 wx.Frame.__init__(self, None, -1,
29 title,
30 size=(420, 200),
31 pos=(400, 448))
32
33 #------------
34
35 # Return icons folder.
36 self.icons_dir = wx.GetApp().GetIconsDir()
37
38 #------------
39
40 # Simplified init method.
41 self.SetProperties()
42 self.CreateCtrls()
43
44 #-----------------------------------------------------------------------
45
46 def SetProperties(self):
47 """
48 ...
49 """
50
51 self.SetMinSize((420, 200))
52
53 #------------
54
55 frameIcon = wx.Icon(os.path.join(self.icons_dir,
56 "wxwin.ico"),
57 type=wx.BITMAP_TYPE_ICO)
58 self.SetIcon(frameIcon)
59
60
61 def CreateCtrls(self):
62 """
63 ...
64 """
65
66 self.listCtrl = MyListCtrl(self)
67
68 #---------------------------------------------------------------------------
69
70 class MyListCtrl(wx.ListCtrl,
71 listmix.ListCtrlAutoWidthMixin,
72 listmix.ColumnSorterMixin):
73 def __init__(self, parent):
74 wx.ListCtrl.__init__(self, parent, -1,
75 style=wx.LC_REPORT |
76 wx.LC_HRULES |
77 wx.LC_VRULES)
78
79 #------------
80
81 # Initialize the listCtrl auto width.
82 listmix.ListCtrlAutoWidthMixin.__init__(self)
83
84 #------------
85
86 self.SetBackgroundColour("#ecf3fd")
87
88 #------------
89
90 # Add some columns.
91 columns = ["Firstname", "Surname"]
92
93 for col, text in enumerate(columns):
94 self.InsertColumn(col, text)
95
96 #------------
97
98 # Set the width of the columns.
99 self.SetColumnWidth(0, 100) # (0, -2)
100 self.SetColumnWidth(1, 300) # (1, -2)
101
102 #------------
103
104 # Add the rows.
105 self.itemDataMap = {}
106
107 for item in data:
108 index = self.InsertItem(self.GetItemCount(), item[0])
109 for col, text in enumerate(item[1:]):
110 self.SetItem(index, col+1, text)
111
112 # Give each item a data value, and map it back
113 # to the item values, for the column sorter.
114 self.SetItemData(index, index)
115 self.itemDataMap[index] = item
116
117 #------------
118
119 # Initialize the column sorter.
120 listmix.ColumnSorterMixin.__init__(self, self.GetItemCount())
121
122 #------------
123
124 self.items = data
125 print("Items :", self.items)
126
127 #-----------------------------------------------------------------------
128
129 def GetListCtrl(self):
130 """
131 ...
132 """
133
134 return self
135
136 #---------------------------------------------------------------------------
137
138 class MyApp(wx.App):
139 def OnInit(self):
140
141 #------------
142
143 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
144
145 #------------
146
147 frame = MyFrame("Sample three (sort list)")
148 self.SetTopWindow(frame)
149 frame.Show(True)
150
151 return True
152
153 #-----------------------------------------------------------------------
154
155 def GetInstallDir(self):
156 """
157 Return the installation directory for my application.
158 """
159
160 return self.installDir
161
162
163 def GetIconsDir(self):
164 """
165 Return the icons directory for my application.
166 """
167
168 icons_dir = os.path.join(self.installDir, "icons")
169 return icons_dir
170
171 #---------------------------------------------------------------------------
172
173 def main():
174 app = MyApp(False)
175 app.MainLoop()
176
177 #---------------------------------------------------------------------------
178
179 if __name__ == "__main__" :
180 main()
Sample four
1 # sample_four.py
2
3 import os
4 import sys
5 import wx
6 import wx.lib.mixins.listctrl as listmix
7
8 # class MyFrame
9 # class MyListCtrl
10 # class MyApp
11
12 #---------------------------------------------------------------------------
13
14 data = [
15 ['Noel', 'Louise'],
16 ['Martin', 'Justine'],
17 ['Antoine', 'Eloise'],
18 ['Jenifer', 'Marguerite'],
19 ['Marc', 'Sophie'],
20 ['Etienne', 'Edith'],
21 ['Jhon', 'Doe']
22 ]
23
24 #---------------------------------------------------------------------------
25
26 class MyFrame(wx.Frame):
27 def __init__(self, title):
28 wx.Frame.__init__(self, None, -1,
29 title,
30 size=(420, 200),
31 pos=(400, 448))
32
33 #------------
34
35 # Return icons folder.
36 self.icons_dir = wx.GetApp().GetIconsDir()
37
38 #------------
39
40 # Simplified init method.
41 self.SetProperties()
42 self.CreateCtrls()
43
44 #-----------------------------------------------------------------------
45
46 def SetProperties(self):
47 """
48 ...
49 """
50
51 self.SetMinSize((420, 200))
52
53 #------------
54
55 frameIcon = wx.Icon(os.path.join(self.icons_dir,
56 "wxwin.ico"),
57 type=wx.BITMAP_TYPE_ICO)
58 self.SetIcon(frameIcon)
59
60
61 def CreateCtrls(self):
62 """
63 ...
64 """
65
66 self.listCtrl = MyListCtrl(self)
67
68 #---------------------------------------------------------------------------
69
70 class MyListCtrl(wx.ListCtrl,
71 listmix.ListRowHighlighter,
72 listmix.ListCtrlAutoWidthMixin,
73 listmix.ColumnSorterMixin):
74 def __init__(self, parent):
75 wx.ListCtrl.__init__(self, parent, -1,
76 style=wx.LC_REPORT |
77 wx.LC_HRULES |
78 wx.LC_VRULES)
79
80 #------------
81
82 # Initialize the listCtrl row highlighter.
83 listmix.ListRowHighlighter.__init__(self)
84
85 # Initialize the listCtrl auto width.
86 listmix.ListCtrlAutoWidthMixin.__init__(self)
87
88 #------------
89
90 self.SetBackgroundColour("#e3e3e3")
91
92 #------------
93
94 # Add some columns.
95 columns = ["Firstname", "Surname"]
96
97 for col, text in enumerate(columns):
98 self.InsertColumn(col, text)
99
100 #------------
101
102 # Set the width of the columns.
103 self.SetColumnWidth(0, 100) # (0, -2)
104 self.SetColumnWidth(1, 300) # (1, -2)
105
106 #------------
107
108 # Add the rows.
109 self.itemDataMap = {}
110
111 for item in data:
112 index = self.InsertItem(self.GetItemCount(), item[0])
113 for col, text in enumerate(item[1:]):
114 self.SetItem(index, col+1, text)
115
116 # Give each item a data value, and map it back
117 # to the item values, for the column sorter.
118 self.SetItemData(index, index)
119 self.itemDataMap[index] = item
120
121 #------------
122
123 # Initialize the column sorter.
124 listmix.ColumnSorterMixin.__init__(self, self.GetItemCount())
125
126 # Column sorted by default.
127 self.SortListItems(1, True)
128
129 #------------
130
131 self.items = data
132 print("Items :", self.items)
133
134 #-----------------------------------------------------------------------
135
136 def GetListCtrl(self):
137 """
138 ...
139 """
140
141 return self
142
143 #---------------------------------------------------------------------------
144
145 class MyApp(wx.App):
146 def OnInit(self):
147
148 #------------
149
150 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
151
152 #------------
153
154 frame = MyFrame("Sample four (ListRowHighlighter)")
155 self.SetTopWindow(frame)
156 frame.Show(True)
157
158 return True
159
160 #-----------------------------------------------------------------------
161
162 def GetInstallDir(self):
163 """
164 Return the installation directory for my application.
165 """
166
167 return self.installDir
168
169
170 def GetIconsDir(self):
171 """
172 Return the icons directory for my application.
173 """
174
175 icons_dir = os.path.join(self.installDir, "icons")
176 return icons_dir
177
178 #---------------------------------------------------------------------------
179
180 def main():
181 app = MyApp(False)
182 app.MainLoop()
183
184 #---------------------------------------------------------------------------
185
186 if __name__ == "__main__" :
187 main()
Sample five
1 # sample_five.py
2
3 import os
4 import sys
5 import wx
6 import wx.lib.mixins.listctrl as listmix
7
8 # class MyFrame
9 # class MyListCtrl
10 # class MyApp
11
12 #---------------------------------------------------------------------------
13
14 data = [
15 ['Noel', 'Louise'],
16 ['Martin', 'Justine'],
17 ['Antoine', 'Eloise'],
18 ['Jenifer', 'Marguerite'],
19 ['Marc', 'Sophie'],
20 ['Etienne', 'Edith'],
21 ['Jhon', 'Doe']
22 ]
23
24 #---------------------------------------------------------------------------
25
26 class MyFrame(wx.Frame):
27 def __init__(self, title):
28 wx.Frame.__init__(self, None, -1,
29 title,
30 size=(420, 200),
31 pos=(400, 448))
32
33 #------------
34
35 # Return icons folder.
36 self.icons_dir = wx.GetApp().GetIconsDir()
37
38 #------------
39
40 # Simplified init method.
41 self.SetProperties()
42 self.CreateCtrls()
43
44 #-----------------------------------------------------------------------
45
46 def SetProperties(self):
47 """
48 ...
49 """
50
51 self.SetMinSize((420, 200))
52
53 #------------
54
55 frameIcon = wx.Icon(os.path.join(self.icons_dir,
56 "wxwin.ico"),
57 type=wx.BITMAP_TYPE_ICO)
58 self.SetIcon(frameIcon)
59
60
61 def CreateCtrls(self):
62 """
63 ...
64 """
65
66 self.listCtrl = MyListCtrl(self)
67
68 #---------------------------------------------------------------------------
69
70 class MyListCtrl(wx.ListCtrl,
71 listmix.ColumnSorterMixin,
72 listmix.ListCtrlAutoWidthMixin):
73 def __init__(self, parent):
74 wx.ListCtrl.__init__(self, parent, -1,
75 style=wx.LC_REPORT |
76 wx.LC_HRULES |
77 wx.LC_VRULES)
78
79 #------------
80
81 # Initialize the listCtrl auto width.
82 listmix.ListCtrlAutoWidthMixin.__init__(self)
83
84 #------------
85
86 self.Bind(wx.EVT_LIST_COL_CLICK, self.OnSort)
87
88 #------------
89
90 self.SetBackgroundColour("#ece9d8")
91
92 #------------
93
94 # Add some columns.
95 columns = ["Firstname", "Surname"]
96
97 for col, text in enumerate(columns):
98 self.InsertColumn(col, text)
99
100 #------------
101
102 # Set the width of the columns.
103 self.SetColumnWidth(0, 100) # (0, -2)
104 self.SetColumnWidth(1, 300) # (1, -2)
105
106 #------------
107
108 # Add the rows.
109 self.itemDataMap = {}
110
111 for item in data:
112 index = self.InsertItem(self.GetItemCount(), item[0])
113 for col, text in enumerate(item[1:]):
114 self.SetItem(index, col+1, text)
115
116 # Give each item a data value, and map it back
117 # to the item values, for the column sorter.
118 self.SetItemData(index, index)
119 self.itemDataMap[index] = item
120
121 # Alternate the row colors of a ListCtrl.
122 if self.GetItemCount() > 0:
123 for x in range(self.GetItemCount()):
124 if x % 2 == 0:
125 self.SetItemBackgroundColour(x, "#ece9d8")
126 else:
127 self.SetItemBackgroundColour(x, "#ffffff")
128
129 #------------
130
131 # Initialize the column sorter.
132 listmix.ColumnSorterMixin.__init__(self, self.GetItemCount())
133
134 # Column sorted by default.
135 self.SortListItems(1, True)
136
137 #------------
138
139 self.items = data
140 print("Items :", self.items)
141
142 #-----------------------------------------------------------------------
143
144 def GetListCtrl(self):
145 """
146 ...
147 """
148
149 return self
150
151
152 def OnSort(self, event):
153 """
154 ...
155 """
156
157 print("\ndef OnSort")
158
159 #------------
160
161 idx = event.GetColumn()
162 print("... Column :", idx)
163
164 #------------
165
166 # Alternate the row colors of a ListCtrl.
167 if self.GetItemCount() > 0:
168 for x in range(self.GetItemCount()):
169 if x % 2 == 0:
170 self.SetItemBackgroundColour(x, "#ece9d8")
171 else:
172 self.SetItemBackgroundColour(x, "#ffffff")
173
174 #---------------------------------------------------------------------------
175
176 class MyApp(wx.App):
177 def OnInit(self):
178
179 #------------
180
181 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
182
183 #------------
184
185 frame = MyFrame("Sample five (highlighted row)")
186 self.SetTopWindow(frame)
187 frame.Show(True)
188
189 return True
190
191 #-----------------------------------------------------------------------
192
193 def GetInstallDir(self):
194 """
195 Return the installation directory for my application.
196 """
197
198 return self.installDir
199
200
201 def GetIconsDir(self):
202 """
203 Return the icons directory for my application.
204 """
205
206 icons_dir = os.path.join(self.installDir, "icons")
207 return icons_dir
208
209 #---------------------------------------------------------------------------
210
211 def main():
212 app = MyApp(False)
213 app.MainLoop()
214
215 #---------------------------------------------------------------------------
216
217 if __name__ == "__main__" :
218 main()
Sample six
1 # sample_six.py
2
3 import os
4 import sys
5 import wx
6 import wx.lib.mixins.listctrl as listmix
7
8 # class MyFrame
9 # class MyListCtrl
10 # class MyApp
11
12 #---------------------------------------------------------------------------
13
14 data = [
15 ['Noel', 'Louise'],
16 ['Martin', 'Justine'],
17 ['Antoine', 'Eloise'],
18 ['Jenifer', 'Marguerite'],
19 ['Marc', 'Sophie'],
20 ['Etienne', 'Edith'],
21 ['Jhon', 'Doe']
22 ]
23
24 #---------------------------------------------------------------------------
25
26 class MyFrame(wx.Frame):
27 def __init__(self, title):
28 wx.Frame.__init__(self, None, -1,
29 title,
30 size=(420, 225),
31 pos=(400, 448))
32
33 #------------
34
35 # Return icons folder.
36 self.icons_dir = wx.GetApp().GetIconsDir()
37
38 #------------
39
40 # Simplified init method.
41 self.SetProperties()
42 self.CreateCtrls()
43
44 #-----------------------------------------------------------------------
45
46 def SetProperties(self):
47 """
48 ...
49 """
50
51 self.SetMinSize((420, 225))
52
53 #------------
54
55 frameIcon = wx.Icon(os.path.join(self.icons_dir,
56 "wxwin.ico"),
57 type=wx.BITMAP_TYPE_ICO)
58 self.SetIcon(frameIcon)
59
60
61 def CreateCtrls(self):
62 """
63 ...
64 """
65
66 self.btnCopy = wx.Button(self, -1, "Copy")
67 self.Bind(wx.EVT_BUTTON, self.OnBtnCopy, self.btnCopy)
68
69 self.listCtrl = MyListCtrl(self)
70
71 #------------
72
73 mainSizer = wx.BoxSizer(wx.VERTICAL)
74 btnSizer = wx.BoxSizer(wx.HORIZONTAL)
75
76 btnSizer.Add(self.btnCopy, 1)
77
78 mainSizer.Add(btnSizer, 0, wx.EXPAND)
79 mainSizer.Add(self.listCtrl, 1, wx.EXPAND)
80
81 self.SetSizer(mainSizer)
82
83
84 def OnBtnCopy(self, event):
85 """
86 ...
87 """
88
89 if not wx.TheClipboard.Open():
90 wx.MessageBox("Unable to copy to clipboard !")
91 return
92
93 text = ""
94 for i in range(self.listCtrl.GetItemCount()):
95 item = self.listCtrl.GetItem(i)
96 if item.GetState() & wx.LIST_STATE_SELECTED:
97 d = self.listCtrl.items[i]
98 text += str(d[0])+' '+str(d[1])+'\r\n'
99 print("Copy to wx.TheClipboard :", text)
100
101 do = wx.TextDataObject(text)
102 wx.TheClipboard.SetData(do)
103 wx.TheClipboard.Close()
104
105 #---------------------------------------------------------------------------
106
107 class MyListCtrl(wx.ListCtrl,
108 listmix.ListCtrlAutoWidthMixin):
109 def __init__(self, parent):
110 wx.ListCtrl.__init__(self, parent, -1,
111 style=wx.LC_REPORT |
112 wx.LC_HRULES |
113 wx.LC_VRULES)
114
115 #------------
116
117 # Initialize the listCtrl auto width.
118 listmix.ListCtrlAutoWidthMixin.__init__(self)
119
120 #------------
121
122 self.SetBackgroundColour("#d1ff00")
123
124 #------------
125
126
127 # Add some columns.
128 columns = ["Firstname", "Surname"]
129
130 for col, text in enumerate(columns):
131 self.InsertColumn(col, text)
132
133 #------------
134
135 # Set the width of the columns.
136 self.SetColumnWidth(0, 100) # (0, -2)
137 self.SetColumnWidth(1, 300) # (1, -2)
138
139 #------------
140
141 # Add the rows.
142 for item in data:
143 index = self.InsertItem(self.GetItemCount(), item[0])
144 for col, text in enumerate(item[1:]):
145 self.SetItem(index, col+1, text)
146
147 #------------
148
149 self.items = data
150 print("Items :", self.items)
151
152 #-----------------------------------------------------------------------
153
154 def GetListCtrl(self):
155 """
156 ...
157 """
158
159 return self
160
161 #---------------------------------------------------------------------------
162
163 class MyApp(wx.App):
164 def OnInit(self):
165
166 #------------
167
168 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
169
170 #------------
171
172 frame = MyFrame("Sample six (copy to wx.Clipboard)")
173 self.SetTopWindow(frame)
174 frame.Show(True)
175
176 return True
177
178 #-----------------------------------------------------------------------
179
180 def GetInstallDir(self):
181 """
182 Return the installation directory for my application.
183 """
184
185 return self.installDir
186
187
188 def GetIconsDir(self):
189 """
190 Return the icons directory for my application.
191 """
192
193 icons_dir = os.path.join(self.installDir, "icons")
194 return icons_dir
195
196 #---------------------------------------------------------------------------
197
198 def main():
199 app = MyApp(False)
200 app.MainLoop()
201
202 #---------------------------------------------------------------------------
203
204 if __name__ == "__main__" :
205 main()
Sample seven
1 # sample_seven.py
2
3 import os
4 import sys
5 import wx
6 import wx.lib.mixins.listctrl as listmix
7
8 # class MyFrame
9 # class MyListCtrl
10 # class MyApp
11
12 #---------------------------------------------------------------------------
13
14 data = [
15 ['Noel', 'Louise'],
16 ['Martin', 'Justine'],
17 ['Antoine', 'Eloise'],
18 ['Jenifer', 'Marguerite'],
19 ['Marc', 'Sophie'],
20 ['Etienne', 'Edith'],
21 ['Jhon', 'Doe']
22 ]
23
24 #---------------------------------------------------------------------------
25
26 class MyFrame(wx.Frame):
27 def __init__(self, title):
28 wx.Frame.__init__(self, None, -1,
29 title,
30 size=(420, 200),
31 pos=(400, 448))
32
33 #------------
34
35 # Return icons folder.
36 self.icons_dir = wx.GetApp().GetIconsDir()
37
38 #------------
39
40 # Simplified init method.
41 self.SetProperties()
42 self.CreateCtrls()
43
44 #-----------------------------------------------------------------------
45
46 def SetProperties(self):
47 """
48 ...
49 """
50
51 self.SetMinSize((420, 200))
52
53 #------------
54
55 frameIcon = wx.Icon(os.path.join(self.icons_dir,
56 "wxwin.ico"),
57 type=wx.BITMAP_TYPE_ICO)
58 self.SetIcon(frameIcon)
59
60
61 def CreateCtrls(self):
62 """
63 ...
64 """
65
66 self.listCtrl = MyListCtrl(self)
67
68 #---------------------------------------------------------------------------
69
70 class MyListCtrl(wx.ListCtrl,
71 listmix.ListCtrlAutoWidthMixin,
72 listmix.ColumnSorterMixin):
73 def __init__(self, parent):
74 wx.ListCtrl.__init__(self, parent, -1,
75 style=wx.LC_REPORT |
76 wx.LC_HRULES |
77 wx.LC_VRULES)
78
79 #------------
80
81 # Initialize the listCtrl auto width.
82 listmix.ListCtrlAutoWidthMixin.__init__(self)
83
84 #------------
85
86 # Return bitmaps folder.
87 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
88
89 #------------
90
91 self.SetBackgroundColour("#fff757")
92
93 #------------
94
95 # Load some images into an image list.
96 il = wx.ImageList(16, 16, True)
97
98 # Add row icon.
99 self.bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
100 "folder.png"))
101
102 # Add image to list.
103 self.idx = il.Add(self.bmp)
104
105 # Assign the image list to it.
106 self.AssignImageList(il, wx.IMAGE_LIST_SMALL)
107
108 #------------
109
110 # Add some columns.
111 columns = ["Firstname", "Surname"]
112
113 for col, text in enumerate(columns):
114 self.InsertColumn(col, text)
115
116 #------------
117
118 # Set the width of the columns.
119 self.SetColumnWidth(0, 100) # (0, -2)
120 self.SetColumnWidth(1, 300) # (1, -2)
121
122 #------------
123
124 # Add the rows.
125 self.itemDataMap = {}
126
127 for item in data:
128 index = self.InsertItem(self.GetItemCount(), item[0], self.idx)
129 for col, text in enumerate(item[1:]):
130 self.SetItem(index, col+1, text)
131
132 # Give each item a data value, and map it back
133 # to the item values, for the column sorter.
134 self.SetItemData(index, index)
135 self.itemDataMap[index] = item
136
137 # Give each item a image.
138 self.SetItemImage(index, self.idx)
139
140 #------------
141
142 # Initialize the column sorter.
143 listmix.ColumnSorterMixin.__init__(self, self.GetItemCount())
144
145 # Column sorted by default.
146 self.SortListItems(0, True)
147
148 #------------
149
150 self.items = data
151 print("Items :", self.items)
152
153 #-----------------------------------------------------------------------
154
155 def GetListCtrl(self):
156 """
157 ...
158 """
159
160 return self
161
162 #---------------------------------------------------------------------------
163
164 class MyApp(wx.App):
165 def OnInit(self):
166
167 #------------
168
169 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
170
171 #------------
172
173 frame = MyFrame("Sample seven (row icon)")
174 self.SetTopWindow(frame)
175 frame.Show(True)
176
177 return True
178
179 #-----------------------------------------------------------------------
180
181 def GetInstallDir(self):
182 """
183 Return the installation directory for my application.
184 """
185
186 return self.installDir
187
188
189 def GetIconsDir(self):
190 """
191 Return the icons directory for my application.
192 """
193
194 icons_dir = os.path.join(self.installDir, "icons")
195 return icons_dir
196
197
198 def GetBitmapsDir(self):
199 """
200 Return the bitmaps directory for my application.
201 """
202
203 bitmaps_dir = os.path.join(self.installDir, "bitmaps")
204 return bitmaps_dir
205
206 #---------------------------------------------------------------------------
207
208 def main():
209 app = MyApp(False)
210 app.MainLoop()
211
212 #---------------------------------------------------------------------------
213
214 if __name__ == "__main__" :
215 main()
Sample eight
1 # sample_eight.py
2
3 import os
4 import sys
5 import wx
6 import wx.lib.mixins.listctrl as listmix
7
8 # class MyFrame
9 # class MyListCtrl
10 # class MyApp
11
12 #---------------------------------------------------------------------------
13
14 data = [
15 ['Noel', 'Louise'],
16 ['Martin', 'Justine'],
17 ['Antoine', 'Eloise'],
18 ['Jenifer', 'Marguerite'],
19 ['Marc', 'Sophie'],
20 ['Etienne', 'Edith'],
21 ['Jhon', 'Doe']
22 ]
23
24 #---------------------------------------------------------------------------
25
26 class MyFrame(wx.Frame):
27 def __init__(self, title):
28 wx.Frame.__init__(self, None, -1,
29 title,
30 size=(420, 200),
31 pos=(400, 448))
32
33 #------------
34
35 # Return icons folder.
36 self.icons_dir = wx.GetApp().GetIconsDir()
37
38 #------------
39
40 # Simplified init method.
41 self.SetProperties()
42 self.CreateCtrls()
43
44 #-----------------------------------------------------------------------
45
46 def SetProperties(self):
47 """
48 ...
49 """
50
51 self.SetMinSize((420, 200))
52
53 #------------
54
55 frameIcon = wx.Icon(os.path.join(self.icons_dir,
56 "wxwin.ico"),
57 type=wx.BITMAP_TYPE_ICO)
58 self.SetIcon(frameIcon)
59
60
61 def CreateCtrls(self):
62 """
63 ...
64 """
65
66 self.listCtrl = MyListCtrl(self)
67
68 #---------------------------------------------------------------------------
69
70 class MyListCtrl(wx.ListCtrl,
71 listmix.ListCtrlAutoWidthMixin,
72 listmix.ColumnSorterMixin):
73 def __init__(self, parent):
74 wx.ListCtrl.__init__(self, parent, -1,
75 style=wx.LC_REPORT |
76 wx.LC_HRULES |
77 wx.LC_VRULES)
78
79 #------------
80
81 # Initialize the listCtrl auto width.
82 listmix.ListCtrlAutoWidthMixin.__init__(self)
83
84 #------------
85
86 # Return bitmaps folder.
87 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
88
89 #------------
90
91 self.SetBackgroundColour("#ffbb55")
92
93 #------------
94
95 # Load some images into an image list.
96 il = wx.ImageList(16, 16, True)
97
98 # Add row icon.
99 self.bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
100 "folder.png"))
101
102 # Add some arrows for the column sorter.
103 self.up = wx.Bitmap(os.path.join(self.bitmaps_dir,
104 "sm_up.png"))
105
106 self.dn = wx.Bitmap(os.path.join(self.bitmaps_dir,
107 "sm_down.png"))
108
109 # Add image to list.
110 self.idx = il.Add(self.bmp)
111 self.sm_up = il.Add(self.up)
112 self.sm_dn = il.Add(self.dn)
113
114 # Assign the image list to it.
115 self.AssignImageList(il, wx.IMAGE_LIST_SMALL)
116
117 #------------
118
119 # Add some columns.
120 columns = ["Firstname", "Surname"]
121
122 for col, text in enumerate(columns):
123 self.InsertColumn(col, text)
124
125 #------------
126
127 # Set the width of the columns.
128 self.SetColumnWidth(0, 100) # (0, -2)
129 self.SetColumnWidth(1, 300) # (1, -2)
130
131 #------------
132
133 # Add the rows.
134 self.itemDataMap = {}
135
136 for item in data:
137 index = self.InsertItem(self.GetItemCount(), item[0], self.idx)
138 for col, text in enumerate(item[1:]):
139 self.SetItem(index, col+1, text)
140
141 # Give each item a data value, and map it back
142 # to the item values, for the column sorter.
143 self.SetItemData(index, index)
144 self.itemDataMap[index] = item
145
146 # Give each item a image.
147 self.SetItemImage(index, self.idx)
148
149 #------------
150
151 # Initialize the column sorter.
152 listmix.ColumnSorterMixin.__init__(self, self.GetItemCount())
153
154 # Column sorted by default.
155 self.SortListItems(0, True)
156
157 #------------
158
159 self.items = data
160 print("Items :", self.items)
161
162 #-----------------------------------------------------------------------
163
164 def GetListCtrl(self):
165 """
166 ...
167 """
168
169 return self
170
171
172 def GetSortImages(self):
173 """
174 ...
175 """
176
177 return (self.sm_dn, self.sm_up)
178
179 #---------------------------------------------------------------------------
180
181 class MyApp(wx.App):
182 def OnInit(self):
183
184 #------------
185
186 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
187
188 #------------
189
190 frame = MyFrame("Sample eight (row/header icon)")
191 self.SetTopWindow(frame)
192 frame.Show(True)
193
194 return True
195
196 #-----------------------------------------------------------------------
197
198 def GetInstallDir(self):
199 """
200 Return the installation directory for my application.
201 """
202
203 return self.installDir
204
205
206 def GetIconsDir(self):
207 """
208 Return the icons directory for my application.
209 """
210
211 icons_dir = os.path.join(self.installDir, "icons")
212 return icons_dir
213
214
215 def GetBitmapsDir(self):
216 """
217 Return the bitmaps directory for my application.
218 """
219
220 bitmaps_dir = os.path.join(self.installDir, "bitmaps")
221 return bitmaps_dir
222
223 #---------------------------------------------------------------------------
224
225 def main():
226 app = MyApp(False)
227 app.MainLoop()
228
229 #---------------------------------------------------------------------------
230
231 if __name__ == "__main__" :
232 main()
Sample nine
1 # sample_nine.py
2
3 import os
4 import sys
5 import wx
6 import wx.lib.mixins.listctrl as listmix
7
8 # class MyFrame
9 # class MyListCtrl
10 # class MyApp
11
12 #---------------------------------------------------------------------------
13
14 data = [
15 ['Noel', 'Louise'],
16 ['Martin', 'Justine'],
17 ['Antoine', 'Eloise'],
18 ['Jenifer', 'Marguerite'],
19 ['Marc', 'Sophie'],
20 ['Etienne', 'Edith'],
21 ['Jhon', 'Doe']
22 ]
23
24 #---------------------------------------------------------------------------
25
26 class MyFrame(wx.Frame):
27 def __init__(self, title):
28 wx.Frame.__init__(self, None, -1,
29 title,
30 size=(420, 200),
31 pos=(400, 448))
32
33 #------------
34
35 # Return icons folder.
36 self.icons_dir = wx.GetApp().GetIconsDir()
37
38 #------------
39
40 # Simplified init method.
41 self.SetProperties()
42 self.CreateCtrls()
43
44 #-----------------------------------------------------------------------
45
46 def SetProperties(self):
47 """
48 ...
49 """
50
51 self.SetMinSize((420, 200))
52
53 #------------
54
55 frameIcon = wx.Icon(os.path.join(self.icons_dir,
56 "wxwin.ico"),
57 type=wx.BITMAP_TYPE_ICO)
58 self.SetIcon(frameIcon)
59
60
61 def CreateCtrls(self):
62 """
63 ...
64 """
65
66 self.listCtrl = MyListCtrl(self)
67
68 #---------------------------------------------------------------------------
69
70 class MyListCtrl(wx.ListCtrl,
71 listmix.ListCtrlAutoWidthMixin,
72 listmix.ColumnSorterMixin):
73 def __init__(self, parent):
74 wx.ListCtrl.__init__(self, parent, -1,
75 style=wx.LC_REPORT |
76 wx.LC_HRULES |
77 wx.LC_VRULES)
78
79 #------------
80
81 # Initialize the listCtrl auto width.
82 listmix.ListCtrlAutoWidthMixin.__init__(self)
83
84 #------------
85
86 # Return bitmaps folder.
87 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
88
89 #------------
90
91 self.SetBackgroundColour("#ffe8ff")
92
93 #------------
94
95 # Load some images into an image list.
96 il = wx.ImageList(16, 16, True)
97
98 # Add row icon.
99 self.bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
100 "folder.png"))
101
102 # Add some arrows for the column sorter.
103 self.up = wx.Bitmap(os.path.join(self.bitmaps_dir,
104 "sm_up.png"))
105
106 self.dn = wx.Bitmap(os.path.join(self.bitmaps_dir,
107 "sm_down.png"))
108
109 # Add image to list.
110 self.idx = il.Add(self.bmp)
111 self.sm_up = il.Add(self.up)
112 self.sm_dn = il.Add(self.dn)
113
114 # Assign the image list to it.
115 self.AssignImageList(il, wx.IMAGE_LIST_SMALL)
116
117 #------------
118
119 # Add some columns.
120 columns = ["Firstname", "Surname"]
121
122 for col, text in enumerate(columns):
123 self.InsertColumn(col, text)
124
125 #------------
126
127 # Set the width of the columns.
128 self.SetColumnWidth(0, 100) # (0, -2)
129 self.SetColumnWidth(1, 300) # (1, -2)
130
131 #------------
132
133 # Add the rows.
134 self.itemDataMap = {}
135
136 for item in data:
137 index = self.InsertItem(self.GetItemCount(), item[0], self.idx)
138 for col, text in enumerate(item[1:]):
139 self.SetItem(index, col+1, text)
140
141 # Give each item a data value, and map it back
142 # to the item values, for the column sorter.
143 self.SetItemData(index, index)
144 self.itemDataMap[index] = item
145
146 # Give each item a image.
147 self.SetItemImage(index, self.idx)
148
149 #------------
150
151 # Initialize the column sorter.
152 listmix.ColumnSorterMixin.__init__(self, self.GetItemCount())
153
154 # Column sorted by default.
155 self.SortListItems(0, True)
156
157 #------------
158
159 self.items = data
160 print("Items :", self.items)
161
162 #------------
163
164 # For wxMSW.
165 self.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnRightClick)
166
167 # For wxGTK.
168 self.Bind(wx.EVT_RIGHT_UP, self.OnRightClick)
169
170 #-----------------------------------------------------------------------
171
172 def OnRightClick(self, event):
173 """
174 ...
175 """
176
177 # Only do this part the first time
178 # so the events are only bound once.
179 if not hasattr(self, "popupID1"):
180 self.popupID1 = wx.NewIdRef()
181 self.popupID2 = wx.NewIdRef()
182 self.popupID3 = wx.NewIdRef()
183
184 self.Bind(wx.EVT_MENU, self.OnPopupOne, id=self.popupID1)
185 self.Bind(wx.EVT_MENU, self.OnPopupTwo, id=self.popupID2)
186 self.Bind(wx.EVT_MENU, self.OnPopupThree, id=self.popupID3)
187
188 #------------
189
190 # Make a menu.
191 menu = wx.Menu()
192
193 # Add some items.
194 menu.Append(self.popupID1, "First message box")
195 menu.Append(self.popupID2, "Second message box")
196 menu.Append(self.popupID3, "Delete all items")
197
198 # Popup the menu. If an item is selected then its
199 # handler will be called before PopupMenu returns.
200 self.PopupMenu(menu)
201 menu.Destroy()
202
203
204 def OnPopupOne(self, event):
205 """
206 ...
207 """
208
209 dlg = wx.MessageDialog(self,
210 "Hello from Python and wxPython !",
211 "First message box",
212 wx.OK | wx.ICON_INFORMATION)
213 dlg.ShowModal()
214 dlg.Destroy()
215
216
217 def OnPopupTwo(self, event):
218 """
219 ...
220 """
221
222 dlg = wx.MessageDialog(self,
223 "Welcome !",
224 "Second message box",
225 wx.OK | wx.ICON_INFORMATION)
226 dlg.ShowModal()
227 dlg.Destroy()
228
229
230 def OnPopupThree(self, event):
231 """
232 ...
233 """
234
235 self.DeleteAllItems()
236
237
238 def GetListCtrl(self):
239 """
240 ...
241 """
242
243 return self
244
245
246 def GetSortImages(self):
247 """
248 ...
249 """
250
251 return (self.sm_dn, self.sm_up)
252
253 #---------------------------------------------------------------------------
254
255 class MyApp(wx.App):
256 def OnInit(self):
257
258 #------------
259
260 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
261
262 #------------
263
264 frame = MyFrame("Sample nine (popup menu)")
265 self.SetTopWindow(frame)
266 frame.Show(True)
267
268 return True
269
270 #-----------------------------------------------------------------------
271
272 def GetInstallDir(self):
273 """
274 Return the installation directory for my application.
275 """
276
277 return self.installDir
278
279
280 def GetIconsDir(self):
281 """
282 Return the icons directory for my application.
283 """
284
285 icons_dir = os.path.join(self.installDir, "icons")
286 return icons_dir
287
288
289 def GetBitmapsDir(self):
290 """
291 Return the bitmaps directory for my application.
292 """
293
294 bitmaps_dir = os.path.join(self.installDir, "bitmaps")
295 return bitmaps_dir
296
297 #---------------------------------------------------------------------------
298
299 def main():
300 app = MyApp(False)
301 app.MainLoop()
302
303 #---------------------------------------------------------------------------
304
305 if __name__ == "__main__" :
306 main()
Sample ten
1 # sample_ten.py
2
3 """
4
5 Author : Chris Meyers
6 Modified and updated for wxPython Phoenix by Ecco
7 Date (d/m/y) : 2003-2015 (v0.0.1)
8 08/02/2020 (v0.0.2)
9 License : http://www.gnu.org/licenses/copyleft.html
10 Link : http://openbookproject.net/py4fun/gui/wxPhone.html
11 Version : 0.0.2
12
13 """
14
15 import os
16 import sys
17 import wx
18 import wx.lib.mixins.listctrl as listmix
19
20 # class MyAddDlg
21 # class MyFrame
22 # class MyApp
23
24 #-------------------------------------------------------------------------------
25
26 phoneList = [
27 ("Muggs - J. Fred", "683-2341"),
28 ("Andrea - James", "862-8654"),
29 ("Tell - Guillaume", "235-3254"),
30 ("Teddy - Mink", "458-7943"),
31 ("Marc - Dyos", "973-5463"),
32 ("Henry - Mask", "162-1023"),
33 ("John - Lenon", "284-6354"),
34 ("Andy - Smith", "462-1596"),
35 ]
36
37 phoneList.sort()
38
39 #-------------------------------------------------------------------------------
40
41 class MyAddDlg(wx.Dialog):
42 def __init__(self, parent, id, title, pos=wx.DefaultPosition,
43 size=wx.DefaultSize, style=wx.DEFAULT_DIALOG_STYLE):
44 wx.Dialog.__init__(self, parent, id, title, pos, size, style)
45
46 #------------
47
48 # Simplified init method.
49 self.CreateCtrls()
50 self.BindEvents()
51 self.DoLayout()
52
53 #-----------------------------------------------------------------------
54
55 def CreateCtrls(self):
56 """
57 ...
58 """
59
60 # Initialize some controls.
61 self.text1 = wx.TextCtrl(self, -1, "")
62 self.text2 = wx.TextCtrl(self, -1, "")
63
64 #------------
65
66 self.okay = wx.Button(self, wx.ID_OK)
67 self.cancel = wx.Button(self, wx.ID_CANCEL)
68
69 #------------
70
71 self.st = wx.StaticText(self, -1, "")
72 self.label1 = wx.StaticText(self, -1, "Name :")
73 self.label2 = wx.StaticText(self, -1, "Phone :")
74
75
76 def BindEvents(self):
77 """
78 ...
79 """
80
81 # Bind our events.
82 self.Bind(wx.EVT_BUTTON, self.OnButton)
83
84
85 def DoLayout(self):
86 """
87 ...
88 """
89
90 # Initialize the sizers and fill them.
91 hsizer = wx.BoxSizer(wx.HORIZONTAL)
92 sizer = wx.BoxSizer(wx.VERTICAL)
93
94 #------------
95
96 hsizer.Add(self.st, 1, wx.EXPAND, 0)
97 hsizer.Add(self.okay, 0, wx.EXPAND, 0)
98 hsizer.Add(self.cancel, 0, wx.EXPAND, 0)
99
100 #------------
101
102 sizer.Add(self.label1, 0, wx.EXPAND | wx.ALL, 4)
103 sizer.Add(self.text1, 0, wx.EXPAND | wx.ALL, 4)
104 sizer.Add(self.label2, 0, wx.EXPAND | wx.ALL, 4)
105 sizer.Add(self.text2, 0, wx.EXPAND | wx.ALL, 4)
106 sizer.Add(hsizer, 0, wx.EXPAND | wx.ALL, 4)
107
108 #------------
109
110 # Finally, tell the dialog to use the sizer for layout.
111 self.SetSizerAndFit(sizer)
112
113
114 def OnButton(self, event):
115 """
116 ...
117 """
118
119 self.value1 = self.text1.GetValue()
120 self.value2 = self.text2.GetValue()
121 self.EndModal(event.GetId())
122
123 #-------------------------------------------------------------------------------
124
125 class MyFrame(wx.Frame):
126 def __init__(self, parent, id, title):
127 wx.Frame.__init__(self, parent, -1,
128 title,
129 size=(380, 320))
130
131 #------------
132
133 # Return icons folder.
134 self.icons_dir = wx.GetApp().GetIconsDir()
135
136 #------------
137
138 # Simplified init method.
139 self.SetProperties()
140 self.CreateCtrls()
141 self.BindEvents()
142 self.DoLayout()
143
144 #------------
145
146 self.CenterOnScreen(wx.BOTH)
147
148 #---------------------------------------------------------------------------
149
150 def SetProperties(self):
151 """
152 Set the frame properties (title, icon, transparency...).
153 """
154
155 self.SetMinSize((380, 320))
156
157 #------------
158
159 frameIcon = wx.Icon(os.path.join(self.icons_dir,
160 "wxwin.ico"),
161 type=wx.BITMAP_TYPE_ICO)
162 self.SetIcon(frameIcon)
163
164
165 def CreateCtrls(self):
166 """
167 Make widgets for my frame.
168 """
169
170 self.pnl = wx.Panel(self, -1)
171 self.pnl.SetBackgroundColour("#e6e6e6")
172
173 #------------
174
175 self.txt1 = wx.StaticText(self.pnl, -1, "Name : ", size=(50, -1))
176 self.name = wx.TextCtrl(self.pnl, 12, "", (-1, -1), (150, -1))
177
178 #------------
179
180 self.txt2 = wx.StaticText(self.pnl, -1, "Phone : ", size=(50, -1))
181 self.phone = wx.TextCtrl(self.pnl, 12, "", (-1, -1), (150, -1))
182
183 #------------
184
185 self.b1 = wx.Button(self.pnl, 11, "&Add")
186 self.b2 = wx.Button(self.pnl, 12, "&Update")
187 self.b3 = wx.Button(self.pnl, 13, "&Delete")
188
189 #------------
190
191 self.lc = wx.ListCtrl(self.pnl, 15,
192 style=wx.LC_REPORT |
193 wx.BORDER_SUNKEN |
194 wx.LC_VRULES |
195 wx.LC_HRULES |
196 wx.LC_SINGLE_SEL)
197 self.lc.InsertColumn(0, "Name")
198 self.lc.InsertColumn(1, "Phone")
199
200 self.lc.SetColumnWidth(0, wx.LIST_AUTOSIZE)
201 self.lc.SetColumnWidth(1, 230)
202
203 self.lc.SetBackgroundColour("#f3e8c4")
204
205
206 def BindEvents(self):
207 """
208 Bind all the events related to my frame.
209 """
210
211 self.Bind(wx.EVT_BUTTON, self.AddEntry, id=11)
212 self.Bind(wx.EVT_BUTTON, self.UpdateEntry, id=12)
213 self.Bind(wx.EVT_BUTTON, self.DeleteEntry, id=13)
214
215 self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.GetSelect, id=15)
216
217
218 def DoLayout(self):
219 """
220 ...
221 """
222
223 # MainSizer is the top-level one that manages everything.
224 mainSizer = wx.BoxSizer(wx.VERTICAL)
225
226 #------------
227
228 nameSizer = wx.BoxSizer(wx.HORIZONTAL)
229 phoneSizer = wx.BoxSizer(wx.HORIZONTAL)
230 btnSizer = wx.BoxSizer(wx.HORIZONTAL)
231 listSizer = wx.BoxSizer(wx.VERTICAL)
232 borderSizer = wx.BoxSizer(wx.VERTICAL)
233
234 #------------
235
236 nameSizer.Add(self.txt1, 0, wx.ALIGN_CENTER)
237 nameSizer.Add(self.name, 0)
238
239 phoneSizer.Add(self.txt2, 0, wx.ALIGN_CENTER)
240 phoneSizer.Add(self.phone, 0)
241
242 btnSizer.Add(self.b1, 1, wx.EXPAND|wx.RIGHT, 5)
243 btnSizer.Add(self.b2, 1, wx.EXPAND, 5)
244 btnSizer.Add(self.b3, 1, wx.EXPAND|wx.LEFT, 5)
245
246 listSizer.Add(self.lc, 1, wx.EXPAND)
247
248 #------------
249
250 borderSizer.Add(nameSizer, 0, wx.ALL, 5)
251 borderSizer.Add(phoneSizer, 0, wx.ALL, 5)
252 borderSizer.Add(btnSizer, 0, wx.ALL, 5)
253 borderSizer.Add(listSizer, 1, wx.EXPAND|wx.ALL, 5)
254
255 #------------
256
257 mainSizer.Add(borderSizer, 1, wx.EXPAND|wx.ALL, 5)
258
259 #------------
260
261 # Finally, tell the panel to use the sizer for layout.
262 self.pnl.SetSizer(mainSizer)
263 self.Layout()
264
265
266 def SetSelect (self) :
267 """
268 ...
269 """
270
271
272 print("Phone list :", phoneList)
273
274 #------------
275
276 self.lc.DeleteAllItems()
277
278 #------------
279
280 for i in range(len(phoneList)) :
281 name, phone = phoneList[i]
282 self.lc.InsertItem(i, name)
283 self.lc.SetItem(i, 1, phone)
284
285 #------------
286
287 self.lc.SetColumnWidth(0, wx.LIST_AUTOSIZE)
288
289 #------------
290
291 # Just in case update attempted before load.
292 self.currentSel = 0
293
294
295 def GetSelect (self, event) :
296 """
297 ...
298 """
299
300 self.currentSel = event.Index
301
302 name, phone = phoneList[self.currentSel]
303 self.name.SetValue(name)
304 print("Name :", self.name.GetValue())
305
306 self.phone.SetValue(phone)
307 print("Phone :", self.phone.GetValue())
308
309 print("Selected row :", self.currentSel)
310
311
312 def AddEntry (self, event) :
313 """
314 ...
315 """
316
317 dlg = MyAddDlg(self, -1, "Add...")
318 if dlg.ShowModal() == wx.ID_OK:
319 phoneList.append([dlg.value1,
320 dlg.value2])
321
322 self.SetSelect()
323
324
325 def UpdateEntry(self, event) :
326 """
327 ...
328 """
329
330 phoneList[self.currentSel] = [self.name.GetValue(),
331 self.phone.GetValue()]
332
333 self.SetSelect()
334
335
336 def DeleteEntry(self, event) :
337 """
338 ...
339 """
340
341 del phoneList[self.currentSel]
342
343 self.SetSelect()
344
345 #-------------------------------------------------------------------------------
346
347 class MyApp(wx.App):
348 def OnInit(self):
349
350 #------------
351
352 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
353
354 #------------
355
356 frame = MyFrame(None, -1, "Sample ten (Phone list v0.0.2)")
357 frame.SetSelect()
358 self.SetTopWindow(frame)
359 frame.Show(True)
360
361 return True
362
363 #-----------------------------------------------------------------------
364
365 def GetInstallDir(self):
366 """
367 Return the installation directory for my application.
368 """
369
370 return self.installDir
371
372
373 def GetIconsDir(self):
374 """
375 Return the icons directory for my application.
376 """
377
378 icons_dir = os.path.join(self.installDir, "icons")
379 return icons_dir
380
381 #-------------------------------------------------------------------------------
382
383 def main():
384 app = MyApp(False)
385 app.MainLoop()
386
387 #-------------------------------------------------------------------------------
388
389 if __name__ == "__main__" :
390 main()
Download source
Additional Information
Link :
http://jak-o-shadows.users.sourceforge.net/python/wxpy/dblistctrl.html
http://wxpython-users.1045709.n5.nabble.com/Example-of-Database-Interaction-td2361801.html
http://www.kitebird.com/articles/pydbapi.html
https://www.pgadmin.org/download/
https://github.com/1966bc/pyggybank
https://sourceforge.net/projects/pyggybank/
- - - - -
https://wiki.wxpython.org/TitleIndex
Thanks to
Chris Meyers (sample_ten.py coding), Robin Dunn, Robin Rappin, Mike Driscoll, the wxPython community...
About this page
Date(d/m/y) Person (bot) Comments :
07/03/20 - Ecco (Created page for wxPython Phoenix).
Comments
- blah, blah, blah....