YAD dialogs under Python

http://rpm.pbone.net/index.php3/stat/45/idpl/17003956/numer/1/nazwa/yad

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:
$python3
>>>...
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", '
'--width=800'
,
'--height=150',
'--center',
'--columns=2',
'--title', title,
'--text', description,
'--form',
'--field', 'Chart Title', 'Gibraltar',
'--field', 'Number', '10007',
'--field', 'Scale', '25,000',
'--field', 'Units', 'Meters',
'--field', 'Projection', 'Mercator',
'--field', 'Datum', 'WGS84'],
stdout=subprocess.PIPE)
outlist = result.stdout
print(outlist)
outstring = outlist.decode(
"utf-8")
print(
"Results are:")
print(outstring)
tdi_args = outstring.split(
"|")
print(tdi_args)
chart_title = tdi_args[
0]
number = tdi_args[
1]
scale = tdi_args[
2]
units = tdi_args[
3]
projection = tdi_args[
4]
datum = tdi_args[
5]
print(chart_title, number, scale, units, projection, datum)