scientific-programming-exer.../exam/ex03/callbacks.py

79 lines
2.5 KiB
Python
Raw Permalink Normal View History

2019-01-31 18:53:49 +00:00
from gi.repository import Gtk
2019-01-31 16:51:05 +00:00
from settings import load_words_from_file, manually_add_question, save_questions_to_file
2019-01-31 18:53:49 +00:00
from exercise import Question
2019-01-31 16:51:05 +00:00
class CallbackHandler(object):
def __init__(self, gtk_builder):
self.builder = gtk_builder
2019-01-31 18:53:49 +00:00
self.question = None
2019-01-31 16:51:05 +00:00
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()
2019-01-31 18:53:49 +00:00
try:
manually_add_question(question, answer)
except:
return
question_view.set_text("")
answer_view.set_text("")
2019-01-31 16:51:05 +00:00
2019-01-31 18:53:49 +00:00
def on_load_from_file_chooser_file_set(self, widget):
2019-01-31 16:51:05 +00:00
file_chooser = self.builder.get_object("load_from_file_chooser")
file_name = file_chooser.get_filename()
load_words_from_file(file_name)
2019-01-31 18:53:49 +00:00
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()
2019-01-31 16:51:05 +00:00
save_questions_to_file(file_name)
2019-01-31 18:53:49 +00:00
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)
2019-01-31 16:51:05 +00:00