]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-05/goodExample.py
Added the sample code from wxPython In Action to the samples dir
[wxWidgets.git] / wxPython / samples / wxPIA_book / Chapter-05 / goodExample.py
1 #!/usr/bin/env python
2
3 import wx
4
5 class RefactorExample(wx.Frame):
6
7 def __init__(self, parent, id):
8 wx.Frame.__init__(self, parent, id, 'Refactor Example',
9 size=(340, 200))
10 panel = wx.Panel(self, -1)
11 panel.SetBackgroundColour("White")
12 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
13 self.createMenuBar()
14 self.createButtonBar(panel)
15 self.createTextFields(panel)
16
17 def menuData(self):
18 return (("&File",
19 ("&Open", "Open in status bar", self.OnOpen),
20 ("&Quit", "Quit", self.OnCloseWindow)),
21 ("&Edit",
22 ("&Copy", "Copy", self.OnCopy),
23 ("C&ut", "Cut", self.OnCut),
24 ("&Paste", "Paste", self.OnPaste),
25 ("", "", ""),
26 ("&Options...", "DisplayOptions", self.OnOptions)))
27
28 def createMenuBar(self):
29 menuBar = wx.MenuBar()
30 for eachMenuData in self.menuData():
31 menuLabel = eachMenuData[0]
32 menuItems = eachMenuData[1:]
33 menuBar.Append(self.createMenu(menuItems), menuLabel)
34 self.SetMenuBar(menuBar)
35
36 def createMenu(self, menuData):
37 menu = wx.Menu()
38 for eachLabel, eachStatus, eachHandler in menuData:
39 if not eachLabel:
40 menu.AppendSeparator()
41 continue
42 menuItem = menu.Append(-1, eachLabel, eachStatus)
43 self.Bind(wx.EVT_MENU, eachHandler, menuItem)
44 return menu
45
46 def buttonData(self):
47 return (("First", self.OnFirst),
48 ("<< PREV", self.OnPrev),
49 ("NEXT >>", self.OnNext),
50 ("Last", self.OnLast))
51
52 def createButtonBar(self, panel, yPos = 0):
53 xPos = 0
54 for eachLabel, eachHandler in self.buttonData():
55 pos = (xPos, yPos)
56 button = self.buildOneButton(panel, eachLabel, eachHandler, pos)
57 xPos += button.GetSize().width
58
59 def buildOneButton(self, parent, label, handler, pos=(0,0)):
60 button = wx.Button(parent, -1, label, pos)
61 self.Bind(wx.EVT_BUTTON, handler, button)
62 return button
63
64 def textFieldData(self):
65 return (("First Name", (10, 50)),
66 ("Last Name", (10, 80)))
67
68 def createTextFields(self, panel):
69 for eachLabel, eachPos in self.textFieldData():
70 self.createCaptionedText(panel, eachLabel, eachPos)
71
72 def createCaptionedText(self, panel, label, pos):
73 static = wx.StaticText(panel, wx.NewId(), label, pos)
74 static.SetBackgroundColour("White")
75 textPos = (pos[0] + 75, pos[1])
76 wx.TextCtrl(panel, wx.NewId(), "", size=(100, -1), pos=textPos)
77
78 # Just grouping the empty event handlers together
79 def OnPrev(self, event): pass
80 def OnNext(self, event): pass
81 def OnLast(self, event): pass
82 def OnFirst(self, event): pass
83 def OnOpen(self, event): pass
84 def OnCopy(self, event): pass
85 def OnCut(self, event): pass
86 def OnPaste(self, event): pass
87 def OnOptions(self, event): pass
88 def OnCloseWindow(self, event):
89 self.Destroy()
90
91 if __name__ == '__main__':
92 app = wx.PySimpleApp()
93 frame = RefactorExample(parent=None, id=-1)
94 frame.Show()
95 app.MainLoop()
96