]>
Commit | Line | Data |
---|---|---|
1 | import wx | |
2 | ||
3 | class MyFrame(wx.Frame): | |
4 | def __init__(self): | |
5 | wx.Frame.__init__(self, None, -1, | |
6 | "Find Item Example") | |
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 | item = self.GetMenuBar().FindItemById(event.GetId()) | |
41 | text = item.GetText() | |
42 | wx.MessageBox("You selected the '%s' item" % text) | |
43 | ||
44 | ||
45 | ||
46 | if __name__ == "__main__": | |
47 | app = wx.PySimpleApp() | |
48 | frame = MyFrame() | |
49 | frame.Show() | |
50 | app.MainLoop() | |
51 | ||
52 |