scientific-programming-exer.../exam/ex03/callbacks.py
2019-02-02 16:59:18 +01:00

79 lines
2.5 KiB
Python

from gi.repository import Gtk
from settings import load_words_from_file, manually_add_question, save_questions_to_file
from exercise import Question
class CallbackHandler(object):
def __init__(self, gtk_builder):
self.builder = gtk_builder
self.question = None
def on_manually_add_submit_button_clicked(self, widget):
question_view = self.builder.get_object("add_question_question_view")
answer_view = self.builder.get_object("add_question_answer_view")
question = question_view.get_text()
answer = answer_view.get_text()
try:
manually_add_question(question, answer)
except:
return
question_view.set_text("")
answer_view.set_text("")
def on_load_from_file_chooser_file_set(self, widget):
file_chooser = self.builder.get_object("load_from_file_chooser")
file_name = file_chooser.get_filename()
load_words_from_file(file_name)
def on_save_questions_to_file_button_clicked(self, widget):
dialog = Gtk.FileChooserDialog("Please choose a file", None
, Gtk.FileChooserAction.SAVE
, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL
, Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
response = dialog.run()
if(response == Gtk.ResponseType.CANCEL):
dialog.destroy()
return
file_name = dialog.get_filename()
dialog.destroy()
save_questions_to_file(file_name)
def on_submit_answer_button_clicked(self, widget):
answer_view = self.builder.get_object("answer_view")
correct_answer_view = self.builder.get_object("correct_answer_view")
answer_was_correct_view = self.builder.get_object("answer_was_correct_view")
dispatch_answer_was_correct = {True: "That is correct:"
, False: "Wrong, correct was:"}
submit_answer_button = widget
submit_answer_button.set_sensitive(False)
answer = answer_view.get_text()
was_correct = self.question.submit_answer(answer)
correct_answer_view.set_text(self.question.answer)
answer_was_correct_view.set_text(dispatch_answer_was_correct[was_correct])
def on_next_question_button_clicked(self, widget):
answer_view = self.builder.get_object("answer_view")
correct_answer_view = self.builder.get_object("correct_answer_view")
answer_was_correct_view = self.builder.get_object("answer_was_correct_view")
submit_answer_button = self.builder.get_object("submit_answer_button")
question_view = self.builder.get_object("question_view")
submit_answer_button.set_sensitive(True)
answer_view.set_text("")
correct_answer_view.set_text("")
answer_was_correct_view.set_text("")
self.question = Question.fetch()
question_view.set_text(self.question.question)