[Pythonmac-SIG] Re: Binary Matplotlib package, working. Need
to keep script running after plotting.
Chris Barker
Chris.Barker at noaa.gov
Tue Mar 8 00:14:07 CET 2005
Louis Pecora wrote:
> You pretty much got it.
OK. I've enclosed a little script that demonstrates calling up some
stock dialogs in a script with wxPython. really the only trick is that
you need to initialize an app first. Look through the wx docs to see
what standard dialogs there are. Plus there is an example of a custom
one. It wouldn't take much to write some quickie function wrappers
around those dialogs if you want to be able to call them with one line
of code.
> Well, EasyDialogs has given me no trouble so far now that I know to run
> it with Pythonw. I never use calls to it and matplotlib at the same
time.
However, there can only be one wxApp per instance of Python, and it can
only be initialized once, so you may not be able to switch back and
forth between matplotlib wx stuff and EasyDialogs. That being said, I
just tested it, and it crashed Python when I closed a matplotlib window too.
Using matplotlib in a procedural script like this will take a little bit
of work. As it stands, the pylab interface expects to set up and deal
with all the GUI stuff itself. If you have a wxApp running already, I
don't think it's going to work. Check out the "embedded in wx" examples
that come with matplotlib, to see how to use it inside an existing wx
app. Also, Matt Newville just posted a message to the matplotlib mailing
list that might be what you need:
Matt Newville wrote:
> Hi Andrea,
>
> The pylab interface is generally the non-Class-based approached,
> suitable for small, procedural scripts. Personally, I'd say
> this is good for interactive use, 'one-time' scripts, and very
> simple uses of matplotlib, but that's just my opinion: it seems
> many people use pylab for more demanding apps.
>
> But: you don't need to import pylab at all if you're creating a
> matplotlib figure in a wxPython (or other) GUI. And, for me at
> least, toggling the grid and interacting with gui Events work
> fine.
>
> If the embedding_in_wx*.py and the docs aren't enough, you may
> find it useful to look at the wxPython plotting widget (MPlot)
> I've been working on. The code is currently at
> http://cars.uchicago.edu/~newville/Epics/Python/MPlot
>
> This provides a simple wxPython Panel (and/or Frame) widget that
> has simple plot() and oplot() methods. This widget responds to
> events for zoom, etc and several of the plot attributes (title,
> labels, line symbol/color,etc) can be altered through the GUI,
> and you can export plot images and/or use the Printer interface.
>
> The documentation is scant but there are a couple examples and a
> README. I'm intending to improve the functionality and docs and
> have at least one other person interested and helping out on
> this. Anyway, feel free to steal from this (that goes for
> anyone else on the list as well!!). Any suggestions for
> improvement would be great.
>
> Cheers,
>
> --Matt
> Thanks for all your feedback.
>
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
-------------- next part --------------
#!/usr/bin/env pythonw
"""
This is a little script that tried to demonstrate a simple "procedural"
program using wxPython. tHe goal is to have a script that runs through a
few questions for the user, poppin up dialogs as it goes, but wittout a
main frame, and all the baggage that usually comes with writing a full,
event drive app.
"""
import wx
from sys import exit
## Here's an example of a custom dialog with no parent
class MyCheckDialog(wx.Dialog):
def __init__(self, Choices):
wx.Dialog.__init__(self, None, -1, 'wxDialog')
self.Choices = Choices
self.clb = wx.CheckListBox(self, -1, wx.DefaultPosition, wx.DefaultSize, self.Choices)
ok = wx.Button(self, wx.ID_OK, 'Ok')
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.clb, 1, wx.EXPAND|wx.ALL, 5)
sizer.Add(ok, 0, wx.ALIGN_RIGHT|wx.ALL^wx.TOP, 5)
self.SetSizer(sizer)
#self.Fit()
self.Center() # make it come up on the center of the screen
def GetChecked(self):
Checked = []
for (index, item) in enumerate(self.Choices):
if self.clb.IsChecked(index):
Checked.append(item)
return Checked
# You could put some code here, to run before initializing wx.
# you need to start by initializing a wxApp
app = wx.PySimpleApp()
## now you can run your script, bringing up various dialogs.
fd = wx.FileDialog(None,"Pick a File")
if fd.ShowModal() != wx.ID_OK:
exit(1)
else:
print "You choose the file: ", fd.GetFilename()
md = wx.MessageDialog(None, 'Continue?')
if md.ShowModal() != wx.ID_OK:
exit(1)
else:
print "You chose to continue"
scd = wx.SingleChoiceDialog(None, 'Pick One',
'A Single Choice Dialog',
['single', 'choice', 'dialog','with','some','choices'])
if scd.ShowModal() != wx.ID_OK:
exit(1)
else:
print "You chose:", scd.GetStringSelection()
myd = MyCheckDialog(['check', 'list', 'box', 'another'])
if myd.ShowModal() != wx.ID_OK:
exit(1)
else:
print "You checked:", myd.GetChecked()
More information about the Pythonmac-SIG
mailing list