|
| 1 | +# _*_ coding: utf-8 _*_ |
| 2 | + |
| 3 | +""" |
| 4 | +Hello World |
| 5 | +""" |
| 6 | + |
| 7 | +import wx |
| 8 | + |
| 9 | + |
| 10 | +class HelloFrame(wx.Frame): |
| 11 | + """ |
| 12 | + A Frame that says Hello World |
| 13 | + """ |
| 14 | + |
| 15 | + def __init__(self, *args, **kw): |
| 16 | + # ensure the parent's __init__ is called |
| 17 | + super(HelloFrame, self).__init__(*args, **kw) |
| 18 | + |
| 19 | + # create a panel in the frame |
| 20 | + pnl = wx.Panel(self) |
| 21 | + |
| 22 | + # and put some text with a larger bold font on it |
| 23 | + st = wx.StaticText(parent=pnl, label="Hello World!", pos=(25, 25)) |
| 24 | + font = st.GetFont() |
| 25 | + font.PointSize += 10 |
| 26 | + font = font.Bold() |
| 27 | + st.SetFont(font) |
| 28 | + |
| 29 | + # create a menu bar |
| 30 | + self.make_menubar() |
| 31 | + |
| 32 | + # and a status bar |
| 33 | + self.CreateStatusBar() |
| 34 | + self.SetStatusText("Welcome to wxPython!") |
| 35 | + |
| 36 | + def make_menubar(self): |
| 37 | + """ |
| 38 | + A menu bar is composed of menus, which are composed of menu items. |
| 39 | + This method builds a set of menus and binds handlers to be called |
| 40 | + when the menu item is selected. |
| 41 | + """ |
| 42 | + # Make a file menu with Hello and Exit items |
| 43 | + menu_file = wx.Menu() |
| 44 | + # The "\t..." syntax defines an accelerator key that also triggers the same event |
| 45 | + |
| 46 | + item_hello = menu_file.Append(-1, "&Hello...\tCtrl-H", "Help string shown in status bar for this menu item") |
| 47 | + menu_file.AppendSeparator() |
| 48 | + |
| 49 | + # When using a stock ID we don't need to specify the menu item's label |
| 50 | + item_exit = menu_file.Append(wx.ID_EXIT) |
| 51 | + |
| 52 | + # Now a help menu for the about item |
| 53 | + menu_help = wx.Menu() |
| 54 | + item_about = menu_help.Append(wx.ID_ABOUT) |
| 55 | + |
| 56 | + # Make the menu bar and add the two menus to it. The '&' defines |
| 57 | + # that the next letter is the "mnemonic" for the menu item. On the |
| 58 | + # platforms that support it those letters are underlined and can be |
| 59 | + # triggered from the keyboard. |
| 60 | + menu_bar = wx.MenuBar() |
| 61 | + menu_bar.Append(menu_file, "&File") |
| 62 | + menu_bar.Append(menu_help, "&Help") |
| 63 | + |
| 64 | + # Give the menu bar to the frame |
| 65 | + self.SetMenuBar(menu_bar) |
| 66 | + |
| 67 | + # Finally, associate a handler function with the EVT_MENU event for |
| 68 | + # each of the menu items. That means that when that menu item is |
| 69 | + # activated then the associated handler function will be called. |
| 70 | + self.Bind(wx.EVT_MENU, self.OnHello, item_hello) |
| 71 | + self.Bind(wx.EVT_MENU, self.OnExit, item_exit) |
| 72 | + self.Bind(wx.EVT_MENU, self.OnAbout, item_about) |
| 73 | + |
| 74 | + def OnExit(self, event): |
| 75 | + """Close the frame, terminating the application.""" |
| 76 | + self.Close(True) |
| 77 | + |
| 78 | + def OnHello(self, event): |
| 79 | + """Say hello to the user.""" |
| 80 | + wx.MessageBox("Hello again from wxPython") |
| 81 | + |
| 82 | + def OnAbout(self, event): |
| 83 | + """Display an About Dialog""" |
| 84 | + wx.MessageBox("This is a wxPython Hello World sample", "About Hello World 2", wx.OK | wx.ICON_INFORMATION) |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == '__main__': |
| 88 | + # When this module is run (not imported) then create the app, the |
| 89 | + # frame, show it, and start the event loop. |
| 90 | + app = wx.App() |
| 91 | + frm = HelloFrame(None, title='Hello World 2') |
| 92 | + frm.Show() |
| 93 | + app.MainLoop() |
0 commit comments