~ellisonbg/ipython/trunk-dev

« back to all changes in this revision

Viewing changes to docs/examples/lib/gui-wx.py

  • Committer: Brian Granger
  • Date: 2010-01-31 23:57:46 UTC
  • mfrom: (1109.1.113 ipython)
  • Revision ID: ellisonbg@gmail.com-20100131235746-rj81qa8klooepq2m
Merging from upstream trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""A Simple wx example to test IPython's event loop integration.
 
2
 
 
3
To run this do:
 
4
 
 
5
In [5]: %gui wx
 
6
 
 
7
In [6]: %run gui-wx.py
 
8
 
 
9
Ref: Modified from wxPython source code wxPython/samples/simple/simple.py
 
10
 
 
11
This example can only be run once in a given IPython session because when 
 
12
the frame is closed, wx goes through its shutdown sequence, killing further
 
13
attempts.  I am sure someone who knows wx can fix this issue.
 
14
 
 
15
Furthermore, once this example is run, the Wx event loop is mostly dead, so
 
16
even other new uses of Wx may not work correctly.  If you know how to better
 
17
handle this, please contact the ipython developers and let us know.
 
18
 
 
19
Note however that we will work with the Matplotlib and Enthought developers so
 
20
that the main interactive uses of Wx we are aware of, namely these tools, will
 
21
continue to work well with IPython interactively.
 
22
"""
 
23
 
 
24
import wx
 
25
 
 
26
 
 
27
class MyFrame(wx.Frame):
 
28
    """
 
29
    This is MyFrame.  It just shows a few controls on a wxPanel,
 
30
    and has a simple menu.
 
31
    """
 
32
    def __init__(self, parent, title):
 
33
        wx.Frame.__init__(self, parent, -1, title,
 
34
                          pos=(150, 150), size=(350, 200))
 
35
 
 
36
        # Create the menubar
 
37
        menuBar = wx.MenuBar()
 
38
 
 
39
        # and a menu 
 
40
        menu = wx.Menu()
 
41
 
 
42
        # add an item to the menu, using \tKeyName automatically
 
43
        # creates an accelerator, the third param is some help text
 
44
        # that will show up in the statusbar
 
45
        menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit this simple sample")
 
46
 
 
47
        # bind the menu event to an event handler
 
48
        self.Bind(wx.EVT_MENU, self.OnTimeToClose, id=wx.ID_EXIT)
 
49
 
 
50
        # and put the menu on the menubar
 
51
        menuBar.Append(menu, "&File")
 
52
        self.SetMenuBar(menuBar)
 
53
 
 
54
        self.CreateStatusBar()
 
55
 
 
56
        # Now create the Panel to put the other controls on.
 
57
        panel = wx.Panel(self)
 
58
 
 
59
        # and a few controls
 
60
        text = wx.StaticText(panel, -1, "Hello World!")
 
61
        text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
 
62
        text.SetSize(text.GetBestSize())
 
63
        btn = wx.Button(panel, -1, "Close")
 
64
        funbtn = wx.Button(panel, -1, "Just for fun...")
 
65
 
 
66
        # bind the button events to handlers
 
67
        self.Bind(wx.EVT_BUTTON, self.OnTimeToClose, btn)
 
68
        self.Bind(wx.EVT_BUTTON, self.OnFunButton, funbtn)
 
69
 
 
70
        # Use a sizer to layout the controls, stacked vertically and with
 
71
        # a 10 pixel border around each
 
72
        sizer = wx.BoxSizer(wx.VERTICAL)
 
73
        sizer.Add(text, 0, wx.ALL, 10)
 
74
        sizer.Add(btn, 0, wx.ALL, 10)
 
75
        sizer.Add(funbtn, 0, wx.ALL, 10)
 
76
        panel.SetSizer(sizer)
 
77
        panel.Layout()
 
78
 
 
79
 
 
80
    def OnTimeToClose(self, evt):
 
81
        """Event handler for the button click."""
 
82
        print "See ya later!"
 
83
        self.Close()
 
84
 
 
85
    def OnFunButton(self, evt):
 
86
        """Event handler for the button click."""
 
87
        print "Having fun yet?"
 
88
 
 
89
 
 
90
class MyApp(wx.App):
 
91
    def OnInit(self):
 
92
        frame = MyFrame(None, "Simple wxPython App")
 
93
        self.SetTopWindow(frame)
 
94
 
 
95
        print "Print statements go to this stdout window by default."
 
96
 
 
97
        frame.Show(True)
 
98
        return True
 
99
 
 
100
app = wx.GetApp()
 
101
if app is None:
 
102
    app = MyApp(redirect=False, clearSigInt=False)
 
103
 
 
104
try:
 
105
    from IPython.lib.inputhook import appstart_wx
 
106
    appstart_wx(app)
 
107
except ImportError:
 
108
    app.MainLoop()
 
109