Show More
@@ -0,0 +1,93 b'' | |||||
|
1 | # encoding: utf-8 | |||
|
2 | ||||
|
3 | """ | |||
|
4 | Enable pyglet to be used interacive by setting PyOS_InputHook. | |||
|
5 | ||||
|
6 | Authors: Nicolas P. Rougier | |||
|
7 | """ | |||
|
8 | ||||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | # Copyright (C) 2008-2009 The IPython Development Team | |||
|
11 | # | |||
|
12 | # Distributed under the terms of the BSD License. The full license is in | |||
|
13 | # the file COPYING, distributed as part of this software. | |||
|
14 | #----------------------------------------------------------------------------- | |||
|
15 | ||||
|
16 | #----------------------------------------------------------------------------- | |||
|
17 | # Imports | |||
|
18 | #----------------------------------------------------------------------------- | |||
|
19 | ||||
|
20 | import os | |||
|
21 | import signal | |||
|
22 | import sys | |||
|
23 | import time | |||
|
24 | from timeit import default_timer as clock | |||
|
25 | import pyglet | |||
|
26 | ||||
|
27 | if os.name == 'posix': | |||
|
28 | import select | |||
|
29 | elif sys.platform == 'win32': | |||
|
30 | import msvcrt | |||
|
31 | ||||
|
32 | #----------------------------------------------------------------------------- | |||
|
33 | # Code | |||
|
34 | #----------------------------------------------------------------------------- | |||
|
35 | ||||
|
36 | def stdin_ready(): | |||
|
37 | if os.name == 'posix': | |||
|
38 | infds, outfds, erfds = select.select([sys.stdin],[],[],0) | |||
|
39 | if infds: | |||
|
40 | return True | |||
|
41 | else: | |||
|
42 | return False | |||
|
43 | elif sys.platform == 'win32': | |||
|
44 | return msvcrt.kbhit() | |||
|
45 | ||||
|
46 | ||||
|
47 | def inputhook_pyglet(): | |||
|
48 | """Run the pyglet event loop by processing pending events only. | |||
|
49 | ||||
|
50 | This keeps processing pending events until stdin is ready. After | |||
|
51 | processing all pending events, a call to time.sleep is inserted. This is | |||
|
52 | needed, otherwise, CPU usage is at 100%. This sleep time should be tuned | |||
|
53 | though for best performance. | |||
|
54 | """ | |||
|
55 | # We need to protect against a user pressing Control-C when IPython is | |||
|
56 | # idle and this is running. We trap KeyboardInterrupt and pass. | |||
|
57 | try: | |||
|
58 | t = clock() | |||
|
59 | while not stdin_ready(): | |||
|
60 | pyglet.clock.tick() | |||
|
61 | for window in pyglet.app.windows: | |||
|
62 | window.switch_to() | |||
|
63 | window.dispatch_events() | |||
|
64 | window.dispatch_event('on_draw') | |||
|
65 | window.flip() | |||
|
66 | ||||
|
67 | # We need to sleep at this point to keep the idle CPU load | |||
|
68 | # low. However, if sleep to long, GUI response is poor. As | |||
|
69 | # a compromise, we watch how often GUI events are being processed | |||
|
70 | # and switch between a short and long sleep time. Here are some | |||
|
71 | # stats useful in helping to tune this. | |||
|
72 | # time CPU load | |||
|
73 | # 0.001 13% | |||
|
74 | # 0.005 3% | |||
|
75 | # 0.01 1.5% | |||
|
76 | # 0.05 0.5% | |||
|
77 | used_time = clock() - t | |||
|
78 | if used_time > 5*60.0: | |||
|
79 | # print 'Sleep for 5 s' # dbg | |||
|
80 | time.sleep(5.0) | |||
|
81 | elif used_time > 10.0: | |||
|
82 | # print 'Sleep for 1 s' # dbg | |||
|
83 | time.sleep(1.0) | |||
|
84 | elif used_time > 0.1: | |||
|
85 | # Few GUI events coming in, so we can sleep longer | |||
|
86 | # print 'Sleep for 0.05 s' # dbg | |||
|
87 | time.sleep(0.05) | |||
|
88 | else: | |||
|
89 | # Many GUI events coming in, so sleep only very little | |||
|
90 | time.sleep(0.001) | |||
|
91 | except KeyboardInterrupt: | |||
|
92 | pass | |||
|
93 | return 0 |
@@ -0,0 +1,29 b'' | |||||
|
1 | #!/usr/bin/env python | |||
|
2 | """Simple pyglet example to manually test event loop integration. | |||
|
3 | ||||
|
4 | This is meant to run tests manually in ipython as: | |||
|
5 | ||||
|
6 | In [5]: %gui pyglet | |||
|
7 | ||||
|
8 | In [6]: %run gui-pyglet.py | |||
|
9 | """ | |||
|
10 | ||||
|
11 | import pyglet | |||
|
12 | ||||
|
13 | ||||
|
14 | window = pyglet.window.Window() | |||
|
15 | label = pyglet.text.Label('Hello, world', | |||
|
16 | font_name='Times New Roman', | |||
|
17 | font_size=36, | |||
|
18 | x=window.width//2, y=window.height//2, | |||
|
19 | anchor_x='center', anchor_y='center') | |||
|
20 | @window.event | |||
|
21 | def on_draw(): | |||
|
22 | window.clear() | |||
|
23 | label.draw() | |||
|
24 | ||||
|
25 | try: | |||
|
26 | from IPython.lib.inputhook import enable_pyglet | |||
|
27 | enable_pyglet() | |||
|
28 | except ImportError: | |||
|
29 | pyglet.app.run() |
General Comments 0
You need to be logged in to leave comments.
Login now