LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (October 2006)Back to main SPSSX-L pageJoin or leave SPSSX-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Thu, 5 Oct 2006 12:40:40 -0500
Reply-To:   "Peck, Jon" <peck@spss.com>
Sender:   "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From:   "Peck, Jon" <peck@spss.com>
Subject:   Re: Dialog Windows and Syntax: Part II
Comments:   To: Philip Papush <kparovoz@YAHOO.COM>
Content-Type:   text/plain; charset="UTF-8"

In my last message, I illustrated a simple way to do gui interaction with the user. I want to point out here that, having done that, you can structure a complex job with many parts easily. The key is to organize your code into one or more modules and then import them and call relevant functions.

The Python import command actually executes the imported module(s). Typically, a module just defines classes and functions, but it can just execute a bunch of code. If, though, you define classes and functions in a module, then you can call their contents and pass parameters in the call.

So, after collecting a parameter, p, say, you might do something like

import module1, module2, ...

result = module1.afunction(param) module1.anotherfunction(result) module2.something()

and so on, until your task is done.

While this little example just skims the surface, the modularization capabilities of Python really facilitate well structured, readable, and maintainable code.

-Jon Peck

Apologies if you have heard too much on this topic, but it's good for you :-)

-----Original Message----- From: Peck, Jon Sent: Thursday, October 05, 2006 12:14 PM To: 'Philip Papush'; SPSSX-L@LISTSERV.UGA.EDU Subject: RE: [SPSSX-L] Dialog Windows and Syntax

Conflating SaxBasic scripting, traditional SPSS syntax, and Python is likely to require a lot of baling wire and produce an unwieldy and unmaintainable solution, if it works at all. SaxBasic scripts run in the frontend context, while the syntax runs in a backend context. These two technologies run asynchronously due, in part, to the need for distributed mode and in part due to their mainly separate concerns.

A better way to do this is to drive the gui parameter gathering from Python directly, dispensing with a separate scripting engine.

This is quite workable, but it requires you to learn a bit about programming a gui tool.

However, for simple situations, you can do something like the following. This example uses wxPython, which is a freely downloadable gui toolkit. (There are a number of other gui toolkits you could use, but I like this one the best.)

begin program. import wx app = wx.PySimpleApp() dialog = wx.TextEntryDialog(None, "Please enter the value for x:", "My Procedure", ".05", style=wx.OK|wx.CANCEL) if dialog.ShowModal() == wx.ID_OK: param = dialog.GetValue() dialog.Destroy() app.Destroy() #use param in your program end program.

This displays a text box with the "Please enter..." text and a space for the user to enter a value with a default value of .05. Then you use the parameter the user entered in your Python program, whether you use it directly or substitute it in SPSS syntax that you submit from the Python code. You can, of course make this example more elaborate, but it is simple to do simple things.

I have simple examples with wxPython also for selecting from a variable list or just displaying a message box with OK/Cancel buttons.

This solution does not have synchronization problems; it looks good, and it is easy to code.

I can send these simple examples off list to anyone who requests them.

HTH, Jon Peck SPSS

-----Original Message----- From: SPSSX(r) Discussion [mailto:SPSSX-L@LISTSERV.UGA.EDU] On Behalf Of Philip Papush Sent: Thursday, October 05, 2006 10:45 AM To: SPSSX-L@LISTSERV.UGA.EDU Subject: [SPSSX-L] Dialog Windows and Syntax

Hi,

Suppose I have a simple dialog window that acceps a value for the parameter x:

Sub Main Begin Dialog UserDialog 400,203 ' %GRID:10,7,1,1 Text 20,21,190,28,"Enter value for x:",.Text1 TextBox 20,63,90,21,.tbVariable OKButton 130,63,90,21 End Dialog Dim dlg As UserDialog Dialog dlg Dim strSyntax As String strSyntax = "x=" & dlg.tbVariable & "." objSpssApp.ExecuteCommands strSyntax, True End Sub

Let's call this code 'Test.sbs'. Now, the challenge for me is to make the following Python block (inside a Syntax program) work:

... BEGIN PROGRAM. import spss spss.Submit("SCRIPT '[the appropriate path]\Test.sbs'. ") print x END PROGRAM. ...

In other words I want to pass the values from a dialog box to Python functions inside a large Syntax program. But this code doesn't work. It calls the script which acceps a value for x, but it doesn't print x. SPSS just gets frozen with label "Running Script...". I suspect that I did not link the Python block and parameter x properly (I have no experience with Sax Basic).

I was suggested the altenative solution. To modify the Script:

Sub Main Begin Dialog UserDialog 400,203 ' %GRID:10,7,1,1 Text 20,21,190,28,"Enter value for x:",.Text1 TextBox 20,63,90,21,.tbVariable OKButton 130,63,90,21 End Dialog Dim dlg As UserDialog Dialog dlg Dim strSyntax As String strX = dlg.tbVariable

strSyntax = "BEGIN PROGRAM." & vbLf & _ "import spss" & vbCr & _ "print " & strX & vbCrLf & _ "END PROGRAM."

objSpssApp.ExecuteCommands strSyntax, True

It works if you have 2-3 lines of Syntax code. But this won't help if you have a large Syntax progam (> 1000 lines) with multiple Python blocks using parameter x. I guess it is possible to include all the commands in quotes inside a script (as shown above) even for such a big program. But there should be a more elegant solution to the problem. Any help/hints will be appreciated.

Thanks

Philip


Back to: Top of message | Previous page | Main SPSSX-L page