Site Sponsors:
"Just Do It:" Graphical Command-Line Parameters for Python 3 
There seems to be a huge line between those who want to use console applications, and those who have the need for a GUI.

When using Python however, such lines need not exist: It can be easy to jump between the the Graphical & Textual Worlds!

from tkinter import *

# Mission: Create a way to query a user for command-line values.
# Mission statement implies the encapsulation of a GUI paradigm.
# Here is how to do so using tkinter under Python 3.

''' Prompter: Graphically get a dictionary of command-line strings from a user.
Dictionary result is empty when the `cancel` button has been pressed, else the results
will contain the tag-value pairing (i.e. Dictionary keys match the fields requested, to
get the user's response for each field.)
class Prompter:
def __init__(self):
self._dict = None
self._isOk = None

def _okay(self):
self._isOk = True

def _cancel(self):
self._isOk = False

def InputStrings(*fields, title="Input"):
self = Prompter() = Tk()

self._dict = dict()

if title:

row = 0
# zFields (A Label, plus an Entry, in a grid layout)
for ref in fields:
obj = Label(, text=str(ref))
obj.grid(row=row, column=0)

obj = Entry(, bd=5)
obj.grid(row=row, column=1)

row = row + 1

# zButtons (A Frame in the grid, plus the properly-centered pair of buttons)
bottom = Frame(
bottom.grid(row=row, columnspan=2)
btn = Button(bottom, text="Okay", command=self._okay)

btn = Button(bottom, text="Cancel", command=self._cancel)

# zCenter (Close enough to make no odds?)
width =
height =
x = width / 2 - ( / 2)
y = height / 2 - ( / 2)"+%d+%d" % (x, y))
results = dict()
if self._isOk is not True:
return results

for ref in self._dict.keys():
results[ref] = (self._dict[ref]).get()
return results

if __name__ == "__main__":
# Here is how we would use the Prompter from a Console Program:
results = Prompter.InputStrings("this", "isa", "test", title="Just Do It!")
if len(results) is 0:
print("Pressed Cancel - no values!")
print("Pressed Okay - got values!")
for ref in results:
print(ref, results[ref])

Taking time to review the test case, all one need do is to provide Prompter.InputStrings() with the queries. The title is optional.

Upon activation, Prompter.InputStrings() will create a reasonable-looking user interface, screen-center it & use it to query the user, then return a dictionary of the string response(s) provided.

When the user presses "cancel," the dictionary will be empty.

WIPI Results

Of course, one of the most annoying things about Dict()ionaries in this man's world is - at the time of this writing - the eccentric ordering of their key:value pairs.

Submitted for your perusal therefore, allow me to offer up Prompter.InputOrder() for your more predictable re-usages:

def InputOrder(*fields, title="Input"):
results = Prompter.InputStrings(*fields, title=title)
if len(results) is 0:
return [*zip([],[])]
data = []
for ref in fields:
results = zip(fields, data)
return [*results]

Designed to work exactly as Prompter.InputStrings() (sans the dict(), of course):

-Here be the test case:
    results = Prompter.InputOrder("this", "isa", "test", title="Just Do It!")
if len(results) is 0:
print("Pressed Cancel - no values!")
print("Pressed Okay - got values!")
for ref in results:
print(ref[0], ref[1])
-With the much more predictable, everything is WHERE-I-PUT-IT (W.I.P.I = 'wippy') 'list of lists' result paring:


Add Comment
Fill out the form below to add your own comments.

Insert Special:

Moderation is turned on for this blog. Your comment will require the administrators approval before it will be visible.