Thanks for your reply - and the patch! I am about to check in an updated
backend_wx which basically contains your changes, with one difference: I have
refactored the code:
drawDC.DrawBitmap(self.bitmap, 0, 0)
into a new function in FigureWX: gui_repaint(). I have done this for several
reasons, but mainly because the same code appears in several places
(FigureWX._onSize() as well as draw_if_interactive(), and it turns out to be
needed for the Wx variant of interactive_demo (which I am also about to check
I have also made a change so that if _DEBUG is set to a value less than 5
(i.e. you are interested in debugging), the system exception hook is replaced
with one which performs a traceback and enters pdb. This seems to work
correctly for me on Linux, and I'll verify Win32 when I get back to work next
week. It's a bit of an ugly hack, and I may rethink it later, but it's very
handy for now, and doesn't intrude when not needed.
> A few questions for John Hunter: I think I need to do
> something like the following: - show() must now
> instantiate any figures already defined and enter the the
> main event loop. ShowOn needs to keep track of this. - I
> need to keep track of the number of figures
> instantiated. I assume that Gcf.destroy() does this. - I
> need to ensure that I do not exit when the last figure is
> destroyed, and therefore need to manage that I may need to
> create a new figure manager if there is none.
I think we should consider redoing this whole segment of the code from
the ground up to make for a cleaner / cross GUI implementation. It
may be that Pearu Peteson <pearu@...20...> gui_thread code is the
way to go for this since that is what is was designed to do (enable
interactive control of WX plots (chaco) from the shell). He has
indicated a willingness to port it to pygtk provided we're willing to
help test his code with the WX and GTK matplotlib backends. However,
this would create a scipy dependency...
I followed the thread form Pearu a few weeks back, although have not gotten
around to trying the gui_thread code. I think there is something similar in
the Python Cookbook for PyGtk (chapter 9.12), which might assist in doing a
I'm not keen on adding a dependency on scipy, but perhaps Pearu would consider
making gui_thread its own module (or allowing us to do so). Checking back
over the mail you sent to me, there are only three files involved. One of the
things which drew me to working on Matplotlib was the small set of
dependencies, and it would be a shame to loose this.
I'm very willing to play with the code he has with the Wx backend - I doubt
that it will require much work on my behalf.
I don't think the current architecture for show, draw_if_interactive,
ShowOn etc, is very elegant or easily understandable, and would be
happy to refactor it for the next release. Ideally we could handle
these two cases across backends
1) defer all drawing until a call to show, which draws and realizes
all pending figures. This should not hang the script, ie,
further drawing commands should be possible.
You'll notice in the code that I checked in that I have been playing with this
in the backend_wx code. While it is possible to exit the mainloop, it seems
that it is not possible to re-enter. It may be that I can do something akin
to the Python Cookbook recepie I mentioned above for wx - it doesn't look too
I have attached the code from the book example below, if you're interested.
2) do drawing with each matplotlib.matlab command for interactive
mode (current implementation in backend_gtk with ShowOn.set(1) at
start of script)
As you noteed, this now works with your fix.
For the most part, I think we have this with the GTK backend, but it
may be necessary to refactor in order to get something that works with
both. I'll think it over and take a look at the WX code to see if I
get any ideas how to proceed.
In the meantime, we should also see if we can get matplotlib to work
with gui_thread -- I'll take a look at this too.
Thanks again for the fix
====== interactive_gtk.py =====
import __builtin__, __main__
import codeop, keyword, gtk, os, re, readline, threading, traceback, signal,
list = 
for item in dir(klass):
if item != "_":
for base in klass.__bases__:
for item in walk_class(base):
if item not in list: list.append(item)
def __init__(self, lokals):
self.locals = lokals
self.completions = keyword.kwlist + \
__builtins__.__dict__.keys() + \
def complete(self, text, state):
if state == 0:
if "." in text:
self.matches = self.attr_matches(text)
self.matches = self.global_matches(text)
def update(self, locs):
self.locals = locs
for key in self.locals.keys():
if not key in self.completions:
def global_matches(self, text):
matches = 
n = len(text)
for word in self.completions:
if word[:n] == text:
def attr_matches(self, text):
m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text)
if not m:
expr, attr = m.group(1, 3)
obj = eval(expr, self.locals)
if str(obj)[1:4] == "gtk":
words = walk_class(obj.__class__)
words = dir(eval(expr, self.locals))
matches = 
n = len(attr)
for word in words:
if word[:n] == attr:
matches.append("%s.%s" % (expr, word))
""" Run a GTK mainloop() in a separate thread. Python commands can be
passed to the
thread, where they will be executed. This is implemented by periodically
passed code using a GTK timeout callback. """
TIMEOUT = 100 # interval in milliseconds between timeouts
self.ready = threading.Condition ()
self.globs = globals ()
self.locs = locals ()
self._kill = 0
self.cmd = '' # current code block
self.new_cmd = None # waiting line of code, or None if none waiting
self.completer = Completer(self.locs)
""" Execute waiting code. Called every timeout period. """
if self._kill: gtk.mainquit()
if self.new_cmd != None:
self.cmd = self.cmd + self.new_cmd
self.new_cmd = None
code = codeop.compile_command(self.cmd[:-1])
self.cmd = ''
exec code, self.globs, self.locs
self.cmd = ''
def feed(self, code):
""" Feed a line of code to the thread. This function will block until
the code is
checked by the GTK thread. Returns true if the thread has executed the
Returns false if deferring execution until complete block is
if code[-1:]!='\n': code = code +'\n' # raw_input strips newline
self.new_cmd = code
self.ready.wait() # Wait until processed in timeout interval
return not self.cmd
""" Kill the thread, returning when it has been shut down. """
# Read user input in a loop and send each line to the interpreter thread
print "SIGNAL:", args
prompt = '>>> '
interpreter = GtkInterpreter()
interpreter.feed("from gtk import *")
if len (sys.argv) > 1:
for file in open(sys.argv).readlines():
print 'Interactive GTK Shell'
command = raw_input(prompt) + '\n' # raw_input strips newlines
prompt = interpreter.feed(command) and '>>> ' or '... '
except (EOFError, KeyboardInterrupt): pass
On Monday 29 December 2003 4:15 pm, John Hunter wrote: