Show More
@@ -0,0 +1,140 b'' | |||
|
1 | #!/usr/bin/env python | |
|
2 | # coding: utf-8 | |
|
3 | """ | |
|
4 | Support for creating GUI apps and starting event loops. | |
|
5 | ||
|
6 | IPython's GUI integration allows interative plotting and GUI usage in IPython | |
|
7 | session. IPython has two different types of GUI integration: | |
|
8 | ||
|
9 | 1. The terminal based IPython supports GUI event loops through Python's | |
|
10 | PyOS_InputHook. PyOS_InputHook is a hook that Python calls periodically | |
|
11 | whenever raw_input is waiting for a user to type code. We implement GUI | |
|
12 | support in the terminal by setting PyOS_InputHook to a function that | |
|
13 | iterates the event loop for a short while. It is important to note that | |
|
14 | in this situation, the real GUI event loop is NOT run in the normal | |
|
15 | manner, so you can't use the normal means to detect that it is running. | |
|
16 | 2. In the two process IPython kernel/frontend, the GUI event loop is run in | |
|
17 | the kernel. In this case, the event loop is run in the normal manner by | |
|
18 | calling the function or method of the GUI toolkit that starts the event | |
|
19 | loop. | |
|
20 | ||
|
21 | In addition to starting the GUI event loops in one of these two ways, IPython | |
|
22 | will *always* create an appropriate GUI application object when GUi | |
|
23 | integration is enabled. | |
|
24 | ||
|
25 | If you want your GUI apps to run in IPython you need to do two things: | |
|
26 | ||
|
27 | 1. Test to see if there is already an existing main application object. If | |
|
28 | there is, you should use it. If there is not an existing application object | |
|
29 | you should create one. | |
|
30 | 2. Test to see if the GUI event loop is running. If it is, you should not | |
|
31 | start it. If the event loop is not running you may start it. | |
|
32 | ||
|
33 | This module contains functions for each toolkit that perform these things | |
|
34 | in a consistent manner. Because of how PyOS_InputHook runs the event loop | |
|
35 | you cannot detect if the event loop is running using the traditional calls | |
|
36 | (such as ``wx.GetApp.IsMainLoopRunning()`` in wxPython). If PyOS_InputHook is | |
|
37 | set These methods will return a false negative. That is, they will say the | |
|
38 | event loop is not running, when is actually is. To work around this limitation | |
|
39 | we proposed the following informal protocol: | |
|
40 | ||
|
41 | * Whenever someone starts the event loop, they *must* set the ``_in_event_loop`` | |
|
42 | attribute of the main application object to ``True``. This should be done | |
|
43 | regardless of how the event loop is actually run. | |
|
44 | * Whenever someone stops the event loop, they *must* set the ``_in_event_loop`` | |
|
45 | attribute of the main application object to ``False``. | |
|
46 | * If you want to see if the event loop is running, you *must* use ``hasattr`` | |
|
47 | to see if ``_in_event_loop`` attribute has been set. If it is set, you | |
|
48 | *must* use its value. If it has not been set, you can query the toolkit | |
|
49 | in the normal manner. | |
|
50 | ||
|
51 | The functions below implement this logic for each GUI toolkit. If you need | |
|
52 | to create custom application subclasses, you will likely have to modify this | |
|
53 | code for your own purposes. This code can be copied into your own project | |
|
54 | so you don't have to depend on IPython. | |
|
55 | ||
|
56 | """ | |
|
57 | ||
|
58 | #----------------------------------------------------------------------------- | |
|
59 | # Copyright (C) 2008-2010 The IPython Development Team | |
|
60 | # | |
|
61 | # Distributed under the terms of the BSD License. The full license is in | |
|
62 | # the file COPYING, distributed as part of this software. | |
|
63 | #----------------------------------------------------------------------------- | |
|
64 | ||
|
65 | #----------------------------------------------------------------------------- | |
|
66 | # Imports | |
|
67 | #----------------------------------------------------------------------------- | |
|
68 | ||
|
69 | #----------------------------------------------------------------------------- | |
|
70 | # wx | |
|
71 | #----------------------------------------------------------------------------- | |
|
72 | ||
|
73 | def get_app_wx(*args, **kwargs): | |
|
74 | """Create a new wx app or return an exiting one.""" | |
|
75 | import wx | |
|
76 | app = wx.GetApp() | |
|
77 | if app is None: | |
|
78 | app = wx.PySimpleApp(*args, **kwargs) | |
|
79 | return app | |
|
80 | ||
|
81 | def is_event_loop_running_wx(app=None): | |
|
82 | """Is the wx event loop running.""" | |
|
83 | if app is None: | |
|
84 | app = get_app_wx() | |
|
85 | if hasattr(app, '_in_event_loop'): | |
|
86 | return app._in_event_loop | |
|
87 | else: | |
|
88 | return app.IsMainLoopRunning() | |
|
89 | ||
|
90 | def start_event_loop_wx(app=None): | |
|
91 | """Start the wx event loop in a consistent manner.""" | |
|
92 | if app is None: | |
|
93 | app = get_app_wx() | |
|
94 | if not is_event_loop_running_wx(app): | |
|
95 | app._in_event_loop = True | |
|
96 | app.MainLoop() | |
|
97 | app._in_event_loop = False | |
|
98 | else: | |
|
99 | app._in_event_loop = True | |
|
100 | ||
|
101 | #----------------------------------------------------------------------------- | |
|
102 | # qt4 | |
|
103 | #----------------------------------------------------------------------------- | |
|
104 | ||
|
105 | def get_app_qt4(*args, **kwargs): | |
|
106 | """Create a new qt4 app or return an existing one.""" | |
|
107 | from PyQt4 import QtGui | |
|
108 | app = QtGui.QApplication.instance() | |
|
109 | if app is None: | |
|
110 | app = QtGui.QApplication(*args, **kwargs) | |
|
111 | return app | |
|
112 | ||
|
113 | def is_event_loop_running_qt4(app=None): | |
|
114 | """Is the qt4 event loop running.""" | |
|
115 | if app is None: | |
|
116 | app = get_app_qt4() | |
|
117 | if hasattr(app, '_in_event_loop'): | |
|
118 | return app._in_event_loop | |
|
119 | else: | |
|
120 | # Does qt4 provide a other way to detect this? | |
|
121 | return False | |
|
122 | ||
|
123 | def start_event_loop_qt4(app=None): | |
|
124 | """Start the qt4 event loop in a consistent manner.""" | |
|
125 | if app is None: | |
|
126 | app = get_app_qt4() | |
|
127 | if not is_event_loop_running_qt4(app): | |
|
128 | app._in_event_loop = True | |
|
129 | app.exec_() | |
|
130 | app._in_event_loop = False | |
|
131 | else: | |
|
132 | app._in_event_loop = True | |
|
133 | ||
|
134 | #----------------------------------------------------------------------------- | |
|
135 | # Tk | |
|
136 | #----------------------------------------------------------------------------- | |
|
137 | ||
|
138 | #----------------------------------------------------------------------------- | |
|
139 | # gtk | |
|
140 | #----------------------------------------------------------------------------- |
General Comments 0
You need to be logged in to leave comments.
Login now