SF-LUG - Weekly meeting of SF-LUG on Dec 7

Ken Shaffer Kenneth_C_Shaffer at comcast.net
Fri Dec 12 20:33:02 PST 2014


Jim had a question about the best Python GUI, and I suggested tcl/tk, so
I thought I'd pass along a simple example which takes two args, a window
title, and a question, printing out the result.  Not a perfect example,
as the numbers returned don't seem to go anywhere.

./tstwin.py "Last Election Question" "Did you vote?"

Put the below into an executable file tstwin.py:

#!/usr/bin/python
import sys
import tkMessageBox

#root=Tk()

# Make sure the args exist before using them
def main():
  if len(sys.argv) >= 3:
     retval = mywin(sys.argv[1], sys.argv[2])
  elif len(sys.argv) == 2:
     retval = mywin(sys.argv[1])
  else:
     retval = mywin()
  return retval

def mywin(title="My question", body="Is it working yet?"):
  if tkMessageBox.askyesno(title, body) == 1:
    print "Got a yes"
    return 1
  else:
    print "Nope"
    return 2

if __name__ == '__main__':
  main()





More information about the sf-lug mailing list