pythondialogjythonimagejfiji

GenericDialogPlus - saving FileField as a new variable?


Newbie to scripting here.

I’d like to create a dialog window that prompts user to choose 2 files (“pos”, and “neg”). I’d also like to store these file paths as a variable to use later on.

I’m having trouble storing the file paths. Instead of storing the user chosen paths, I am only able to collect the default path.

import os 
from java.io import File 
from ij import IJ 
from ij.gui import GenericDialog 
from fiji.util.gui import GenericDialogPlus
> 
gdp = GenericDialogPlus(“test”) 
gdp.addFileField(“pos”, “C:/Users/”)
pos = gdp.getNextString() 
gdp.addFileField(“neg”, “C:/Users/”) 
neg = gdp.getNextString() 
gdp.showDialog() 
print("yourfiles "+ pos+ " " + neg)

But this just reutrns:

yourfiles C:/Users/ C:/Users/

even if the user has selected a new file, ie: C:/Users/test.tiff

I’d like the output to be based on whatever the user chose:

yourfiles C:/Users/test.tiff C:/Users/negative.tiff

What command am I missing? Thanks!


Solution

  • Advice from the x-post on ImageJ forum led me the solution.
    Variable asignment based on user input needs to be done after the dialog box is shown (duh, after user input...)

        import os 
        from java.io import File 
        from ij import IJ 
        from ij.gui import GenericDialog 
        from fiji.util.gui import GenericDialogPlus
    
        gdp = GenericDialogPlus(“test”) 
        gdp.addFileField(“pos”, “C:/Users/”)
        gdp.addFileField(“neg”, “C:/Users/”) 
        gdp.showDialog()
    
        pos = gdp.getNextString()
        neg = gdp.getNextString()
        print("yourfiles "+ pos+ " " + neg)
    

    output:

    yourfiles C:/Users/test.tiff C:/Users/negative.tiff