# # This is an attempt to create a GUI for Purchase01 # begun 6/24/07, updated 6/26/07 # Bernard Sypniewski # from Tkinter import * from ScrolledText import * import random root = Tk() root.title("HSL Purchase Program by Bernard Sypniewski") # geometry does WxH NOT HxW root.geometry("550x400") def start(): message = "\n\nThis program, called PURCHASE, is based on the HSL scenario called CHECK." message += "It attempts to model a brief discussion between a customer and a sales clerk" message += "in which the clerk tells the customer the purchase price, the customer checks his wallet" message += "to see if he has sufficient cash for the purchase and, if not, " message += "the customer inquires about paying for the purchase with a check.\n\n" message += ":" * 62 + " PURCHASE Linkage " + ":" * 62 +"\n\n" message += "There are two participants, " + participants[0] + " and " + participants[1] + ". The corresponding role parts are " + rp(0) + " and " + rp(1) + ".\n\n" message += ":" * 62 + " PURCHASE Linkage " + ":" * 62 textWindow.insert(END, message) tlt() def tlt(): message0 = "\n\nHere we start the top level task : " + taskNote(top_level_task[0]) + ". The" message0 += taskNote(top_level_task[0]) + " task has a subtask : \n\t" + taskNote(top_level_task[1]) + "," message0 += " which has subtasks of its own : \n" textWindow.insert(END, message0) t = "\t" for item in gpa: t += taskNote(item) + " " textWindow.insert(END, t) message1 = "" message1 += "\nIf the customer offers to pay by check (dependent on the purchase price and the amount in the customer's wallet)," message1 += " there are additional subtasks:\n" textWindow.insert(END, message1) tt = "\t" for item in gpc: tt += taskNote(item) + " " textWindow.insert(END, tt) def beginCode(): reset() if rNum == 0: start() showPurchasePrice() else: showPurchasePrice() # # The following loop gets subTask names from the gpa tuple and calls the subTasks with those names # for item in gpa: subTask(item) # # If [customer] has enough cash in his wallet to pay the purchase price (enough_cash == 1) this is skipped. # Otherwise, subTasks with names from the ic tuple (ic stands for Insufficient Cash) and begins to ask about # paying with a check # if enough_cash == 0 : subTask(ic[0]) # # The following loop calls subtasks with names from the gpc tuple (gpc stands for Get Permission for Check) # for item in gpc : subTask(item) # # The following function calls all the linkage's subtasks # def subTask(taskName) : if taskName == gpa[0] : check_for_cash() elif taskName == gpa[1] and enough_cash == 1: pay_with_cash() elif taskName == ic[0] : insufficient_cash() elif taskName == gpc[0] : local_inquiry() elif taskName == gpc[1] : local_response() def showPurchasePrice() : runNum(rNum) message2 = "" message2 += xec(taskNote(top_level_task[1]), taskNote(top_level_task[0])) message2 += rpNote(0) + "The total is: $" + purchasePrice message2 += xec(taskNote(gpa[0]), taskNote(top_level_task[1])) textWindow.insert(END, message2) def check_for_cash() : global enough_cash amount_in_wallet = getAmountInWallet() message4 = rpNote(1) + "open wallet to find $" + amount_in_wallet textWindow.insert(END, message4) if float(amount_in_wallet) >= float(purchasePrice) : enough_cash = 1 else: enough_cash = 0 def pay_with_cash() : message5 = xec(taskNote(gpa[1]), taskNote(gpa[0])) message5 += rpNote(1) + sc[random.randrange(3)] textWindow.insert(END, message5) printEL() def insufficient_cash() : message6 = xec(taskNote(ic[1]), taskNote(ic[0])) message6 += rpNote(1) + pbc[random.randrange(4)] textWindow.insert(END, message6) def local_inquiry() : message7 = xec(taskNote(gpc[0]), taskNote(ic[1])) message7 += rpNote(0) + lcq[random.randrange(2)] textWindow.insert(END, message7) def local_response() : message = xec(taskNote(gpc[1]), taskNote(ic[1])) textWindow.insert(END, message) userMsg = "\n\n\t\tChoose the type of answer below and click the ANSWER button" textWindow.insert(END, userMsg) def store_policy(rt) : message10 = xec(taskNote(gpc[2]), taskNote(ic[1])) textWindow.insert(END, message10) if rt == 0 : message11 = rpNote(0) + "Make it out for $" + purchasePrice + ". We have a stamp for the name. \n\t\t\tI also need to see some id." textWindow.insert(END, message11) elif rt == 1 : message12 = rpNote(0) + sp[random.randrange(4)] textWindow.insert(END, message12) printEL() def runNum(rn) : global rNum rn += 1 rnMsg = "\nRUN #" + str(rn) + "\n" rNum = rn textWindow.insert(END, rnMsg) def printEL() : endLine = "-" * 160 el = "\n\n" + endLine textWindow.insert(END, el) def rp(index) : return notation_system[0] + role_part[index] + notation_system[1] def rpNote(index) : return "\n\n" + rp(index) + prompt def getLocalResponse(): if enough_cash == 0: rType = app.rt.get() if rType == "ar": responseType = 0 else: responseType = 1 if responseType == 0 : message8 = rpNote(1) + ar[random.randrange(5)] textWindow.insert(END, message8) elif responseType == 1 : message9 = rpNote(1) + nr[random.randrange(3)] textWindow.insert(END, message9) store_policy(responseType) def xec(item1, item2) : return "\n\t\tExecuting " + item1 + " subtask of " + item2 def taskNote(task) : return notation_property[0] + task + notation_property[1] def makeCents() : cents = random.randrange(100) # # The following IF makes a string out of DOLLARS and CENTS. newCents is a variable that holds a 2 - digit version of CENTS. # The problem that is addressed is that when CENTS is randomly generated, no numbers with trailing 0s are generated. If the # length of the string version of CENTS is 1 then there should be a trailing 0. The first condition adds the 0. # if len (str(cents)) == 1 : newCents = str(cents) + str(0) else : newCents = str(cents) return newCents def makePurchasePrice() : # # PYTHON has a 'feature' which I find curious. If a function assigns a value to a global variable for the first time, PYTHON seems # to want the function to actually CREATE the variable as well as assigning it a value. Creating the variable outside the function # and then using the function to assign it a value doesn't seem to work very well. # global purchasePrice dollars = random.randrange(100) purchasePrice = str(dollars) + "." + makeCents() def getAmountInWallet() : amount_in_wallet = random.randrange(100) return str(amount_in_wallet) def cls() : textWindow.delete(0.0,END) reset() def reset() : enough_cash = 0 responseType = -1 makePurchasePrice() getAmountInWallet() app = Frame(root) app.grid() textWindow = ScrolledText(app, wrap = WORD) textWindow.grid(row = 1, columnspan = 6) app.rt = StringVar() Radiobutton(app, text = "Affirmative response", variable = app.rt, value = "ar").grid(row = 3, column = 0, sticky = W) Radiobutton(app, text = "Negative response", variable = app.rt, value = "nr").grid(row = 3, column = 1, sticky = W) rtButton = Button(app) rtButton.grid(row = 3, column = 4, sticky = W) rtButton.configure(text = "Answer", command = getLocalResponse) rtButton startButton = Button(app) startButton.grid(row = 0,column = 3, sticky = W) startButton.configure(text = "Start", command = beginCode) clsButton = Button(app) clsButton.grid(row = 0, column = 4, sticky = W) clsButton.configure(text = "Clear", command = cls) participants = ("Mel","Bob") role_part = ("clerk","customer") prop_parts = "check" notation_property = ("<",">") notation_system = ("[","]") top_level_task = ("begin purchase","get purchase amount") gpa = ("check for cash", "pay with cash") ic = ("insufficient cash","get permission for check") gpc = ("local inquiry","local response","store policy") lcq = ("Is it a local check?", "Is it a check on a local bank?") sc = ("I have the cash","I have enough cash to pay for this", "Here's the cash. Keep the change") pbc = ("I don't have the cash. Can I pay with a check?","Can I pay with a check?","Let me write you a check.","I don't have enough cash. May I pay by check?") sp = ("I can only take a local check with id, cash or a credit card. \n\t\t\tSorry.", "We only take local checks.", "Out of area checks give us problems. We don't take them.","Only cash, credit cards or local checks with id.") # # prompt is a constant # prompt = " ==>\t\t" # # The next two tuples create affirmative responses (ar) and negative responses (nr) # ar = ("yes", "sure", "of course...", "uh huh", "head nod", "yep") nr = ("no", "nope", "I don't think so", "head shake") # enough_cash = 0 responseType = -1 rNum = 0 root.mainloop()