在wxPython使ListCtrl占据整个窗口
myapp.py:
1 # -*- coding: gb2312 -*-
2 import wx, wx.xrc
3 import sys
4
5 class MyApp(wx.App):
6 def OnInit(self):
7 self.RedirectStdio("err.log")
8 self.res = wx.xrc.XmlResource("my.xrc")
9 self.InitFrame()
10 self.Show()
11 return True
12
13 def InitFrame(self):
14 self.frame = self.res.LoadFrame(None, "MainFrame")
15 self.panel = wx.xrc.XRCCTRL(self.frame, "MainPanel")
16 self.list = wx.ListCtrl(self.panel, wx.NewId(),
17 style=wx.LC_REPORT
18 | wx.BORDER_NONE
19 | wx.LC_EDIT_LABELS
20 | wx.LC_SORT_ASCENDING
21 )
22 self.list.InsertColumn(0, "Artist")
23 self.list.InsertColumn(1, "Title")
24 self.list.InsertColumn(2, "Genre")
25
26 box = wx.BoxSizer(wx.VERTICAL)
27 box.Add(self.list, 1, wx.EXPAND|wx.ALL, 5)
28 self.panel.SetSizer(box)
29
30 def Show(self):
31 self.SetTopWindow(self.frame)
32 self.frame.Show()
33
34 app = MyApp()
35 app.MainLoop()
2 import wx, wx.xrc
3 import sys
4
5 class MyApp(wx.App):
6 def OnInit(self):
7 self.RedirectStdio("err.log")
8 self.res = wx.xrc.XmlResource("my.xrc")
9 self.InitFrame()
10 self.Show()
11 return True
12
13 def InitFrame(self):
14 self.frame = self.res.LoadFrame(None, "MainFrame")
15 self.panel = wx.xrc.XRCCTRL(self.frame, "MainPanel")
16 self.list = wx.ListCtrl(self.panel, wx.NewId(),
17 style=wx.LC_REPORT
18 | wx.BORDER_NONE
19 | wx.LC_EDIT_LABELS
20 | wx.LC_SORT_ASCENDING
21 )
22 self.list.InsertColumn(0, "Artist")
23 self.list.InsertColumn(1, "Title")
24 self.list.InsertColumn(2, "Genre")
25
26 box = wx.BoxSizer(wx.VERTICAL)
27 box.Add(self.list, 1, wx.EXPAND|wx.ALL, 5)
28 self.panel.SetSizer(box)
29
30 def Show(self):
31 self.SetTopWindow(self.frame)
32 self.frame.Show()
33
34 app = MyApp()
35 app.MainLoop()
my.xrc:
1 <?xml version="1.0" encoding="utf-8"?>
2 <resource>
3 <object class="wxFrame" name="MainFrame">
4 <title></title>
5 <object class="wxPanel" name="MainPanel">
6 <style>wxWANTS_CHARS</style>
7 </object>
8 </object>
9 </resource>
2 <resource>
3 <object class="wxFrame" name="MainFrame">
4 <title></title>
5 <object class="wxPanel" name="MainPanel">
6 <style>wxWANTS_CHARS</style>
7 </object>
8 </object>
9 </resource>
说明:
因为panel并不会讲其中的控件最大化,所以其中的ListCtrl并不会占据整个窗口,但是使用BoxSizer可以做到这一点。见代码中的红色部分。