# File: upc.py # based on upc.py from Tkinter import * master = Tk() bigFont = "Helvetica 24" fsaWidth = 300 fsaHeight = 300 charHeight = 25 charWidth = 10 lightSize = 30 epsilon = 4 def makeFsa(fsa): recX1 = fsaWidth/3 recX2 = fsaWidth*2/3 CY1 = charHeight*5 BY1 = charHeight*3 AY1 = charHeight*1 fsa.create_rectangle(recX1, 0, recX2, fsaHeight/2, fill="black",tags="box",width=0) fsa.create_text(recX1-charWidth,AY1,text="A",tag="in",font=bigFont) fsa.create_text(recX2+charWidth,AY1,text="A",tag="out",fill="blue",font=bigFont) fsa.create_text(recX1-charWidth,BY1,text="B",tag="in",font=bigFont) fsa.create_text(recX2+charWidth,BY1,text="B",tag="out",fill="blue",font=bigFont) fsa.create_text(recX1-charWidth,CY1,text="C",tag="in",font=bigFont) fsa.create_text(recX2+charWidth,CY1,text="C",tag="out",fill="blue",font=bigFont) CX1 = fsaWidth/3-charWidth*5 inX1 = recX1-2*charWidth fsa.create_line(inX1,CY1,CX1,CY1,fill="blue",arrow="first") CX2 = recX2+charWidth*5 inX2 = recX2+2*charWidth CY2 = CY1 + charWidth*4 fsa.create_line(CX1,CY1,CX1,CY2,fill="blue") fsa.create_line(CX1,CY2,CX2,CY2,fill="blue") fsa.create_line(CX2,CY1,CX2,CY2,fill="blue") fsa.create_line(inX2,CY1,CX2,CY1,fill="blue") BX1 = CX1-charWidth fsa.create_line(inX1,BY1,BX1,BY1,fill="blue",arrow="first") BY2 = CY2 + charWidth fsa.create_line(BX1,BY1,BX1,BY2,fill="blue") BX2 = CX2+charWidth fsa.create_line(BX1,BY2,BX2,BY2,fill="blue") fsa.create_line(BX2,BY2,BX2,BY1,fill="blue") fsa.create_line(BX2,BY1,inX2,BY1,fill="blue") AX1 = BX1-charWidth fsa.create_line(inX1,AY1,AX1,AY1,fill="blue",arrow="first") AY2 = BY2 + charWidth fsa.create_line(AX1,AY1,AX1,AY2,fill="blue") AX2 = BX2+charWidth fsa.create_line(AX1,AY2,AX2,AY2,fill="blue") fsa.create_line(AX2,AY2,AX2,AY1,fill="blue") fsa.create_line(AX2,AY1,inX2,AY1,fill="blue") fsa = Canvas(master, width=fsaWidth, height=fsaHeight*2/3) makeFsa(fsa) fsa.grid(row=1,column=0,rowspan=8,columnspan=2) Label(master,text="Button X",font=bigFont).grid(row=0,column=0) X = False # let go of the button def release(): global X X = False # push the button def push(event): global X X = True button = Button(master,command=release) button.grid(row=0,column=1) button.bind("", push) # def foo(e): # print "gotcha" entryWidth = 40 enter = range(3) entry = range(3) label = range(3) for i in range(3): label[i] = Label(master, text=chr(ord('A')+i),fg="blue",bg="white") label[i].grid(row=i,column=2) enter[i] = StringVar() entry[i] = Entry(master, width=entryWidth,textvariable=enter[i]) entry[i].insert(INSERT,"False") entry[i].grid(row=i,column=3) # entry[i].bind('',foo) enterLight = range(8) entryLight = range(8) canvasLight = range(8) ovalLight = range(8) color = ["red","blue","green","purple","orange","red","green","yellow"] for i in range(8): canvasLight[i] = Canvas(master,height=lightSize,width=lightSize) canvasLight[i].grid(row=i+3,column=2) ovalLight[i] = canvasLight[i].create_oval(epsilon,epsilon,lightSize-epsilon,lightSize-epsilon,fill=color[i]) enterLight[i] = StringVar() entryLight[i] = Entry(master, width=entryWidth, textvariable=enterLight[i]) entryLight[i].insert(INSERT,"False") entryLight[i].grid(row=i+3,column=3) (A,B,C) = (False,False,False) def update(): global A global B global C global X newval = range(3) for i in range(3): try: newval[i] = eval(enter[i].get()) except: newval[i] = False if newval[i]: label[i].configure(bg="yellow") else: label[i].configure(bg="white") # next click (A,B,C) = newval # new lights for i in range(8): try: val = eval(enterLight[i].get()) except: val = False if val: canvasLight[i].itemconfig(ovalLight[i],fill=color[i]) else: canvasLight[i].itemconfig(ovalLight[i],fill="white") master.after(1000, update) master.after(1000, update) master.mainloop()