Introduction
A Sample for the Splitter Window, less trivial than the one provided in Demo
Feel free to enhance it
Special Concerns
This script uses the new wx Python package, so wxPython 2.4.1 or newer is required.
Code Sample
1 #!/usr/bin/env python
2 import wx
3
4 class Mypanel(wx.Panel):
5 """
6 Contains the buttons and the label of the window
7 """
8
9 def __init__(self, parent):
10 self.parent = parent
11 wx.Panel.__init__(self, parent, pos=(6, 6), size=(200, 500))
12 self.SetBackgroundColour(self.parent.GetBackgroundColour())
13 self.splitv_button= wx.Button(self, label="Split me Vertically")
14 self.splith_button= wx.Button(self, label="Split me Horizontally")
15 self.parent.Bind(wx.EVT_BUTTON, self.parent.splitme, id=self.splitv_button.GetId())
16 self.parent.Bind(wx.EVT_BUTTON, self.parent.splitme, id=self.splith_button.GetId())
17 self.sp1 = None
18 self.sp2 = None
19 if not self.parent.label:
20 self.txt = wx.StaticText(self, label="First Splitter")
21 else:
22 self.txt = wx.StaticText(self, label=self.parent.label)
23 self.unsplit_button = wx.Button(self, label="Unsplit me")
24 self.parent.Bind(wx.EVT_BUTTON, self.parent.unsplitme, id=self.unsplit_button.GetId())
25
26 #making a sizer
27
28 box = wx.BoxSizer(wx.VERTICAL)
29 box.Add(self.txt, 0, wx.EXPAND)
30
31 box.Add(self.splitv_button, 0, wx.EXPAND)
32 box.Add(self.splith_button, 0, wx.EXPAND)
33
34 if self.parent.label:
35 box.Add(self.unsplit_button, 0, wx.EXPAND)
36
37 self.SetAutoLayout(True)
38 self.SetSizer(box)
39 self.Layout()
40
41 self.Fit()
42 self.Show()
43
44 class Mysplitter(wx.SplitterWindow):
45 def __init__(self, parent, title, colour):
46 self.parent = parent
47 self.title = title
48 self.sp1 = None
49 self.sp2 = None
50 wx.SplitterWindow.__init__(self, parent, style=wx.NO_3D | wx.SP_3D)
51 self.SetBackgroundColour(colour)
52 try:
53 self.number = self.parent.number+1
54 except:
55 self.number = 0
56 if self.number > 0:
57 self.label = self.parent.label + self.title[-1] + "."
58 else:
59 self.label = ""
60
61 self.pn = Mypanel(self)
62 self.pn.Show()
63
64 def splitme(self,event):
65 "split the window"
66 button = str(event.GetEventObject().GetLabel())
67 if self.pn:
68 self.pn.Destroy()
69 self.pn = None
70 self.sp1 = Mysplitter(self, "sp1", wx.BLUE)
71 self.sp2 = Mysplitter(self, "sp2", wx.RED)
72
73 if button == 'Split me Vertically':
74 self.SplitVertically(self.sp1, self.sp2, 0)
75 else:
76 self.SplitHorizontally(self.sp1, self.sp2, 0)
77
78 self.SetMinimumPaneSize(20)
79
80
81 def unsplitme(self,event):
82 "unsplit the parent window"
83 self.parent.Unsplit(None)
84 #self.parent.sp1.Destroy() #cause a segmentation fault after a while
85 self.parent.sp1.Hide()
86 #self.parent.sp2.Destroy()
87 self.parent.sp2.Hide()
88 self.parent.pn = Mypanel(self.parent)
89 self.parent.pn.Show()
90 self.parent.Refresh()
91
92 class MyFrame(wx.Frame):
93
94 def __init__(self, parent, title):
95 wx.Frame.__init__(self, parent, title=title, size=(800, 600),
96 style=wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
97 menu = wx.Menu()
98
99 menu.Append(wx.ID_EXIT, "E&xit", "Terminate the program")
100 menuBar = wx.MenuBar()
101 menuBar.Append(menu, "&File");
102 self.SetMenuBar(menuBar)
103 self.splitter = Mysplitter(self, "sp", wx.BLUE)
104 self.Bind(wx.EVT_MENU, self.DoExit, id=wx.ID_EXIT)
105 self.Show()
106
107 def DoExit(self, event):
108 self.Close(True)
109
110
111 app = wx.App(False)
112 frame = MyFrame(None, "gogo")
113 app.MainLoop()
Comments
Franz Steinhaeusler
Thanks, I add it to the page "wxPython Demo Style Guide Update Status.