class ListView(Frame): def __init__(self, parent, height, width, border_style, border_width, background_colour): #Call Frams's __init__ Frame.__init__(self, parent) #create the frame that holds the widgets on the left lists_frame = Frame(self) #Add custom ListPane widget self.lists_list_pane = ListPane(lists_frame, height = 25) self.lists_list_pane.singleClickFunc = self.displayWords #Add labels and Buttons lists_label = Label(lists_frame, text="Word Lists") import_button = Button(lists_frame, text="Import", command=self.importList) export_button = Button(lists_frame, text="Export", command=self.exportList) #Grid everything lists_label.grid(row=0, column=0, sticky=W, pady=3, padx=2) self.lists_list_pane.grid(row=1, column=0, columnspan=2) import_button.grid(row=2, column=0, sticky=W, padx=2) export_button.grid(row=2, column=0) #create the container frame that holds the widgets on the right words_frame = Frame(self) #create custom ListPane widget self.words_list_pane = ListPane(words_frame,height = 25) self.words_list_pane.singleClickFunc = self.displayWordInfo #create labels and buttons self.speak_word_button = Button(words_frame, text="Speak Word", command=self.speakWord, state=DISABLED) self.speak_list_button = Button(words_frame,text="Speak List", command=self.speakList, state=DISABLED) stop_speech_button = Button(words_frame,text="Stop Speech", command=self.stopSpeech) words_label = Label(words_frame, text="Words in List") #grid everything words_label.grid(row=0, column=0, sticky=W, pady=3, padx=2) self.words_list_pane.grid(row=1, column=0, columnspan=3) self.speak_word_button.grid(row=2, column=0) self.speak_list_button.grid(row=2, column=1) stop_speech_button.grid(row=2, column=2) #Create the infoFrame info_frame = Frame(self, height=140, width=width, bd=border_width, bg=background_colour, relief=border_style) self.info_text = Text(info_frame, state=DISABLED, font=tkFont.Font(family="Tahoma", size=9)) self.info_text.pack(fill=BOTH) lists_frame.grid(column=0, row=0) words_frame.grid(column=1, row=0) info_frame.grid(column=0, row=1, columnspan=2) info_frame.pack_propagate(0) #Create databse and Festival connections self.db = SpellingDatabase() self.fest = FestivalInterface() list_records = self.db.getLists() list_names = [] for row in list_records: list_names.append(row[1]) self.lists_list_pane.display(list_names) self.words_list_pane.display(['...']) def importList(self): filename = tkFileDialog.askopenfilename(filetypes = (("Tldr Files", "*.tldr"),)) if str(filename) == "": return tldr_parse = TldrParser(filename) list_name = filename[:-5].split('/')[-1] if self.db.importList(list_name, tldr_parse.getWordList(), tldr_parse.getSource(), tldr_parse.getDateEdited(), tldr_parse.getListSize()): self.lists_list_pane.insert(list_name) else: print "List with that name already exists!!!" self.db.commit() def exportList(self): filename = tkFileDialog.asksaveasfilename(filetypes = (("Tldr Files", "*.tldr"),), initialfile = self.lists_list_pane.get()) if str(filename) == "": return export_list = self.db.getList(self.lists_list_pane.get())[0] word_list = self.db.getWords(self.lists_list_pane.get()) word_dict = {} for word in word_list: word = word[0] word_record = self.db.getWord(word)[0] word_dict[word] = [word_record[2],word_record[3],word_record[4]] tldr_parse = TldrParser() tldr_parse.setWordList(word_dict) tldr_parse.setSource(export_list[2]) tldr_parse.setDateEdited(str(date.today())) tldr_parse.setListSize(export_list[4]) tldr_parse.writetldr(filename) def speakWord(self): word = self.words_list_pane.get() self.fest.speech(word) def speakList(self): word_list = self.words_list_pane.getDisplayed() long_sentence = "" for word in word_list: long_sentence += " %s," % (word) self.fest.speech(long_sentence) def stopSpeech(self): self.fest.resetSpeech() def displayWords(self, item_index): word_records = self.db.getWords(self.lists_list_pane.get(item_index)) word_names = [] for row in word_records: word_names.append(str(row[1])) word_names.sort(key=str.lower) self.words_list_pane.display(word_names) list_record = self.db.getList(self.lists_list_pane.get(item_index)) if len(list_record) > 0: list_record = list_record[0] info_string = """List: %s Source: %s Date Edited: %s Number of Words in List: %s""" % (list_record[1], list_record[2], list_record[3], list_record[4]) self.info_text.configure(state=NORMAL) self.info_text.delete(1.0, END) self.info_text.insert(1.0, info_string) self.info_text.configure(state=DISABLED) self.speak_word_button.configure(state=DISABLED) self.speak_list_button.configure(state=NORMAL) else: self.info_text.configure(state=NORMAL) self.info_text.delete(1.0, END) self.info_text.insert(1.0, "There are no lists in the database.\nImport one or create one") self.info_text.configure(state=DISABLED) def displayWordInfo(self, item_index): word_record = self.db.getWord(self.words_list_pane.get(item_index)) if len(word_record) > 0: word_record = word_record[0] info_string = """Word: %s Definition: %s Usage: "%s" Difficulty: %s""" % (word_record[1], word_record[2], word_record[3], word_record[4]) self.info_text.configure(state=NORMAL) self.info_text.delete(1.0, END) self.info_text.insert(1.0, info_string) self.info_text.configure(state=DISABLED) self.speak_word_button.configure(state=NORMAL) def update(self): list_records = self.db.getLists() list_names = [] for row in list_records: list_names.append(row[1]) self.lists_list_pane.display(list_names) self.words_list_pane.display(['...'])
class SpellingGame(Tk): """Main class for spelling aid. Manages the other screens and holds festival and database connections""" def __init__(self): Tk.__init__(self) self.title('Spelling Game') #Estabilish database connection self.db = SpellingDatabase() #Get words from database self.word_list = self.get_dictionary() #Retrieve list of lists from db self.list_list = self.get_lists() #Start festival process self.festival = FestivalInterface() #Create screens self.init_gui() def get_lists(self): """Retrieve list of lists from database""" list_records = self.db.sql("""SELECT * FROM lists""") list_list = [] for wordlist in list_records: list_list.append(WordList(wordlist)) return list_list def update_lists(self): """Refresh the list-list""" self.list_list = self.get_lists() def get_dictionary(self): """Get dictionary words from database""" word_records = self.db.sql("""SELECT * FROM words WHERE difficulty LIKE 'SB%'""") word_list = [] for word in word_records: word_list.append(Word(word)) return word_list def get_random_list(self, length): """Return a random list of words""" words = random.sample(self.word_list, length) return words def login(self, user): """Show the start screen as user logs in""" self.user = user self.login_frame.pack_forget() self.start_frame.welcomeMessage() self.start_frame.pack() def start_game(self, word_list): """Hide start screen and show game screen. Retrieve words for selected list""" self.current_list = word_list if word_list.name != "Random List": word_records = self.db.getWords(word_list.name) self.current_list.words = [Word(word) for word in word_records] else: self.current_list.words = self.get_random_list(15) self.list_length = len(self.current_list.words) self.start_frame.pack_forget() self.results_frame.pack_forget() self.game_frame = GameFrame(self) self.game_frame.start() self.game_frame.pack() def show_results(self, time_elapsed): """Hide game frame and show results frame""" self.game_frame.pack_forget() self.results_frame.calculate(self.current_list, time_elapsed) self.results_frame.pack() def new_list(self): """Show start screen, hiding either results or list editor""" self.results_frame.pack_forget() self.list_editor.pack_forget() self.start_frame.update_list() self.start_frame.pack() def show_editor(self): """Hide start frame and show list editor""" self.start_frame.pack_forget() self.list_editor.pack() def init_gui(self): """Create the screens""" self.login_frame = LoginFrame(self) self.login_frame.pack() self.start_frame = StartFrame(self) self.results_frame = ResultsFrame(self) self.list_editor = ListEditor(self) self.mainloop()
class SpellingGame(Tk): def __init__(self): Tk.__init__(self) self.title('Spelling Game') self.db = SpellingDatabase() self.word_list = self.get_dictionary() self.list_list = self.get_lists() self.festival = FestivalInterface() self.init_gui() def get_lists(self): list_records = self.db.sql("""SELECT * FROM lists""") list_list = [] for wordlist in list_records: list_list.append(WordList(wordlist)) return list_list def update_lists(self): self.list_list = self.get_lists() def get_dictionary(self): word_records = self.db.sql("""SELECT * FROM words WHERE difficulty LIKE 'SB%'""") word_list = [] for word in word_records: word_list.append(Word(word)) return word_list def get_random_list(self, length): words = random.sample(self.word_list, length) return words def login(self, user): self.user = user self.login_frame.pack_forget() self.start_frame.welcomeMessage() self.start_frame.pack() def start_game(self, word_list): self.current_list = word_list if word_list.name != "Random List": word_records = self.db.getWords(word_list.name) self.current_list.words = [Word(word) for word in word_records] else: self.current_list.words = self.get_random_list(15) self.list_length = len(self.current_list.words) self.start_frame.pack_forget() self.results_frame.pack_forget() self.game_frame = GameFrame(self) self.game_frame.start() self.game_frame.pack() def show_results(self, time_elapsed): self.game_frame.pack_forget() self.results_frame.calculate(self.current_list, time_elapsed) self.results_frame.pack() def new_list(self): self.results_frame.pack_forget() self.list_editor.pack_forget() self.start_frame.update_list() self.start_frame.pack() def show_editor(self): self.start_frame.pack_forget() self.list_editor.pack() def init_gui(self): self.login_frame = LoginFrame(self) self.login_frame.pack() self.start_frame = StartFrame(self) self.results_frame = ResultsFrame(self) self.list_editor = ListEditor(self) self.mainloop()