Show More
@@ -0,0 +1,35 b'' | |||
|
1 | #!/usr/bin/env python | |
|
2 | """Simple GTK example to manually test event loop integration. | |
|
3 | ||
|
4 | This is meant to run tests manually in ipython as: | |
|
5 | ||
|
6 | In [5]: %gui gtk | |
|
7 | ||
|
8 | In [6]: %run gui-gtk.py | |
|
9 | """ | |
|
10 | ||
|
11 | ||
|
12 | import pygtk | |
|
13 | pygtk.require('2.0') | |
|
14 | import gtk | |
|
15 | ||
|
16 | ||
|
17 | def hello_world(wigdet, data=None): | |
|
18 | print "Hello World" | |
|
19 | ||
|
20 | window = gtk.Window(gtk.WINDOW_TOPLEVEL) | |
|
21 | button = gtk.Button("Hello World") | |
|
22 | button.connect("clicked", hello_world, None) | |
|
23 | ||
|
24 | window.add(self.button) | |
|
25 | button.show() | |
|
26 | window.show() | |
|
27 | ||
|
28 | try: | |
|
29 | from IPython.lib.inputhook import appstart_gtk | |
|
30 | appstart_gtk() | |
|
31 | except ImportError: | |
|
32 | gtk.main() | |
|
33 | ||
|
34 | ||
|
35 |
@@ -0,0 +1,32 b'' | |||
|
1 | #!/usr/bin/env python | |
|
2 | """Simple Tk example to manually test event loop integration. | |
|
3 | ||
|
4 | This is meant to run tests manually in ipython as: | |
|
5 | ||
|
6 | In [5]: %gui tk | |
|
7 | ||
|
8 | In [6]: %run gui-tk.py | |
|
9 | """ | |
|
10 | ||
|
11 | from Tkinter import * | |
|
12 | ||
|
13 | class MyApp: | |
|
14 | ||
|
15 | def __init__(self, root): | |
|
16 | frame = Frame(root) | |
|
17 | frame.pack() | |
|
18 | ||
|
19 | self.button = Button(frame, text="Hello", command=self.hello_world) | |
|
20 | self.button.pack(side=LEFT) | |
|
21 | ||
|
22 | def hello_world(self): | |
|
23 | print "Hello World!" | |
|
24 | ||
|
25 | root = Tk() | |
|
26 | ||
|
27 | app = MyApp(root) | |
|
28 | ||
|
29 | try: | |
|
30 | from IPython import appstart_tk; appstart_tk(root) | |
|
31 | except ImportError: | |
|
32 | root.mainloop() |
@@ -70,7 +70,8 b' from IPython.lib import (' | |||
|
70 | 70 | enable_tk, disable_tk, |
|
71 | 71 | set_inputhook, clear_inputhook, |
|
72 | 72 | current_gui, spin, |
|
73 | appstart_qt4, appstart_wx | |
|
73 | appstart_qt4, appstart_wx, | |
|
74 | appstart_gtk, appstart_tk | |
|
74 | 75 | ) |
|
75 | 76 | |
|
76 | 77 | # Release data |
@@ -22,7 +22,8 b' from IPython.lib.inputhook import (' | |||
|
22 | 22 | enable_tk, disable_tk, |
|
23 | 23 | set_inputhook, clear_inputhook, |
|
24 | 24 | current_gui, spin, |
|
25 | appstart_qt4, appstart_wx | |
|
25 | appstart_qt4, appstart_wx, | |
|
26 | appstart_gtk, appstart_tk | |
|
26 | 27 | ) |
|
27 | 28 | |
|
28 | 29 | #----------------------------------------------------------------------------- |
@@ -114,6 +114,56 b' def appstart_wx(app):' | |||
|
114 | 114 | app.MainLoop() |
|
115 | 115 | |
|
116 | 116 | |
|
117 | def appstart_tk(app): | |
|
118 | """Start the tk event loop in a way that plays with IPython. | |
|
119 | ||
|
120 | When a tk app is run interactively in IPython, the event loop should | |
|
121 | not be started. This function checks to see if IPython's tk integration | |
|
122 | is activated and if so, it passes. If not, it will call the | |
|
123 | :meth:`mainloop` method of the tk object passed to this method. | |
|
124 | ||
|
125 | This function should be used by users who want their tk scripts to work | |
|
126 | both at the command line and in IPython. These users should put the | |
|
127 | following logic at the bottom on their script, after they create a | |
|
128 | :class:`Tk` instance (called ``app`` here):: | |
|
129 | ||
|
130 | try: | |
|
131 | from IPython.lib.inputhook import appstart_tk | |
|
132 | appstart_tk(app) | |
|
133 | except ImportError: | |
|
134 | app.mainloop() | |
|
135 | """ | |
|
136 | if app is not None: | |
|
137 | if current_gui() == GUI_TK: | |
|
138 | pass | |
|
139 | else: | |
|
140 | app.mainloop() | |
|
141 | ||
|
142 | def appstart_gtk(): | |
|
143 | """Start the gtk event loop in a way that plays with IPython. | |
|
144 | ||
|
145 | When a gtk app is run interactively in IPython, the event loop should | |
|
146 | not be started. This function checks to see if IPython's gtk integration | |
|
147 | is activated and if so, it passes. If not, it will call | |
|
148 | :func:`gtk.main`. Unlike the other appstart implementations, this does | |
|
149 | not take an ``app`` argument. | |
|
150 | ||
|
151 | This function should be used by users who want their gtk scripts to work | |
|
152 | both at the command line and in IPython. These users should put the | |
|
153 | following logic at the bottom on their script:: | |
|
154 | ||
|
155 | try: | |
|
156 | from IPython.lib.inputhook import appstart_gtk | |
|
157 | appstart_gtk() | |
|
158 | except ImportError: | |
|
159 | gtk.main() | |
|
160 | """ | |
|
161 | import gtk | |
|
162 | if current_gui() == GUI_GTK: | |
|
163 | pass | |
|
164 | else: | |
|
165 | gtk.main() | |
|
166 | ||
|
117 | 167 | #----------------------------------------------------------------------------- |
|
118 | 168 | # Main InputHookManager class |
|
119 | 169 | #----------------------------------------------------------------------------- |
@@ -229,7 +279,12 b' class InputHookManager(object):' | |||
|
229 | 279 | This is for internal IPython use only and user code should not call this. |
|
230 | 280 | Instead, they should issue the raw GUI calls themselves. |
|
231 | 281 | """ |
|
232 |
|
|
|
282 | import gtk | |
|
283 | gtk.gdk.threads_enter() | |
|
284 | while gtk.events_pending(): | |
|
285 | gtk.main_iteration(False) | |
|
286 | gtk.gdk.flush() | |
|
287 | gtk.gdk.threads_leave() | |
|
233 | 288 | |
|
234 | 289 | def _spin_tk(self): |
|
235 | 290 | """Process all pending events in the tk event loop. |
General Comments 0
You need to be logged in to leave comments.
Login now