~brian-sidebotham/wxwidgets-cmake/wxpython-2.9.4

« back to all changes in this revision

Viewing changes to wxPython/samples/simple/simple.py

  • Committer: Brian Sidebotham
  • Date: 2013-08-03 14:30:08 UTC
  • Revision ID: brian.sidebotham@gmail.com-20130803143008-c7806tkych1tp6fc
Initial import into Bazaar

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#----------------------------------------------------------------------
 
2
# A very simple wxPython example.  Just a wx.Frame, wx.Panel,
 
3
# wx.StaticText, wx.Button, and a wx.BoxSizer, but it shows the basic
 
4
# structure of any wxPython application.
 
5
#----------------------------------------------------------------------
 
6
 
 
7
import wx
 
8
 
 
9
 
 
10
class MyFrame(wx.Frame):
 
11
    """
 
12
    This is MyFrame.  It just shows a few controls on a wxPanel,
 
13
    and has a simple menu.
 
14
    """
 
15
    def __init__(self, parent, title):
 
16
        wx.Frame.__init__(self, parent, -1, title,
 
17
                          pos=(150, 150), size=(350, 200))
 
18
 
 
19
        # Create the menubar
 
20
        menuBar = wx.MenuBar()
 
21
 
 
22
        # and a menu 
 
23
        menu = wx.Menu()
 
24
 
 
25
        # add an item to the menu, using \tKeyName automatically
 
26
        # creates an accelerator, the third param is some help text
 
27
        # that will show up in the statusbar
 
28
        menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit this simple sample")
 
29
 
 
30
        # bind the menu event to an event handler
 
31
        self.Bind(wx.EVT_MENU, self.OnTimeToClose, id=wx.ID_EXIT)
 
32
 
 
33
        # and put the menu on the menubar
 
34
        menuBar.Append(menu, "&File")
 
35
        self.SetMenuBar(menuBar)
 
36
 
 
37
        self.CreateStatusBar()
 
38
        
 
39
 
 
40
        # Now create the Panel to put the other controls on.
 
41
        panel = wx.Panel(self)
 
42
 
 
43
        # and a few controls
 
44
        text = wx.StaticText(panel, -1, "Hello World!")
 
45
        text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
 
46
        text.SetSize(text.GetBestSize())
 
47
        btn = wx.Button(panel, -1, "Close")
 
48
        funbtn = wx.Button(panel, -1, "Just for fun...")
 
49
 
 
50
        # bind the button events to handlers
 
51
        self.Bind(wx.EVT_BUTTON, self.OnTimeToClose, btn)
 
52
        self.Bind(wx.EVT_BUTTON, self.OnFunButton, funbtn)
 
53
 
 
54
        # Use a sizer to layout the controls, stacked vertically and with
 
55
        # a 10 pixel border around each
 
56
        sizer = wx.BoxSizer(wx.VERTICAL)
 
57
        sizer.Add(text, 0, wx.ALL, 10)
 
58
        sizer.Add(btn, 0, wx.ALL, 10)
 
59
        sizer.Add(funbtn, 0, wx.ALL, 10)
 
60
        panel.SetSizer(sizer)
 
61
        panel.Layout()
 
62
 
 
63
        # And also use a sizer to manage the size of the panel such
 
64
        # that it fills the frame
 
65
        sizer = wx.BoxSizer()
 
66
        sizer.Add(panel, 1, wx.EXPAND)
 
67
        self.SetSizer(sizer)
 
68
        
 
69
 
 
70
    def OnTimeToClose(self, evt):
 
71
        """Event handler for the button click."""
 
72
        print "See ya later!"
 
73
        self.Close()
 
74
 
 
75
    def OnFunButton(self, evt):
 
76
        """Event handler for the button click."""
 
77
        print "Having fun yet?"
 
78
 
 
79
 
 
80
class MyApp(wx.App):
 
81
    def OnInit(self):
 
82
        frame = MyFrame(None, "Simple wxPython App")
 
83
        self.SetTopWindow(frame)
 
84
 
 
85
        print "Print statements go to this stdout window by default."
 
86
 
 
87
        frame.Show(True)
 
88
        return True
 
89
        
 
90
app = MyApp(redirect=True)
 
91
app.MainLoop()
 
92