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