class MainWindow(Tk): def __init__(self): Tk.__init__(self) self.wm_title("FRCA QBase Reader - Exam Mode") w, h = self.winfo_screenwidth(), self.winfo_screenheight() self.geometry("%dx%d+0+0" % (w, h)) self.bind("<Down>", self.OnSmaller) self.bind("<Up>", self.OnBigger) self.bind("<Right>", self.nextQuestion) self.logic = Logic() self.widgetwraplist = [] self.text = self.logic.prepareQuestion() self.font = tkFont.Font(size=24) self.stmtvar = StringVar() self.stmtvar.set(self.text['question'][0]) self.statement = Label(self, textvariable=self.stmtvar, font=self.font) self.statement.pack(anchor = W) self.widgetwraplist.append(self.statement) self.checkbuttons = {} self.checkbtntext = {} self.checkbuttonvar = {} for i in range(1, 6): self.checkbuttonvar[str(i)] = BooleanVar() self.checkbtntext[str(i)] = StringVar() self.checkbtntext[str(i)].set(self.text['question'][i]) self.checkbuttons['question '+str(i)] = InvokableCheckbutton(self, textvariable = self.checkbtntext[str(i)], variable = self.checkbuttonvar[str(i)], font=self.font) self.checkbuttons['question '+str(i)].pack(anchor = W) self.widgetwraplist.append(self.checkbuttons['question '+str(i)]) self.bind("<Key-"+str(i)+">", self.checkbuttons['question '+str(i)].customInvoke) self.explvar = StringVar() self.explvar.set(' '.join(self.text['question'][6:])) self.explanation = Label(self, textvariable=self.explvar, font=self.font, justify=LEFT) self.widgetwraplist.append(self.explanation) self.buttonNext = Button(self, text = 'Next question', command = lambda: self.nextQuestion(1), font=self.font) self.buttonNext.pack(anchor = S) def nextQuestion(self, event): given_answers = [] self.logic.questioncount += 1 for i in range(1, 6): given_answers.append(self.logic.str2bool(self.checkbuttonvar[str(i)].get())) self.logic.questiondict[self.logic.questionpath]['given answers'] = given_answers if (self.logic.questiondict[self.logic.questionpath]['correct answers'] == self.logic.questiondict[self.logic.questionpath]['given answers']): self.logic.questiondict[self.logic.questionpath]['is correct'] = True else: self.logic.questiondict[self.logic.questionpath]['is correct'] = False if self.logic.questioncount < self.logic.EXAM_END: if self.logic.questioncount == self.logic.EXAM_END-1: self.buttonNext.configure(text='Finish exam') self.text = self.logic.prepareQuestion() self.stmtvar.set(self.text['question'][0]) self.explvar.set(' '.join(self.text['question'][6:])) self.explanation.pack_forget() for i in range(1, 6): self.checkbuttons['question '+str(i)].configure(fg='black') self.checkbuttonvar[str(i)].set(False) self.checkbtntext[str(i)].set(self.text['question'][i]) else: self.stmtvar.set('You scored {}%'.format(str(self.logic.calculateScore()))) if self.logic.calculateScore() >= 80: self.statement.configure(foreground='#009E18') else: self.statement.configure(foreground='red') for i in range(1, 6): self.checkbuttons['question '+str(i)].pack_forget() if self.logic.calculateScore() == 100: self.buttonNext.configure(text='Start a new exam') self.buttonNext.configure(command=lambda: self.startNewExam(1)) self.bind("<Right>", self.startNewExam) else: self.bind("<Right>", self.reviewWindow) self.buttonNext.configure(text='Review wrong answers') self.buttonNext.configure(command= lambda: self.reviewWindow(1)) def reviewWindow(self, event): self.reviewed = 0 self.buttonNext.configure(text='Next') self.buttonNext.pack_forget() for i in range(1, 6): self.checkbuttons['question '+str(i)].pack(anchor = W) self.explanation.pack(anchor = W) self.buttonNext.pack(anchor = S) self.showAnswer(1) def showAnswer(self, event): if self.reviewed == 0: self.bind("<Right>", self.showAnswer) self.buttonNext.configure(command= lambda: self.showAnswer(1)) for q in self.logic.questiondict: if self.logic.questiondict[q]['is correct'] == False: f = open(q, 'r') lines = self.logic.parseQuestion(f) self.stmtvar.set(lines['question'][0]) self.statement.configure(foreground='black') self.explvar.set(' '.join(lines['question'][6:])) for i in range(1, 6): self.checkbtntext[str(i)].set(lines['question'][i]) self.checkbuttonvar[str(i)].set(self.logic.questiondict[q]['given answers'][i-1]) if (self.logic.questiondict[q]['correct answers'][i-1] == self.logic.questiondict[q]['given answers'][i-1]): self.checkbuttons['question '+str(i)].configure(fg='#009E18') else: self.checkbuttons['question '+str(i)].configure(fg='red') del self.logic.questiondict[q] self.reviewed += 1 break if self.reviewed == self.logic.EXAM_END: self.buttonNext.configure(text='Start a new exam') self.buttonNext.configure(command=lambda: self.startNewExam(1)) self.bind("<Right>", self.startNewExam) def startNewExam(self, event): self.logic.questioncount = 0 self.questiondict = {} self.text = self.logic.prepareQuestion() self.stmtvar.set(self.text['question'][0]) for i in range(1, 6): self.checkbuttons['question '+str(i)].configure(fg='black') self.checkbuttonvar[str(i)].set(False) self.checkbtntext[str(i)].set(self.text['question'][i]) self.explanation.pack_forget() self.buttonNext.configure(text='next') self.buttonNext.configure(command=lambda: self.nextQuestion(1)) self.bind("<Right>", self.nextQuestion) def OnBigger(self, event): '''Make the font 2 points bigger''' size = self.font['size'] self.font.configure(size=size+2) def OnSmaller(self, event): '''Make the font 2 points smaller''' size = self.font['size'] self.font.configure(size=size-2) def wrapWidgets(self): self.update() for widget in self.widgetwraplist: widget.configure(wraplength=self.winfo_width())
class DrawDigit(object): def __init__(self, digitsData): self.digitsData = digitsData self.checkingDigit = False self.master = Tk() self.master.title("Draw Digit") self.master.config(bg='grey',width=canvasWidth*2, height=625,padx=50,pady=50) self.isClicking = False self.currentX = -1 self.currentY = -1 self.canvas = Canvas(self.master, width=canvasWidth, height=canvasHeight) self.canvas.config(bg='white') self.canvas.pack() self.recognizeButton = Button(self.master, text="Recognize", command=self.recognize) self.recognizeButton.pack() self.clearButton = Button(self.master, text="Clear", command=self.clearCanvas) self.clearButton.pack() self.rightButton = Button(self.master, text="Right!", command=self.digitRecognized) self.rightButton.pack_forget() self.wrongButton = Button(self.master, text="Wrong!", command=self.digitNotRecognized) self.wrongButton.pack_forget() self.retryButton = Button(self.master, text="Retry!", command=self.resetGUI) self.retryButton.pack_forget() self.master.bind('<Button-1>', self.mousePress) self.master.bind('<B1-Motion>', self.mouseMove) self.master.bind('<ButtonRelease-1>', self.mouseRelease) self.image = Image.new("L",(canvasWidth,canvasHeight)) self.draw = ImageDraw.Draw(self.image) return def mousePress(self, event): if not self.isClicking and not self.checkingDigit: self.currentX = event.x self.currentY = event.y self.isClicking = True return def mouseMove(self, event): if self.isClicking and not self.checkingDigit: self.draw.line([(self.currentX,self.currentY),(event.x,event.y)],(0,0,0),width=5) self.canvas.create_line(self.currentX, self.currentY, event.x, event.y, width=5.0) self.currentX = event.x self.currentY = event.y return def mouseRelease(self, event): self.isClicking = False return def clearCanvas(self): self.canvas.delete('all') self.image = Image.new("L",(canvasWidth,canvasHeight)) self.draw = ImageDraw.Draw(self.image) return def recognize(self): if len(self.canvas.find_all()) != 0: self.checkingDigit = True self.originalImage = ImageOps.invert(self.image) print "***************************************************" print 'Recognizing the digit...' featureVector = ocr_machine_learning.getVector(self.originalImage, self.digitsData) print 'The feature vector for the image is: {0}'.format(featureVector) finalDigit = ocr_machine_learning.recognizeDigit(featureVector) print 'The digit in the image is:' print digitASCII.digits[finalDigit] self.checkCorrectDigitGUI(finalDigit) return def startGUI(self): print 'Please draw the digit in the white square of the GUI window...' self.master.mainloop() return def checkCorrectDigitGUI(self, finalDigit): self.finalDigit = finalDigit self.recognizeButton.pack_forget() self.clearButton.pack_forget() self.rightButton.pack() self.wrongButton.pack() self.retryButton.pack() self.master.update() return def digitRecognized(self): ocr_utils.saveImageToCorrectDigitFolder(self.originalImage, self.finalDigit) self.resetGUI() return def digitNotRecognized(self): ocr_utils.guessedWrong(self.originalImage) self.resetGUI() return def resetGUI(self): self.clearCanvas() self.rightButton.pack_forget() self.wrongButton.pack_forget() self.retryButton.pack_forget() self.canvas.pack() self.recognizeButton.pack() self.clearButton.pack() self.checkingDigit = False print "***************************************************" print "Please draw another digit..." self.master.update() return