Dialog Box with default value

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
gsandy
Posts: 290
Joined: Sun Jan 27, 2019 7:07 pm
Location: Auckland New Zealand

Dialog Box with default value

Post by gsandy »

Can I get a default value pre-entered into a Input Box?
The first Input Box needs a default value of 0.5 and the second 4 – Refer code below.
Is there a simple way to combine the two separate Input Boxes into one?

Code: Select all

from PIL import Image
from PySide import QtGui
from PySide import QtCore

##Enter Radius of Circle
reply = QtGui.QInputDialog.getText(None, "Default 0.5","Enter Circle Radius mm:")
if reply[1]:
# user clicked OK
	radiusOfCircle = float(reply[0]) #Numeral
else:
# user clicked Cancel
	radiusOfCircle = float(reply[0]) # which will be "" if they clicked Cancel

##Enter Circle Spacing
replyspace = QtGui.QInputDialog.getText(None, "Default 4","Enter Circle Spacing:")
if replyspace[1]:
# user clicked OK
	pixelSpacing = int(replyspace[0]) #Interger
else:
# user clicked Cancel
	pixelSpacing = int(replyspace[0]) # which will be "" if they clicked Cancel
	
User avatar
onekk
Veteran
Posts: 6199
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Dialog Box with default value

Post by onekk »

You want to enter two value in one input box?

there is no way, or at least, you could enter the values, using a text field, parse the resulting text string to extract the two values, maybe using the comma as separator and the dot as "decimal separator" (common problem if you want to enter values is the choice of separators)

And then assign the resulting values (maybe after some sanity checks).

Is this way viable for you?

Regards.

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Dialog Box with default value

Post by heda »

"Can I get a default value pre-entered into a Input Box?"
yes, quite easy to find out how in the pyside docs, or just searching on QInputDialog.

Code: Select all

>>> reply = QtGui.QInputDialog.getText(None, "title","Enter Circle Radius mm:", text='0.5')
>>> reply = QtGui.QInputDialog.getDouble(None, "title","Enter Circle Radius mm:", value=0.5)
"Is there a simple way to combine the two separate Input Boxes into one?"
No, from docs "The PySide.QtGui.QInputDialog class provides a simple convenience dialog to get a single value from the user."

Is it hard to make your own dialogue that can take 2 values?
yes it could be if you just have started with coding,
no not really if you know a bit and read docs and look at examples.
Look at PySide_Intermediate_Examples, or just search the web for other examples.
gsandy
Posts: 290
Joined: Sun Jan 27, 2019 7:07 pm
Location: Auckland New Zealand

Re: Dialog Box with default value

Post by gsandy »

Thanks heda that's exactly what I wanted, the code worked well.

I didn't make myself very clear on the other part of the question. similar to attached, it's two fields on one form.
Attachments
Snap 2021-06-21 at 10.00.28.png
Snap 2021-06-21 at 10.00.28.png (48.65 KiB) Viewed 791 times
Post Reply