More Python

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

More Python

Tim AT Home
okay Python gurus -
 
My next step is to display a list of questions with default answers displayed - then probe
the user as to whether they need to be changed and then if so - ask each question in turn
and get the users input on new values - in real time, pausing the python execution till
I get the correct answers
 
My initial attempt to obtain user input
 
myorI = raw_input('Portrait=1 : Landscape=2 <<< ENTER CHOICE')
 
does not seem to do anything - no prompt is 'raised' and so the program just hangs there.
 
Hints?
 
 
 

Thanks!

Tim

****************************

Notice: This e-mail and any attachments may contain confidential and privileged information.  If you are not the intended recipient, please notify the sender immediately by return e-mail, do not use the information, delete this e-mail and destroy any copies.  Any dissemination or use of this information by a person other than the intended recipient is unauthorized and may be illegal.  Email transmissions cannot be guaranteed to be secure or error free. The sender therefore does not accept any liability for errors or omissions in the contents of this message that arise as a result of email transmissions.

 
Reply | Threaded
Open this post in threaded view
|

Re: More Python

Peck, Jon

The Python raw_input approach is not going to work.  The Python/SPSS interaction is not architected for this sort of thing.  You might be able to rig up something by spawning a Python shell, but I wouldn't recommend that approach.

 

I can think of three ways to go about this sort of thing.  Perhaps one of these will meet your requirements.  I'll list them from easiest to hardest.

 

  1. If the number of questions is modest and static, you could create a custom dialog box with the Custom Dialog Builder in 17 and pose the questions as controls in the dialog.  You could put the dialog on the Custom menu or anywhere else in the SPSS menu system.  For example, a Combo Box could have the question as label and the possible answers as the dropdown contents.  Text controls, check boxes, or radio buttons could also work here.  These dialogs are very easy to create, and you can have subdialogs, but this approach probably would be awkward if there are a lot of questions.

 

  1. Use Python in external mode, where SPSS is running inside your Python program.  In this mode, the raw_input approach would work, but the SPSS user interface would never appear.  You could collect the information and feed it to SPSS in your program as appropriate, but the user would not see SPSS or SPSS output (unless you launched another visible SPSS session).

 

  1. Use a third-party library such as wxPython (http://www.wxpython.org/download.php ) to create pop-up boxes posing the questions and collecting the answers.  wxPython is a comprehensive GUI development environment, so you can build practically anything.  However, it's also rather complex.

 

The saving thing here is that wxPython comes with some very simple, prebuilt types of dialogs, so it is quite easy to put up a series of those, collect the responses, and get on with life.  These could be run as a separate program or embedded within the SPSS session or used in external mode.

 

Here are some wxPython examples:  The first one just puts up an alert with Yes/No responses possible.

BEGIN PROGRAM.

# wxPython Message Dialog example

 

import wx

 

app = wx.PySimpleApp()

dlg = wx.MessageDialog(None, "Ok to reformat hard disk?", "Important Question",

                       wx.YES_NO | wx.ICON_QUESTION)

ret = dlg.ShowModal()

if ret == wx.ID_YES:

    # put Yes action code here

    print "You said yes"

else:

    # put No action code here

    print "You said No"

 

dlg.Destroy()

app.Destroy()

END PROGRAM.

 

Here is one that is more realistic.  It opens a dataset and puts up a choice list – in this case it is a list of the scale variables in the dataset.  The user picks one items in the list, presses OK, and the program does something with it.

 

BEGIN PROGRAM.

# wxPython variable picker

 

import wx, spss, spssaux

 

spssaux.OpenDataFile("c:/spss17/samples/english/cars.sav")

vardict = spssaux.VariableDict(variableLevel=['scale'])

choicelist = vardict.variables

 

app = wx.PySimpleApp()

dlg = wx.SingleChoiceDialog(None, "Select a variable", "&Variables", choicelist)

if dlg.ShowModal() == wx.ID_OK:

    var = dlg.GetStringSelection()

else:

    var = None

   

dlg.Destroy()

app.Destroy()

 

if var:

    spss.Submit("DESCRIPTIVES " + str(var))

END PROGRAM.

 

There is also a simple input box that just lets the user type some text.

 

These examples all just put up one dialog box and then close down the gui, but you could equally put up a series of these.

 

I have not tried wxPython with v17.  With this approach, there are two gui's active.  Since they would be in separate processes, I expect that they would get along well enough, but it would be a good idea to experiment a little to make sure.  When Python was running within the SPSS process, this approach was a little fragile, but I expect that that is no longer the case with v17.

 

HTH,

Jon Peck

 

 


From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Tim Hennigar
Sent: Friday, June 05, 2009 3:22 PM
To: [hidden email]
Subject: [SPSSX-L] More Python

 

okay Python gurus -

 

My next step is to display a list of questions with default answers displayed - then probe

the user as to whether they need to be changed and then if so - ask each question in turn

and get the users input on new values - in real time, pausing the python execution till

I get the correct answers

 

My initial attempt to obtain user input

 

myorI = raw_input('Portrait=1 : Landscape=2 <<< ENTER CHOICE')

 

does not seem to do anything - no prompt is 'raised' and so the program just hangs there.

 

Hints?

 

 

 

Thanks!

Tim

****************************

Notice: This e-mail and any attachments may contain confidential and privileged information.  If you are not the intended recipient, please notify the sender immediately by return e-mail, do not use the information, delete this e-mail and destroy any copies.  Any dissemination or use of this information by a person other than the intended recipient is unauthorized and may be illegal.  Email transmissions cannot be guaranteed to be secure or error free. The sender therefore does not accept any liability for errors or omissions in the contents of this message that arise as a result of email transmissions.

 

Reply | Threaded
Open this post in threaded view
|

Re: More Python

Albert-Jan Roskam
In reply to this post by Tim AT Home
Hi Jon,

In his Python book, Mark Summerfield isn't very positive about WXPython for the reason you mentioned. He discusses Tkinter, because it's part of the Python basic library, but that module, too, has drawbacks. Isn't PyQt becoming the 'de facto' standard nowadays? (Summerfield barely mentions this package, perhaps out of modesty, because he's the main developer). Or how about PyGTK, or, for simpler projects, SimpleGUI?

Moreover, wouldn't it be easier to create a web survey (with e.g. Joomla or Limesurvey) to harvest the data, and approach the database using SPSS/PASW? That seems simpler to me than writing an entire gui. Then again, it might also be nice to just write a flashy GUI, for the heck of it.

Cheers,
Albert-Jan


--- On Sat, 6/6/09, Peck, Jon <[hidden email]> wrote:

> From: Peck, Jon <[hidden email]>
> Subject: Re: More Python
> To: [hidden email]
> Date: Saturday, June 6, 2009, 11:37 PM
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> The Python raw_input approach is not
> going to work. � The
> Python/SPSS interaction is not architected for this sort of
> thing. � You
> might be able to rig up something by spawning a Python
> shell, but I wouldn't
> recommend that approach.
>
>  �
>
> I can think of three ways to go about
> this sort of thing.�  Perhaps
> one of these will meet your requirements. � I'll
> list them from easiest to
> hardest.
>
>  �
>
>
>  If
> the number of
>      questions is modest and static, you could create a
> custom dialog box with
>      the Custom Dialog Builder in 17 and pose the questions
> as controls in the
>      dialog.�  You could put the dialog on the Custom
> menu or anywhere else
>      in the SPSS menu system.�  For example, a Combo
> Box could have the
>      question as label and the possible answers as the
> dropdown contents. � Text
>      controls, check boxes, or radio buttons could also
> work here. � These dialogs
>      are very easy to create, and you can have subdialogs,
> but this approach
>      probably would be awkward if there are a lot of
> questions.
>
>
>  �
>
>
>  Use
> Python in
>      external mode, where SPSS is running inside your
> Python program. � In
>      this mode, the raw_input approach would work, but the
> SPSS user interface
>      would never appear. � You could collect the
> information and feed it to
>      SPSS in your program as appropriate, but the user
> would not see SPSS or
>      SPSS output (unless you launched another visible SPSS
> session).
>
>
>  �
>
>
>  Use
> a third-party
>      library such as wxPython (http://www.wxpython.org/download.php
>      ) to create pop-up boxes posing the questions and
> collecting the answers.�
>      wxPython is a comprehensive GUI development
> environment, so you can build
>      practically anything. � However, it's also
> rather complex.
>
>
>  �
>
> The saving thing here is that
> wxPython comes with some
> very simple, prebuilt types of dialogs, so it is quite easy
> to put up a series
> of those, collect the responses, and get on with
> life.�  These could be run
> as a separate program or embedded within the SPSS session
> or used in external
> mode.
>
>  �
>
> Here are some wxPython examples:�
> The first one just puts up an
> alert with Yes/No responses possible.
>
> BEGIN PROGRAM.
>
> # wxPython Message Dialog
> example
>
>  �
>
> import wx
>
>  �
>
> app = wx.PySimpleApp()
>
>
> dlg = wx.MessageDialog(None, "Ok to
> reformat hard disk?",
> "Important Question",
>
> � � � � � � � � � � � � � � � � � � � � � �
> wx.YES_NO |
> wx.ICON_QUESTION)
>
> ret = dlg.ShowModal()
>
> if ret == wx.ID_YES:
>
> � � �  # put Yes action code
> here
>
> � � �  print "You said
> yes"
>
> else:
>
> � � �  # put No action code
> here
>
> � � �  print "You said
> No"
>
>  �
>
> dlg.Destroy()
>
> app.Destroy()
>
> END PROGRAM.
>
>  �
>
> Here is one that is more realistic.
> � It opens a dataset and puts
> up a choice list – in this case it is a list of the
> scale variables in
> the dataset.�  The user picks one items in the list,
> presses OK, and the program
> does something with it.
>
>  �
>
> BEGIN PROGRAM.
>
> # wxPython variable
> picker
>
>  �
>
> import wx, spss,
> spssaux
>
>  �
>
> spssaux.OpenDataFile("c:/spss17/samples/english/cars.sav")
>
>
> vardict =
> spssaux.VariableDict(variableLevel=['scale'])
>
>
> choicelist =
> vardict.variables
>
>  �
>
> app = wx.PySimpleApp()
>
>
> dlg = wx.SingleChoiceDialog(None,
> "Select a variable", "&Variables",
> choicelist)
>
> if dlg.ShowModal() ==
> wx.ID_OK:
>
> � � �  var = dlg.GetStringSelection()
>
> else:
>
> � � �  var =
> None
>
> � � �
>
>
> dlg.Destroy()
>
> app.Destroy()
>
>  �
>
> if var:
>
> � � �
> spss.Submit("DESCRIPTIVES " +
> str(var))
>
> END PROGRAM.
>
>  �
>
> There is also a simple input box that
> just lets the user type some
> text.
>
>  �
>
> These examples all just put up one
> dialog box and then close down the
> gui, but you could equally put up a series of
> these.
>
>  �
>
> I have not tried wxPython with v17.
> � With this approach, there are
> two gui's active. � Since they would be in separate
> processes, I expect
> that they would get along well enough, but it would be a
> good idea to
> experiment a little to make sure.�  When Python was
> running within the SPSS
> process, this approach was a little fragile, but I expect
> that that is no
> longer the case with v17.
>
>  �
>
> HTH,
>
> Jon Peck
>
>  �
>
>  �
>
>
>
>
>
>
>
>
>
> From: SPSSX(r)
> Discussion [mailto:[hidden email]] On
> Behalf Of Tim Hennigar
>
> Sent: Friday,
> June 05, 2009 3:22
> PM
>
> To:
> [hidden email]
>
> Subject:
> [SPSSX-L] More Python
>
>
>
>  �
>
>
>
> okay Python
> gurus -
>
>
>
>
>
> �
>
>
>
>
>
> My next step is
> to display a list of questions with default
> answers displayed - then probe
>
>
>
>
>
> the user as to
> whether they need to be changed and then if
> so - ask each question in turn
>
>
>
>
>
> and get the
> users input on new values - in real time,
> pausing the python execution till
>
>
>
>
>
> I get the
> correct answers
>
>
>
>
>
> �
>
>
>
>
>
> My initial
> attempt to obtain user input
>
>
>
>
>
> �
>
>
>
>
>
> myorI =
> raw_input('Portrait=1 : Landscape=2 <<<
> ENTER CHOICE')
>
>
>
>
>
> �
>
>
>
>
>
> does not seem to
> do anything - no prompt is 'raised' and so
> the program just hangs there.
>
>
>
>
>
> �
>
>
>
>
>
> Hints?
>
>
>
>
>
>
> �
>
>
>
>
>
> �
>
>
>
>
>
> �
>
>
>
> Thanks!
>
> Tim
>
> ****************************
>
> Notice:
> This e-mail and any attachments may contain confidential
> and privileged
> information.�  If you are not the intended recipient,
> please notify the
> sender immediately by return e-mail, do not use the
> information, delete this
> e-mail and destroy any copies.�  Any dissemination or
> use of this
> information by a person other than the intended recipient
> is unauthorized and
> may be illegal.�  Email transmissions cannot be
> guaranteed to be secure or
> error free. The sender therefore does not accept any
> liability for errors or
> omissions in the contents of this message that arise as a
> result of email
> transmissions.
>
>
>
> �
>
>
>
>
>
>
>
>
>

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: More Python

Peck, Jon
-----Original Message-----
From: Albert-jan Roskam [mailto:[hidden email]]
Sent: Sunday, June 07, 2009 3:25 AM
To: [hidden email]; Peck, Jon
Subject: Re: More Python


Hi Jon,

In his Python book, Mark Summerfield isn't very positive about WXPython for the reason you mentioned. He discusses Tkinter, because it's part of the Python basic library, but that module, too, has drawbacks. Isn't PyQt becoming the 'de facto' standard nowadays? (Summerfield barely mentions this package, perhaps out of modesty, because he's the main developer). Or how about PyGTK, or, for simpler projects, SimpleGUI?
[>>>Peck, Jon] There is a lot of debate in the Python community about alternative guis.  wxPython is certainly one of the top choices, but PyGTK also has its fans. PyQt, if I recall correctly, has had licensing issues, but these are apparently being resolved.
Easygui is probably the simplest to use, but it is also very limited.
TKinter comes with Python, but its appearance is mediocre, and there has been a move to get rid of it for years.  There is a new version that is apparently improved, but it is not the one distributed with Python.  And it is no easier to use than wxPython or the others.
wxGlade can be used to build guis.

Moreover, wouldn't it be easier to create a web survey (with e.g. Joomla or Limesurvey) to harvest the data, and approach the database using SPSS/PASW? That seems simpler to me than writing an entire gui. Then again, it might also be nice to just write a flashy GUI, for the heck of it.
[>>>Peck, Jon] I wouldn't suggest building a whole survey package from scratch with these tools.  If that's the goal, there are plenty of packages around.  Or, even, a few VBA forms would be easy to throw together.  It all depends on the purpose.

Cheers,
Albert-Jan


--- On Sat, 6/6/09, Peck, Jon <[hidden email]> wrote:

> From: Peck, Jon <[hidden email]>
> Subject: Re: More Python
> To: [hidden email]
> Date: Saturday, June 6, 2009, 11:37 PM
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> The Python raw_input approach is not
> going to work.  The
> Python/SPSS interaction is not architected for this sort of
> thing.  You
> might be able to rig up something by spawning a Python
> shell, but I wouldn't
> recommend that approach.
>
>
>
> I can think of three ways to go about
> this sort of thing.  Perhaps
> one of these will meet your requirements.  I'll
> list them from easiest to
> hardest.
>
>
>
>
>  If
> the number of
>      questions is modest and static, you could create a
> custom dialog box with
>      the Custom Dialog Builder in 17 and pose the questions
> as controls in the
>      dialog.  You could put the dialog on the Custom
> menu or anywhere else
>      in the SPSS menu system.  For example, a Combo
> Box could have the
>      question as label and the possible answers as the
> dropdown contents.  Text
>      controls, check boxes, or radio buttons could also
> work here.  These dialogs
>      are very easy to create, and you can have subdialogs,
> but this approach
>      probably would be awkward if there are a lot of
> questions.
>
>
>
>
>
>  Use
> Python in
>      external mode, where SPSS is running inside your
> Python program.  In
>      this mode, the raw_input approach would work, but the
> SPSS user interface
>      would never appear.  You could collect the
> information and feed it to
>      SPSS in your program as appropriate, but the user
> would not see SPSS or
>      SPSS output (unless you launched another visible SPSS
> session).
>
>
>
>
>
>  Use
> a third-party
>      library such as wxPython (http://www.wxpython.org/download.php
>      ) to create pop-up boxes posing the questions and
> collecting the answers.
>      wxPython is a comprehensive GUI development
> environment, so you can build
>      practically anything.  However, it's also
> rather complex.
>
>
>
>
> The saving thing here is that
> wxPython comes with some
> very simple, prebuilt types of dialogs, so it is quite easy
> to put up a series
> of those, collect the responses, and get on with
> life.  These could be run
> as a separate program or embedded within the SPSS session
> or used in external
> mode.
>
>
>
> Here are some wxPython examples:
> The first one just puts up an
> alert with Yes/No responses possible.
>
> BEGIN PROGRAM.
>
> # wxPython Message Dialog
> example
>
>
>
> import wx
>
>
>
> app = wx.PySimpleApp()
>
>
> dlg = wx.MessageDialog(None, "Ok to
> reformat hard disk?",
> "Important Question",
>
>
> wx.YES_NO |
> wx.ICON_QUESTION)
>
> ret = dlg.ShowModal()
>
> if ret == wx.ID_YES:
>
>     # put Yes action code
> here
>
>     print "You said
> yes"
>
> else:
>
>     # put No action code
> here
>
>     print "You said
> No"
>
>
>
> dlg.Destroy()
>
> app.Destroy()
>
> END PROGRAM.
>
>
>
> Here is one that is more realistic.
>  It opens a dataset and puts
> up a choice list - in this case it is a list of the
> scale variables in
> the dataset.  The user picks one items in the list,
> presses OK, and the program
> does something with it.
>
>
>
> BEGIN PROGRAM.
>
> # wxPython variable
> picker
>
>
>
> import wx, spss,
> spssaux
>
>
>
> spssaux.OpenDataFile("c:/spss17/samples/english/cars.sav")
>
>
> vardict =
> spssaux.VariableDict(variableLevel=['scale'])
>
>
> choicelist =
> vardict.variables
>
>
>
> app = wx.PySimpleApp()
>
>
> dlg = wx.SingleChoiceDialog(None,
> "Select a variable", "&Variables",
> choicelist)
>
> if dlg.ShowModal() ==
> wx.ID_OK:
>
>     var = dlg.GetStringSelection()
>
> else:
>
>     var =
> None
>
>
>
>
> dlg.Destroy()
>
> app.Destroy()
>
>
>
> if var:
>
>
> spss.Submit("DESCRIPTIVES " +
> str(var))
>
> END PROGRAM.
>
>
>
> There is also a simple input box that
> just lets the user type some
> text.
>
>
>
> These examples all just put up one
> dialog box and then close down the
> gui, but you could equally put up a series of
> these.
>
>
>
> I have not tried wxPython with v17.
>  With this approach, there are
> two gui's active.  Since they would be in separate
> processes, I expect
> that they would get along well enough, but it would be a
> good idea to
> experiment a little to make sure.  When Python was
> running within the SPSS
> process, this approach was a little fragile, but I expect
> that that is no
> longer the case with v17.
>
>
>
> HTH,
>
> Jon Peck
>
>
>
>
>
>
>
>
>
>
>
>
>
> From: SPSSX(r)
> Discussion [mailto:[hidden email]] On
> Behalf Of Tim Hennigar
>
> Sent: Friday,
> June 05, 2009 3:22
> PM
>
> To:
> [hidden email]
>
> Subject:
> [SPSSX-L] More Python
>
>
>
>
>
>
>
> okay Python
> gurus -
>
>
>
>
>
>
>
>
>
>
>
> My next step is
> to display a list of questions with default
> answers displayed - then probe
>
>
>
>
>
> the user as to
> whether they need to be changed and then if
> so - ask each question in turn
>
>
>
>
>
> and get the
> users input on new values - in real time,
> pausing the python execution till
>
>
>
>
>
> I get the
> correct answers
>
>
>
>
>
>
>
>
>
>
>
> My initial
> attempt to obtain user input
>
>
>
>
>
>
>
>
>
>
>
> myorI =
> raw_input('Portrait=1 : Landscape=2 <<<
> ENTER CHOICE')
>
>
>
>
>
>
>
>
>
>
>
> does not seem to
> do anything - no prompt is 'raised' and so
> the program just hangs there.
>
>
>
>
>
>
>
>
>
>
>
> Hints?
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Thanks!
>
> Tim
>
> ****************************
>
> Notice:
> This e-mail and any attachments may contain confidential
> and privileged
> information.  If you are not the intended recipient,
> please notify the
> sender immediately by return e-mail, do not use the
> information, delete this
> e-mail and destroy any copies.  Any dissemination or
> use of this
> information by a person other than the intended recipient
> is unauthorized and
> may be illegal.  Email transmissions cannot be
> guaranteed to be secure or
> error free. The sender therefore does not accept any
> liability for errors or
> omissions in the contents of this message that arise as a
> result of email
> transmissions.
>
>
>
>
>
>
>
>
>
>
>
>
>

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD