button.setOnAction does not print anything when scripting



I’m using the scripting feature to extend the Qupath GUI by creating a new button on the toolbar. As a simple test, I try to get it to print “Test Print # 2: Button Clicked” whenever it is clicked.

However, while the button is successfully created, nothing appears on the console when clicked. Any idea why this simple script does not print anything when the button is pressed?

Qupath Version: 0.1.2
Groovy Version: 2.4.7, JDK 8
OS: Windows 10


//import javafx.application.Platform
import javafx.scene.control.Button
import javafx.scene.control.Tooltip
import qupath.lib.gui.QuPathGUI

def qupath = QuPathGUI.getInstance()

def button = new Button('CT')
button.setPrefSize(40, QuPathGUI.iconSize)
button.setTooltip(new Tooltip("Cursor Tracker"));
button.setOnAction {
   print("Test Print # 2: Button Clicked")

print("Test Print # 1")

"INFO: Test Print # 1" successfully displays when the script is run.

Easy and consistent measurement based cell coloring

Haven’t tested it but have you checked the Log rather than the script output? It should be accessible through the View menu. Many messages only show up there when inside functions, button presses, etc.


Yep, looks like that. You can see the same behavior in all of the GUI dialog window based scripts.

1 Like

Note that, in the script editor, there’s an option Run → Send output to log which is on by default… since that is the easiest way to make anything that is printed vision in the GUI.

You could also try replacing Groovy’s print method with System.out.print in case it is closer to what you want.

1 Like

Strangely for me, it is not appearing on the log either. I tested the script on qupath v0.2.0-m2 too but “Test Print # 2: Button Clicked” is not being displayed there either, on the logs or console.

No luck with System.out.print either.

However, I was able to successfully print on the log by calling the Logger.info function.

Logger logger = LoggerFactory.getLogger(QuPathGUI.class);
button.setOnAction {
   print("Test Print # 2: Button Clicked")  
   logger.info("Test Print # 3: call logger.info on button click")

Ah, that may be the best way to do it… as I recall, the script editor registers itself to listen to log events only for the duration that the script is running and it also does some redirection of Groovy printing so things end up passed to the log during that time. In other words, it sounds like it makes sense and I just hadn’t really considered the case where scripts might create persistent components that want to print.

Anyhow, is the solution you’ve found enough for what you need?


Oops, yes, sorry. I was testing in 0.2.0m2, but you did say 0.1.2 where it doesn’t work. I forgot that I swapped over some of my GUI script testing to 0.2.0 specifically for that reason! Nice to know that there is a way to get output in 0.1.2, though, thanks!

Edit: Surprised you weren’t able to get it working in 0.2.0 though. Only way I could disable it there was through the Run menu.


yes strangely println doesnt work on my 0.2.0m2 build but either way the logger solution serves my purpose.