[wxPython-users] using wxpython with non-GUI applications
Chris Barker
Chris.Barker at noaa.gov
Wed Jan 9 13:27:06 PST 2008
Mike Burnett wrote:
> I have python code that runs in a non-GUI environment (i.e., command =
> line) for doing complex computations. I would like to display the =
> periodic results of the computations graphically. Wxpython includes the =
> graphical tools I need to display the results, but it is designed for =
> event driven windows applications. Can wxpython be used for my minimal =
> graphics needs,
It may not be ideal, but it's quite usable. You can either:
- if you don't mind the GUI freezing when your code is computing, just =
run it form an event
- put all the computation in another thread
- you can do a simple procedural wx app my initializing a wx.App, then =
bringing up modal dialogs sequentially (modal dialogs have their own =
event loop). You can bring up custom dialogs that have just about =
anything in them. See the enclosed sample.
-Chris
-- =
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (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 pythonw2.4
"""
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 =3D Choices =
self.clb =3D wx.CheckListBox(self, -1, wx.DefaultPosition, wx.Defau=
ltSize, self.Choices)
ok =3D wx.Button(self, wx.ID_OK, 'Ok')
=
sizer =3D 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 =3D []
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 =3D wx.App(False)
## now you can run your script, bringing up various dialogs.
fd =3D wx.FileDialog(None,"Pick a File")
if fd.ShowModal() !=3D wx.ID_OK:
exit(1)
else:
print "You choose the file: ", fd.GetFilename()
md =3D wx.MessageDialog(None, 'Continue?')
if md.ShowModal() !=3D wx.ID_OK:
exit(1)
else:
print "You chose to continue"
scd =3D wx.SingleChoiceDialog(None, 'Pick One',
'A Single Choice Dialog',
['single', 'choice', 'dialog','with','some','ch=
oices'])
if scd.ShowModal() !=3D wx.ID_OK:
exit(1)
else:
print "You chose:", scd.GetStringSelection()
# now lets get some input on the command line:
I =3D raw_input("type something here >>")
print "You typed:", I
myd =3D MyCheckDialog(['check', 'list', 'box', 'another'])
if myd.ShowModal() !=3D wx.ID_OK:
exit(1)
else:
print "You checked:", myd.GetChecked()
More information about the wxpython-users
mailing list