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