Re: Dialog Windows and Syntax: Part II

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

Re: Dialog Windows and Syntax: Part II

Peck, Jon
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'; [hidden email]
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:[hidden email]] On Behalf Of Philip Papush
Sent: Thursday, October 05, 2006 10:45 AM
To: [hidden email]
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
Reply | Threaded
Open this post in threaded view
|

Re: Dialog Windows and Syntax: Part II

Philip Papush
Jon,

Thank you very much for your answer. You hit the bulleye. Exactly what I
was looking for. I ran your example in Syntax and it passes the parameter
value to the program with no problem. I downloaded wxPython demos with
codes. I was amaized what nice and flexible dialogs can be created with
relatively little effort. Thank you again.

P.S. Are you going to SPSS Directions this November?

Regards,

Philip
Reply | Threaded
Open this post in threaded view
|

Re: Dialog Windows and Syntax: Part II

Peck, Jon
Glad this worked for you.

I will be at Directions in November talking about new programmability features in SPSS 15 and helping with the post-conference training SPSS Programming Facilities and hanging around the demo area.

-Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Philip Papush
Sent: Monday, October 09, 2006 7:47 AM
To: [hidden email]
Subject: Re: [SPSSX-L] Dialog Windows and Syntax: Part II

Jon,

Thank you very much for your answer. You hit the bulleye. Exactly what I
was looking for. I ran your example in Syntax and it passes the parameter
value to the program with no problem. I downloaded wxPython demos with
codes. I was amaized what nice and flexible dialogs can be created with
relatively little effort. Thank you again.

P.S. Are you going to SPSS Directions this November?

Regards,

Philip
Reply | Threaded
Open this post in threaded view
|

Re: Dialog Windows and Syntax: Part II

Philip Papush
In reply to this post by Peck, Jon
Jon,

I installed wx.Python but I still cannot find out if there is a GUI Panel
in Python to drag and place buttons like in Sax Basic. If yes, how do I
open it?

Thanks,

Philip
Reply | Threaded
Open this post in threaded view
|

Re: Dialog Windows and Syntax: Part II

Peck, Jon
wxPython does not have a simple gui builder corresponding closely to the SaxBasic model.  It's capabilities are very general and take a bit more work to learn than SaxBasic.  However, there are several ways to get started.  When you install wxPython, you get a number of tools, including a simple gui editor called XRCed, which appears on the wxPython Docs Demos and Tools menu.  It can construct an xml specification of a dialog which you create visually using the editor.

Unlike typical Windows editors, layout is controlled via sizers within which you place the controls you want.

Then you use Python code and wx.xrc classes to load and manipulate those dialogs.  There is a very informal wiki at

http://wiki.wxpython.org/index.cgi/FrontPage

It includes a tutorial
http://wiki.wxpython.org/index.cgi/XRCTutorial

and more information about xrc at

http://wiki.wxpython.org/index.cgi/UsingXmlResources

I have not personally worked a lot with these tools, and there are several alternative approaches, but this will give you an idea of what is out there.

HTH,
Jon Peck



-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Philip Papush
Sent: Monday, October 09, 2006 8:09 AM
To: [hidden email]
Subject: Re: [SPSSX-L] Dialog Windows and Syntax: Part II

Jon,

I installed wx.Python but I still cannot find out if there is a GUI Panel
in Python to drag and place buttons like in Sax Basic. If yes, how do I
open it?

Thanks,

Philip