user input

Post any questions, ideas, or topics related to Jython and Python scripting.
Post Reply
User avatar
hproe
Posts: 504
Joined: Sat Nov 27, 2010 3:46 pm

user input

Post by hproe »

Hi -

I might have overlooked the pertinent information. I cannot find a way to ask for user input in a script. I have tried a=(raw_)input('prompt: '), but get OOError: The handle is invalid.

HP
User avatar
bobc
Posts: 990
Joined: Mon Nov 15, 2010 5:57 pm

Re: user input

Post by bobc »

Hi HP -

After running some tests and speaking with a programmer, as far as asking a a user to input a numerical value or something of that sort into a script, that functionality doesn't seem to work well currently. I wrote up McIDAS-V Inquiry 1889 to cover adding this enhancement. One use case I thought for this would be prompting the user to enter a band number, which would then be propagated through getADDEImage and createLayer. Is this how you were thinking of using the user input?

One thing that does work as for asking the user for input is selectData(). When selectData is run in the Jython Shell, a Field Selector window pops up where a user can select a field that has already been loaded. For example, you can load in some gridded data and then run the following:

Code: Select all

a = selectData()
panel = buildWindow()
layer = panel[0].createLayer('Color-Shaded Plan View',a)
layer.setEnhancement('Gray Scale')

In the Field Selector window from selectData(), a user would select a field, which would be assigned to 'a'. This field, 'a', is then passed through Create Layer.

Thanks -
Bob Carp
McIDAS Help Desk
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: user input

Post by tomw »

Hi HP...

I do not believe the "raw_input" would work in this context since you don't really have a "command line" (assuming you are running the script within the McIDAS-V framework). [I edited this, since at first glance, the form looked like what Jeff had done in the Formula line....]

If you want to ask for input while your script is running, you could use Java's built-in dialog. For example:

Code: Select all

from javax.swing import JOptionPane, JFrame
...
frame = JFrame();
ans = JOptionPane..showInputDialog(frame, "Type Your Answer Here"," Window title", JOptionPane.PLAIN_MESSAGE);


These dialogs are modal, so your script should stop and wait for the user to enter their answer.

The variable "ans" will contain the string that the user typed. There are many, many variations available for JOptionPane -- have a look at https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
User avatar
hproe
Posts: 504
Joined: Sat Nov 27, 2010 3:46 pm

Re: user input

Post by hproe »

Hi Bob and Tom -

Excellent. Both methods do the job. I have implemented Tom's proposal as it suits better the purpose of just getting a "ready to go" from the user.

many thanks, HP
Post Reply