Get selected item from generic dialog box


I am currently writing a program that creates a dialog window prompting the user to select one choice depending on what they are seeing in the image. The options are:

“coraline”, “tunicate”, “macro”, “red”, “more than one”, “background”

However, it can’t seem to identify the choice the user made. My code always returns 0.

from ij.gui import GenericDialog

def getOptions():  
	item = ["coraline", "tunicate", "macro", "red", "more than one", "background"]
	gd = GenericDialog("Options")  
	gd.addRadioButtonGroup("options", item, 3, 2, "coraline")
	if gd.wasCanceled():   
		return 0
	button = gd.getNextRadioButton()

	if button is item[0]:
		print button
		print 1
	elif button is item[1]:
		print button
		print 1
	elif button is item[2]:
		print button
		print 1
	elif button is item[3]:
		print button
		print 1
	elif button is item[4]:
		print button
		print 1
	elif button is item[5]:
		print button
		print 1
		print 0


Thank you in advance for your help!


Hello Juliana -

The “is” tests in your if, elif statements should be “==” tests.
“==” tests whether the two objects are equal – that is, in this case,
that they contain the same sequence of characters. “is” tests
whether the two objects are the same, that is, whether both
variables point to the same location in memory where the a
single copy of the object is stored.

In your case, you have two copies of the same string (same
characters), but they are two separate strings in different
locations in memory. So all of your “is” tests test false.

This is just one of those python things. (A similar issue exists
in java.)

Thanks, mm

I recommend using script parameters instead of GenericDialog, as they can significantly simplify your scripts. Try running the following Python script from the script editor in Fiji:

#@ String (choices={"coraline", "tunicate", "macro", "red", "more than one", "background"}) option

print option

if (option == "coraline"):
  print 1

Elaborating on the answer by @mountain_man, here’s some info on stackoverflow (a good place to look for answers concerning general programming questions):