]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-10/add_items.py
Added the sample code from wxPython In Action to the samples dir
[wxWidgets.git] / wxPython / samples / wxPIA_book / Chapter-10 / add_items.py
1 import wx
2
3 class MyFrame(wx.Frame):
4 def __init__(self):
5 wx.Frame.__init__(self, None, -1,
6 "Add Menu Items")
7 p = wx.Panel(self)
8 self.txt = wx.TextCtrl(p, -1, "new item")
9 btn = wx.Button(p, -1, "Add Menu Item")
10 self.Bind(wx.EVT_BUTTON, self.OnAddItem, btn)
11
12 sizer = wx.BoxSizer(wx.HORIZONTAL)
13 sizer.Add(self.txt, 0, wx.ALL, 20)
14 sizer.Add(btn, 0, wx.TOP|wx.RIGHT, 20)
15 p.SetSizer(sizer)
16
17 self.menu = menu = wx.Menu()
18 simple = menu.Append(-1, "Simple menu item")
19 menu.AppendSeparator()
20 exit = menu.Append(-1, "Exit")
21 self.Bind(wx.EVT_MENU, self.OnSimple, simple)
22 self.Bind(wx.EVT_MENU, self.OnExit, exit)
23
24 menuBar = wx.MenuBar()
25 menuBar.Append(menu, "Menu")
26 self.SetMenuBar(menuBar)
27
28
29 def OnSimple(self, event):
30 wx.MessageBox("You selected the simple menu item")
31
32 def OnExit(self, event):
33 self.Close()
34
35 def OnAddItem(self, event):
36 item = self.menu.Append(-1, self.txt.GetValue())
37 self.Bind(wx.EVT_MENU, self.OnNewItemSelected, item)
38
39 def OnNewItemSelected(self, event):
40 wx.MessageBox("You selected a new item")
41
42
43
44 if __name__ == "__main__":
45 app = wx.PySimpleApp()
46 frame = MyFrame()
47 frame.Show()
48 app.MainLoop()
49
50