Input function writing on wrong line

Hello, Slicer community!

I was trying to make the python process wait for me while I manually edit the segmentation.

I thought of using the input function for this.
But somehow, the python interacter keep makes me to write on wrong line and the function would not work properly…

I tested the same script on VS code and it worked as it supposed to.
So I do not understand why slicer is making me write on wrong line.

Can anyone give me advice on why this problem may occur and how to I can solve this?
below is my script and thank you always for your help.

import os
import sys

def moveOn():
	toPass = ["y", "Y"]
	while True:
			value = input("Do you want to move on?(Y/N) : ")
		except ValueError:
			print("Sorry, I didn't understand that.")
			return moveOn()
		if value not in toPass:
			print("Sorry, you typed wrong value.")
			return moveOn()
			return value

nrrdDir = "F:/inputTesting"
nrrdList = [file for file in os.listdir(nrrdDir) if file.endswith(r'.nrrd')]
for volFile in nrrdList:
	volName = str(volFile).strip(".nrrd")
	volDir = nrrdDir + "/" + volFile
	print("____" + volName + " Polishing...\n")
	value = moveOn()
	print("If this is printed, then input worked")

Showing the user some messages on some text console in a GUI application would be highly unusual and inconvenient for users. Instead, you can quickly create a nice and convenient GUI in Qt designer. See for example this tutorial. You can put a Segment Editor widget inside your module, so your users don’t need to switch to a different module during segmentation. Having your python script in a Slicer module also takes care of selecting inputs, outputs, installation, distribution to users, updates, etc.

If you don’t need very smooth user experience just some very quick way to run a python script somewhat interactively, then you can add a button anywhere on the GUI, for example in the status bar, and continue with the processing when the user clicked the button. For example:

def onNextStep():
    print("Editing completed.")

nextStepButton = qt.QPushButton("Finish segmentation")
slicer.util.mainWindow().statusBar().insertPermanentWidget(0, nextStepButton)
nextStepButton.connect('clicked()', onNextStep)

# To remove the button:
# slicer.util.mainWindow().statusBar().removeWidget(nextStepButton)


1 Like

About the original issue of the unexpected behavior of the console: I’ve submitted a fix for the issue, it should be integrated into the Slicer Preview Release within a few days. I would still recommend to use buttons, widgets, etc. on a module GUI to communicate with end users, instead of via messages in a text console.

1 Like

This is so awesome!! :laughing:
I did tried to make a button but just could not figure out how to make it work properly!
So thank you so much for providing this awesome script!!
This is way better than what I have been doing!!
And thank you for fixing the console as well!!!

1 Like