YAD dialogs under Python


YAD is a program that will display GTK+ dialogs, and return (either in the return code, or on standard output) the users input. This allows you to present information, and ask for information from the user, from all manner of shell scripts.

There are at least three invocation methods:

1. as a console command line entry with hypenated options, e.g.,
yad --entry --title=mtitle --label=mlabel --text=dtext --posx=150 --posy=90 --width=300 --height=150

2. as a Python routine.

The python option can be called from either a console:
or a python script.

The python script option is achieved with the subprocess module.

Regardless of what method you choose, the output of yad is always to standard output. So we need to capture standard output to be able to make use of the output.

Under BASH you simply set a variable equal to the command and parse it:
outlist = yad ...

Under Python you need to capture the subprocess output. This is done with the variable.stdout property.

Its return value is a bytes object. So if you want a proper string, you’ll need to decode it.
>>> result.stdout.decode('utf-8')

This results in a string list.

You then use the .split method to read the individual elements.

Finally, you access the individual elements of the list by [index]

We’ll populate this page with more detail IDC. But in the meantime, this code works under Python 3.7:

# This routine was updated and validated 180918
# by C. Andrews Lavarre, Privus Technologies, LLC.
# It collects data for MapCal Chart Information
# based on yad
# Arguments:
# title
# number
# scale
# units
# projection
# datum

# Initialize
import sys
import subprocess
import io
from contextlib import redirect_stdout

title = "Data Entry"
description = "Enter chart information:"
result = subprocess.run(['yad',
'--on-top", '
'--title', title,
'--text', description,
'--field', 'Chart Title', 'Gibraltar',
'--field', 'Number', '10007',
'--field', 'Scale', '25,000',
'--field', 'Units', 'Meters',
'--field', 'Projection', 'Mercator',
'--field', 'Datum', 'WGS84'],
outlist = result.stdout
outstring = outlist.decode(
"Results are:")
tdi_args = outstring.split(
chart_title = tdi_args[
number = tdi_args[
scale = tdi_args[
units = tdi_args[
projection = tdi_args[
datum = tdi_args[
print(chart_title, number, scale, units, projection, datum)