Show More
@@ -1,121 +1,106 b'' | |||||
1 | #!/usr/bin/env python |
|
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 | A Simple wx example to test IPython's event loop integration. |
|
3 | A Simple wx example to test IPython's event loop integration. | |
8 |
|
4 | |||
9 | To run this do: |
|
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 | In [6]: %run gui-wx.py |
|
9 | In [6]: %run gui-wx.py | |
14 |
|
10 | |||
15 | Ref: Modified from wxPython source code wxPython/samples/simple/simple.py |
|
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 | import wx |
|
14 | import wx | |
31 |
|
15 | |||
32 |
|
16 | |||
33 | class MyFrame(wx.Frame): |
|
17 | class MyFrame(wx.Frame): | |
34 | """ |
|
18 | """ | |
35 | This is MyFrame. It just shows a few controls on a wxPanel, |
|
19 | This is MyFrame. It just shows a few controls on a wxPanel, | |
36 | and has a simple menu. |
|
20 | and has a simple menu. | |
37 | """ |
|
21 | """ | |
38 | def __init__(self, parent, title): |
|
22 | def __init__(self, parent, title): | |
39 | wx.Frame.__init__(self, parent, -1, title, |
|
23 | wx.Frame.__init__(self, parent, -1, title, | |
40 | pos=(150, 150), size=(350, 200)) |
|
24 | pos=(150, 150), size=(350, 200)) | |
41 |
|
25 | |||
42 | # Create the menubar |
|
26 | # Create the menubar | |
43 | menuBar = wx.MenuBar() |
|
27 | menuBar = wx.MenuBar() | |
44 |
|
28 | |||
45 | # and a menu |
|
29 | # and a menu | |
46 | menu = wx.Menu() |
|
30 | menu = wx.Menu() | |
47 |
|
31 | |||
48 | # add an item to the menu, using \tKeyName automatically |
|
32 | # add an item to the menu, using \tKeyName automatically | |
49 | # creates an accelerator, the third param is some help text |
|
33 | # creates an accelerator, the third param is some help text | |
50 | # that will show up in the statusbar |
|
34 | # that will show up in the statusbar | |
51 | menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit this simple sample") |
|
35 | menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit this simple sample") | |
52 |
|
36 | |||
53 | # bind the menu event to an event handler |
|
37 | # bind the menu event to an event handler | |
54 | self.Bind(wx.EVT_MENU, self.OnTimeToClose, id=wx.ID_EXIT) |
|
38 | self.Bind(wx.EVT_MENU, self.OnTimeToClose, id=wx.ID_EXIT) | |
55 |
|
39 | |||
56 | # and put the menu on the menubar |
|
40 | # and put the menu on the menubar | |
57 | menuBar.Append(menu, "&File") |
|
41 | menuBar.Append(menu, "&File") | |
58 | self.SetMenuBar(menuBar) |
|
42 | self.SetMenuBar(menuBar) | |
59 |
|
43 | |||
60 | self.CreateStatusBar() |
|
44 | self.CreateStatusBar() | |
61 |
|
45 | |||
62 | # Now create the Panel to put the other controls on. |
|
46 | # Now create the Panel to put the other controls on. | |
63 | panel = wx.Panel(self) |
|
47 | panel = wx.Panel(self) | |
64 |
|
48 | |||
65 | # and a few controls |
|
49 | # and a few controls | |
66 | text = wx.StaticText(panel, -1, "Hello World!") |
|
50 | text = wx.StaticText(panel, -1, "Hello World!") | |
67 | text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD)) |
|
51 | text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD)) | |
68 | text.SetSize(text.GetBestSize()) |
|
52 | text.SetSize(text.GetBestSize()) | |
69 | btn = wx.Button(panel, -1, "Close") |
|
53 | btn = wx.Button(panel, -1, "Close") | |
70 | funbtn = wx.Button(panel, -1, "Just for fun...") |
|
54 | funbtn = wx.Button(panel, -1, "Just for fun...") | |
71 |
|
55 | |||
72 | # bind the button events to handlers |
|
56 | # bind the button events to handlers | |
73 | self.Bind(wx.EVT_BUTTON, self.OnTimeToClose, btn) |
|
57 | self.Bind(wx.EVT_BUTTON, self.OnTimeToClose, btn) | |
74 | self.Bind(wx.EVT_BUTTON, self.OnFunButton, funbtn) |
|
58 | self.Bind(wx.EVT_BUTTON, self.OnFunButton, funbtn) | |
75 |
|
59 | |||
76 | # Use a sizer to layout the controls, stacked vertically and with |
|
60 | # Use a sizer to layout the controls, stacked vertically and with | |
77 | # a 10 pixel border around each |
|
61 | # a 10 pixel border around each | |
78 | sizer = wx.BoxSizer(wx.VERTICAL) |
|
62 | sizer = wx.BoxSizer(wx.VERTICAL) | |
79 | sizer.Add(text, 0, wx.ALL, 10) |
|
63 | sizer.Add(text, 0, wx.ALL, 10) | |
80 | sizer.Add(btn, 0, wx.ALL, 10) |
|
64 | sizer.Add(btn, 0, wx.ALL, 10) | |
81 | sizer.Add(funbtn, 0, wx.ALL, 10) |
|
65 | sizer.Add(funbtn, 0, wx.ALL, 10) | |
82 | panel.SetSizer(sizer) |
|
66 | panel.SetSizer(sizer) | |
83 | panel.Layout() |
|
67 | panel.Layout() | |
84 |
|
68 | |||
85 |
|
69 | |||
86 | def OnTimeToClose(self, evt): |
|
70 | def OnTimeToClose(self, evt): | |
87 | """Event handler for the button click.""" |
|
71 | """Event handler for the button click.""" | |
88 | print "See ya later!" |
|
72 | print "See ya later!" | |
89 | self.Close() |
|
73 | self.Close() | |
90 |
|
74 | |||
91 | def OnFunButton(self, evt): |
|
75 | def OnFunButton(self, evt): | |
92 | """Event handler for the button click.""" |
|
76 | """Event handler for the button click.""" | |
93 | print "Having fun yet?" |
|
77 | print "Having fun yet?" | |
94 |
|
78 | |||
95 |
|
79 | |||
96 | class MyApp(wx.App): |
|
80 | class MyApp(wx.App): | |
97 | def OnInit(self): |
|
81 | def OnInit(self): | |
98 | frame = MyFrame(None, "Simple wxPython App") |
|
82 | frame = MyFrame(None, "Simple wxPython App") | |
99 | self.SetTopWindow(frame) |
|
83 | self.SetTopWindow(frame) | |
100 |
|
84 | |||
101 | print "Print statements go to this stdout window by default." |
|
85 | print "Print statements go to this stdout window by default." | |
102 |
|
86 | |||
103 | frame.Show(True) |
|
87 | frame.Show(True) | |
104 | return True |
|
88 | return True | |
105 |
|
89 | |||
106 |
|
90 | |||
107 | if __name__ == '__main__': |
|
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 | app = wx.GetApp() |
|
93 | app = wx.GetApp() | |
113 | if app is None: |
|
94 | if app is None: | |
114 | app = MyApp(redirect=False, clearSigInt=False) |
|
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 | try: |
|
102 | try: | |
117 | from IPython.lib.inputhook import enable_wx |
|
103 | from IPython.lib.inputhook import enable_wx | |
118 | enable_wx(app) |
|
104 | enable_wx(app) | |
119 | except ImportError: |
|
105 | except ImportError: | |
120 | app.MainLoop() |
|
106 | app.MainLoop() | |
121 |
|
General Comments 0
You need to be logged in to leave comments.
Login now