Jump to content

Create a dialog box of X columns and Y rows based on delimited values in a script


---
 Share

Recommended Posts

Hello,

Is it possible to interact with the creation of a dialog from a script? For example, can you define the columns and rows of a dialog in a scripting environment before calling the dialog?

To illustrate, I’ll use a simple example.

Giving these values to these variables (left image) I want to create the dialog of the right image.

image.png.52a8dd0095d305fa44f50cbbb600e52a.pngimage.png.a72fbd45df71cfb299b58874efe11183.png

If the values given were different, the dialog would also change. See the image below.

 

image.png.8cf9d0cd966a34ea5d168a3dc86d7f04.pngimage.png.75909f0242a50832674669c3f2509f63.png

Is there any way I could control the dialog in this manner?

Thank you in advance,

Irune

Link to comment
Share on other sites

Hello Irune,

Yes, this is possible. When you insert a dialog into the script, you can select the option "Embedded into script". You will notice that the dialog definition is a Python dictionary, which is passed to gom.script.sys.create_user_defined_dialog (). Ususally, this dictionary is created with the Dialog editor and used as a constant, but it is possible to modify it before calling gom.script.sys.create_user_defined_dialog ().

For each widget in the dialog, you will see a key/value pair for "rows" and "columns" which defines its position in the dialog window. The dialog window's total number of rows and columns is calculated internally.

You can create widgets by adding entries in the dictionary by your script accordingly.

Best regards,

Matthias

Link to comment
Share on other sites

Please sign in to view this username.

 Have you been able to make this work? 

Please sign in to view this username.

 could you please give an example of how his works in practice? I have been trying to figure this out, but I don't know how to begin without also breaking the embedded dialog code.

 

 

Link to comment
Share on other sites

I did some digging around and using other examples on the forums, I developed some code that can dynamically add a number of (in my case, checkboxes), as well other widgets like the continuous text widget.

The code is a bit rudimentary as I am new to Python and I am not sure if it what

Please sign in to view this username.

 was hinting at, but this code is a start. One issue it does have is that it doesn't allow me to use html formatting on the text within the continuous text field despite me using <html> tagging.

 

import gom

def createBlankForm(width, height):
	
	blankForm='<dialog>' \
	'<title>Test Dialog</title>' \
	'<style></style>' \
	'<control id="Empty"/>' \
	'<position></position>' \
	'<embedding></embedding>' \
	'<sizemode></sizemode>' \
	'<size width="' + str(width) + '" height="' + str(height) + '"/>' \
	'<content columns="10" rows="10">'
	
	return blankForm

def addcheckBox(colPosition, rowPosition, checkBoxName, instance):
	
	checkBox =  '<widget columnspan="1" type="input::checkbox" rowspan="1" row="' + str(rowPosition) + '"column="' + str(colPosition) + '">' \
				'	<name>checkbox_' + str(instance) + '</name>' \
				'	<tooltip></tooltip>' \
				'	<value>true</value>' \
				'	<title>' + str(checkBoxName) + '</title>' \
				'</widget>'  
				
	return checkBox

def addContinuousText(colPosition, rowPosition, text):
	
	c_text =  '<widget columnspan="1" type="display::text" rowspan="1" row="' + str(rowPosition) + '"column="' + str(colPosition) + '">' \
			'	<name>text</name>' \
			'	<tooltip></tooltip>' \
			'	<text>' + str(text) + '</text>' \
			'	<value>true</value>' \
			'</widget>'  
			
	return c_text

def buildDialog():
	
	dialog = createBlankForm(500, 500)
	dialog += addContinuousText(0,0,'<html><p align=\"center\"><span style=\"font-weight:600; color:#ff0000;\">abc</span></p></html>')
	
	startingRow = 1
	startingCol = 1
	for i in range(5):
		dialog += addcheckBox(startingCol, startingRow + i, 'Random_Name_' + str(i), i)
		
	return dialog + '</content></dialog>'
	
def dialog_event_handler (widget):
	#print(widget)
	
	if widget == dialog.checkbox_1:
		print('Checkbox_1 was interacted with!')
	
	return 1

dialog=gom.script.sys.create_user_defined_dialog (content = buildDialog())
dialog.handler = dialog_event_handler
gom.script.sys.show_user_defined_dialog (dialog=dialog)

 

Edited
minor correction
Link to comment
Share on other sites

Thank you for your responses!

 

Please sign in to view this username.

, that code was very helpful!

Still, I need to look into it more deeply to fully understand it.

I have been trying to do something similar myself. I attempted to define the content of the dialog outside so I could use a ‘for’ loop to add all the rows I wanted. To achieve this, I used the same format and structure as the dialog code.

Here is an example of a code I wrote to create as many rows of buttons as I need:

import gom



number_of_rows=5

contenido=[]
nombre='BUTTON'

for i in range(number_of_rows):
	nombre1='button_'+str(i)
	contenidoi=[{"button_type": "toggle","columns": 1,"icon": "AAAAAA==","icon_file_name": "","icon_size": {"value": "icon"},"icon_system_size": {"value": "default"},"icon_system_type": {"value": "ok"},"icon_type": {"value": "none"},"name": nombre1,"rows": 1,"text": {"id": "","text": nombre,"translatable": True},"tooltip": {"id": "","text": "","translatable": True},"type": "button::pushbutton"}]
	print(contenidoi)      
	contenido.append(contenidoi)
	
print(contenido)


DIALOG=gom.script.sys.create_user_defined_dialog (dialog={
				"content": contenido,
				"control": {
								"id": "OkCancel"
				},
				"embedding": "",
				"position": "",
				"size": {
								"height": 242,
								"width": 198
				},
				"sizemode": "",
				"style": "",
				"title": {
								"id": "",
								"text": "Dialog Title",
								"translatable": True
				}
})

#
# Event handler function called if anything happens inside of the dialog
#
def dialog_event_handler (widget):
	pass

DIALOG.handler = dialog_event_handler

RESULT=gom.script.sys.show_user_defined_dialog (dialog=DIALOG)

Best regards,

Irune

Link to comment
Share on other sites

Please sign in to view this username.

I'm glad that my code helped. It looks like you're on the right track, and unlike me it appears you discovered how to dynamically create a dialog box using the JSON code instead of XML code (which I believe is why my HTML formatting code is not working). It seems that all we need to simply do is use '.append' to build out the dialog box using JSON code. I will try converting my code to use JSON when I have the chance.

In the meantime, I used these two posts to help learn how to dynamically build a dialog box: https://qualityforum.zeiss.com/topic/1217-selection-element-list-user-defined-script-function/#comment-3959 and https://qualityforum.zeiss.com/topic/1484-create-a-dynamic-dialog/#comment-4940.

I believe that GOM dialog boxes are grid based and the positions of widgets originate from a 0,0 position in the top-left corner. You can tell the script that you want 10 rows and 10 columns, but you can't have any gaps between the rows and columns unless you find a way to use seperators.

Here is slightly altered code from one of these posts similar that may help you determine out how to define a number of rows and columns.

def selection(prompt='', item_list=[]):	
	if not isinstance(item_list, list):
		
        return
	
	header = '<dialog>' \
			'<title>What is the meaning of life?</title>' \
			'<control id="Empty"/>' \
			'<position>center</position>' \
			'<sizemode>fixed</sizemode>' \
			'<size width="200" height="100"/>' \
			'<content rows="{}" columns="{}">' \
			'<widget row="0" column="0" type="label">' \
			'<name>prompt</name>' \
			'<text>{}</text>' \
			'</widget>'
	
	button = '<widget column="{}" row="{}" type="button::pushbutton">' \
			'<name>{}</name>' \
			'<text>{}</text>' \
			'<type>toggle</type>' \
			'<focus>True</focus>' \
			'</widget>'
	
	def build_dialog():
		
		num_columns = int(len(item_list) / 5) + 1		
		dialog = header.format(len(item_list) + 1, num_columns, prompt)		
		index = 0
		
		for i in range(1, len(item_list) + 1):
			
			if i <= 5:				
				row = i				
				column = index
				
				if i == 5:					
					index += 1
			else:				
				row = abs(index * 5 - i)				
				column = index
				
#				if i % 5 == 0: #Uncomment these two lines to add a third row of buttons					
#					index += 1
			
			dialog += button.format(column, row, i, item_list[i - 1])
		
		return dialog + '</content></dialog>'
	
	def dialog_event_handler(widget):
		global selected
		
		if isinstance(widget, gom.Widget):
			selected = widget.text
			gom.script.sys.close_user_defined_dialog(dialog=dialog)
	
	dialog = gom.script.sys.create_user_defined_dialog(content=build_dialog())
	dialog.handler = dialog_event_handler
	gom.script.sys.show_user_defined_dialog(dialog=dialog)
    
	return selected
	
buttonList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
selection('Put anything you want here', buttonList)

 

 

Edited
Link to comment
Share on other sites

I have to move on to other tasks, but I think I've figured out a bit of how the software is interpreting JSON code and building dialog boxes. Hopefully a developer or someone more knowledgeable can chime in, however I believe this is how things are working:

-----

1) Everything appears to originate from the top left corner of the first available row.

2) For any widget(s) you wish to add, first you define the columns it will span.

    - If you wish to add just one widget for that given row, simply add the one widget. The next widget or widget(s) will be in the next row down.

    - If you wish to add multiple widgets to the same row, you need to build a list of the widgets that belong in that row. The next widget or widget(s) will be in the next row down.

-----

I'm going to share some of my code below to give you an idea of how you can put together JSON based dialog boxes. I will also share commented code that can help teach you how the rows and columns are interpreted.

-----

The code for my dialog box:

import gom

#number_of_rows=5
#contenido=[]
#nombre='BUTTON'
#
#for i in range(number_of_rows):
#	nombre1='button_'+str(i)
#	contenidoi=[{"button_type": "toggle","columns": 1,"icon": "AAAAAA==","icon_file_name": "","icon_size": {"value": "icon"},"icon_system_size": {"value": "default"},"icon_system_type": {"value": "ok"},"icon_type": {"value": "none"},"name": nombre1,"rows": 1,"text": {"id": "","text": nombre,"translatable": True},"tooltip": {"id": "","text": "","translatable": True},"type": "button::pushbutton"}]
#	print(contenidoi)      
#	contenido.append(contenidoi)

nombre='BUTTON'
contenidoi=[{"button_type": "toggle","columns": 1,"icon": "AAAAAA==","icon_file_name": "","icon_size": {"value": "icon"},"icon_system_size": {"value": "default"},"icon_system_type": {"value": "ok"},"icon_type": {"value": "none"},"name": nombre,"rows": 1,"text": {"id": "","text": nombre,"translatable": True},"tooltip": {"id": "","text": "","translatable": True},"type": "button::pushbutton"}]

def blankForm():
	base = [
				{
					"columns": 1,
					"default_font_family": "",
					"default_font_size": 0,
					"name": "text",
					"rows": 1,
					"text": {
						"id": "",
						"text": "<html><p align=\"center\">Test</p><p style=\"-qt-paragraph-type:empty;\" align=\"center\"><br/></p><p align=\"center\"><span style=\"font-size:16pt; font-weight:600; color:#ff0000;\">ABC123</span></p></html>",
						"translatable": True
					},
					"tooltip": {
						"id": "",
						"text": "",
						"translatable": True
					},
					"type": "display::text",
					"wordwrap": False
				}
			]
	
	return base

def addContinuousText():			
	c_text = [
				{
					"columns": 2,
					"default_font_family": "",
					"default_font_size": 0,
					"name": "text",
					"rows": 1,
					"text": {
						"id": "",
						"text": "<html><p align=\"center\">Example</p><p align=\"center\"><span style=\"font-size:12pt; font-weight:600; color:#ff0000;\">ABC123</span></p></html>",
						"translatable": True
					},
					"tooltip": {
						"id": "",
						"text": "",
						"translatable": True
					},
					"type": "display::text",
					"wordwrap": False
				}
			]
			
	return c_text

def addInput(default):
	input = [
				{
					"columns": 2,
					"name": "input_scanner",
					"password": False,
					"read_only": False,
					"rows": 1,
					"tooltip": {
						"id": "",
						"text": "",
						"translatable": True
					},
					"type": "input::string",
					"value": default
				}
			]
			
	return input

def addCheckbox(description):			
	checkbox = [
					{
						"columns": 2,
						"name": "checkbox",
						"rows": 1,
						"title": {
							"id": "",
							"text": description,
							"translatable": True
						},
						"tooltip": {
							"id": "",
							"text": "",
							"translatable": True
						},
						"type": "input::checkbox",
						"value": False
					}
				]

	return checkbox			

def addVerticalSpace(instance, size):
	v_spacer = [
					{
						"columns": 2,
						"maximum_size": -1,
						"minimum_size": size,
						"name": "spacer_" + str(instance),
						"rows": 1,
						"tooltip": {
							"id": "",
							"text": "",
							"translatable": True
						},
						"type": "spacer::vertical"
					}
				]
				
	return v_spacer

def addSingleButton(instance, type):
	button = [
				{
					"button_type": "push",
					"columns": 1,
					"icon": "AAAAAA==",
					"icon_file_name": "",
					"icon_size": {
						"value": "icon"
					},
					"icon_system_size": {
						"value": "extra_large"
					},
					"icon_system_type": {
						"value": type
					},
					"icon_type": {
						"value": "system"
					},
					"name": "button_" + str(instance),
					"rows": 1,
					"text": {
						"id": "",
						"text": "",
						"translatable": True
					},
					"tooltip": {
						"id": "",
						"text": "",
						"translatable": True
					},
					"type": "button::pushbutton"
				}
			]
			
	return button
	
def addButtonList(firstButton, instance, type):
	
	buttonList = []

	secondButton = [
						{
							"button_type": "push",
							"columns": 1,
							"icon": "AAAAAA==",
							"icon_file_name": "",
							"icon_size": {
								"value": "icon"
							},
							"icon_system_size": {
								"value": "extra_large"
							},
							f"icon_system_type": {
								"value": type
							},
							"icon_type": {
								"value": "system"
							},
							"name": "button_" + str(instance),
							"rows": 1,
							"text": {
								"id": "",
								"text": "",
								"translatable": True
							},
							"tooltip": {
								"id": "",
								"text": "",
								"translatable": True
							},
							"type": "button::pushbutton"
						}
					]
	
	buttonList.append(firstButton[0])
	buttonList.append(secondButton[0])
	
	return buttonList
			
blankForm = []
blankForm.append(addContinuousText())
blankForm.append(addInput('the default text'))
blankForm.append(addCheckbox('Checkbox #1'))
blankForm.append(addVerticalSpace(1, 5))

aButton = addSingleButton(1,'ok')
buttonsAsRows = addButtonList(aButton, 2, 'cancel')
blankForm.append(buttonsAsRows)

blankForm.append(addVerticalSpace(2, 25))
blankForm.append(addSingleButton(3, 'warning'))
blankForm.append(addSingleButton(4, 'arrow_right'))

DIALOG=gom.script.sys.create_user_defined_dialog (dialog={
				"content": blankForm,
				"control": {
								"id": "OkCancel"
				},
				"embedding": "always_toplevel",
				"position": "automatic",
				"size": {
								"height": 500,
								"width": 500
				},
				"sizemode": "fixed",
				"style": "",
				"title": {
								"id": "",
								"text": "Dialog Title",
								"translatable": True
				}
})

def dialog_event_handler (widget):
	pass

DIALOG.handler = dialog_event_handler

RESULT=gom.script.sys.show_user_defined_dialog (dialog=DIALOG)


"""
OPEN THE CODE BELOW IN THE GUI EDITOR TO SEE A GRID BASED LAYOUT. 

1) PLAY AROUND WITH THE ROW AND COLUMNS THAT THE WIDGETS WILL EMCOMPASS.
2) NEXT TAKE A LOOK AT THE SEQUENCE THE WIDGETS IN THE CODE. PAY ATTENTION TO THE NUMBERS OF ROWS AND COLUMNS.
	IF YOU USE UNIQUE NAMES FOR EACH WIDGET, YOU CAN DETERMINE THE ORDER IN WHICH YOU NEED TO WRITE OUT YOUR WIDGETS DYNAMICALLY.
"""

DIALOG=gom.script.sys.create_user_defined_dialog (dialog={
				"content": [
								[
												{
																"columns": 2,
																"default_font_family": "",
																"default_font_size": 0,
																"name": "text",
																"rows": 1,
																"text": {
																				"id": "",
																				"text": "<html><p align=\"center\"><span style=\"font-weight:600;\">Widget 1</span></p></html>",
																				"translatable": True
																},
																"tooltip": {
																				"id": "",
																				"text": "",
																				"translatable": True
																},
																"type": "display::text",
																"wordwrap": False
												},
												{
												},
												{
																"columns": 1,
																"default_font_family": "",
																"default_font_size": 0,
																"name": "text_1",
																"rows": 1,
																"text": {
																				"id": "",
																				"text": "<html><p align=\"center\"><span style=\"font-weight:600;\">Widget 2</span></p></html>",
																				"translatable": True
																},
																"tooltip": {
																				"id": "",
																				"text": "",
																				"translatable": True
																},
																"type": "display::text",
																"wordwrap": False
												}
								],
								[
												{
																"columns": 3,
																"default_font_family": "",
																"default_font_size": 0,
																"name": "text_2",
																"rows": 1,
																"text": {
																				"id": "",
																				"text": "<html><p align=\"center\"><span style=\"font-weight:600;\">Widget 3</span></p></html>",
																				"translatable": True
																},
																"tooltip": {
																				"id": "",
																				"text": "",
																				"translatable": True
																},
																"type": "display::text",
																"wordwrap": False
												},
												{
												},
												{
												}
								],
								[
												{
																"columns": 1,
																"default_font_family": "",
																"default_font_size": 0,
																"name": "text_3",
																"rows": 3,
																"text": {
																				"id": "",
																				"text": "<html><p align=\"center\"><span style=\"font-weight:600;\">Widget 4</span></p></html>",
																				"translatable": True
																},
																"tooltip": {
																				"id": "",
																				"text": "",
																				"translatable": True
																},
																"type": "display::text",
																"wordwrap": False
												},
												{
																"columns": 1,
																"default_font_family": "",
																"default_font_size": 0,
																"name": "text_4",
																"rows": 1,
																"text": {
																				"id": "",
																				"text": "<html><p align=\"center\"><span style=\"font-weight:600;\">Widget 5</span></p></html>",
																				"translatable": True
																},
																"tooltip": {
																				"id": "",
																				"text": "",
																				"translatable": True
																},
																"type": "display::text",
																"wordwrap": False
												},
												{
																"columns": 1,
																"default_font_family": "",
																"default_font_size": 0,
																"name": "text_6",
																"rows": 2,
																"text": {
																				"id": "",
																				"text": "<html><p align=\"center\"><span style=\"font-weight:600;\">Widget 6</span></p></html>",
																				"translatable": True
																},
																"tooltip": {
																				"id": "",
																				"text": "",
																				"translatable": True
																},
																"type": "display::text",
																"wordwrap": False
												}
								],
								[
												{
												},
												{
																"columns": 1,
																"default_font_family": "",
																"default_font_size": 0,
																"name": "text_5",
																"rows": 2,
																"text": {
																				"id": "",
																				"text": "<html><p align=\"center\"><span style=\"font-weight:600;\">Widget 7</span></p></html>",
																				"translatable": True
																},
																"tooltip": {
																				"id": "",
																				"text": "",
																				"translatable": True
																},
																"type": "display::text",
																"wordwrap": False
												},
												{
												}
								],
								[
												{
												},
												{
												},
												{
																"columns": 1,
																"default_font_family": "",
																"default_font_size": 0,
																"name": "text_7",
																"rows": 3,
																"text": {
																				"id": "",
																				"text": "<html><p align=\"center\"><span style=\"font-weight:600;\">Widget 8</span></p></html>",
																				"translatable": True
																},
																"tooltip": {
																				"id": "",
																				"text": "",
																				"translatable": True
																},
																"type": "display::text",
																"wordwrap": False
												}
								],
								[
												{
																"columns": 2,
																"default_font_family": "",
																"default_font_size": 0,
																"name": "text_10",
																"rows": 1,
																"text": {
																				"id": "",
																				"text": "<html><p align=\"center\"><span style=\"font-weight:600;\">Widget 9</span></p></html>",
																				"translatable": True
																},
																"tooltip": {
																				"id": "",
																				"text": "",
																				"translatable": True
																},
																"type": "display::text",
																"wordwrap": False
												},
												{
												},
												{
												}
								],
								[
												{
																"columns": 1,
																"default_font_family": "",
																"default_font_size": 0,
																"name": "text_8",
																"rows": 1,
																"text": {
																				"id": "",
																				"text": "<html><p align=\"center\"><span style=\"font-weight:600;\">Widget 10</span></p></html>",
																				"translatable": True
																},
																"tooltip": {
																				"id": "",
																				"text": "",
																				"translatable": True
																},
																"type": "display::text",
																"wordwrap": False
												},
												{
																"columns": 1,
																"default_font_family": "",
																"default_font_size": 0,
																"name": "text_9",
																"rows": 1,
																"text": {
																				"id": "",
																				"text": "<html><p align=\"center\"><span style=\"font-weight:600;\">Widget 11</span></p></html>",
																				"translatable": True
																},
																"tooltip": {
																				"id": "",
																				"text": "",
																				"translatable": True
																},
																"type": "display::text",
																"wordwrap": False
												},
												{
												}
								]
				],
				"control": {
								"id": "OkCancel"
				},
				"embedding": "always_toplevel",
				"position": "automatic",
				"size": {
								"height": 545,
								"width": 539
				},
				"sizemode": "automatic",
				"style": "",
				"title": {
								"id": "",
								"text": "Dialog Title",
								"translatable": True
				}
})

#
# Event handler function called if anything happens inside of the dialog
#
def dialog_event_handler (widget):
	pass

DIALOG.handler = dialog_event_handler

RESULT=gom.script.sys.show_user_defined_dialog (dialog=DIALOG)

image.png.22c873a0155ee6e5d3fa25b8839dd00a.png   image.png.f662385a4c6e116cbc8314d829d4bfb5.png

Edited
Link to comment
Share on other sites

 Share

×
×
  • Create New...