def add_note(self, note, input_event): # find insertion index of the note insert_index = self.index(tkinter.INSERT) # if the inputted key is a `, then run the break apart event if note == "`": self.break_note(insert_index) return # check if at end of the score if int(insert_index.split(".")[1]) == len(self.get(1.0, "end-1c")): # PROBLEM HERE # if it was a keyboard event, there was an attempt to add text to the text area, remove 2 characters # if not keyboard, remove one character from the end (just whitespace) if input_event: string = self.get(1.0, "end-2c") else: string = self.get(1.0, "end-1c") # convert to upper case for easy keyboard parsing current_note = note.upper() # nuke the current text to make sure nothing i dont want added is added self.delete("1.0", tkinter.END) self.insert(tkinter.END, string) # check validity of octave 2 (only high A is allowed) if Variables.octave == 2 and ord(current_note) > 65: return # check if current note fits in the measure if Variables.current_measure_length + (1.0 / float(Variables.note_length)) > Variables.get_measure_length(): messagebox.showerror("Invalid Configuration", "ERROR: This note is too long to fit in this measure!") return # check if the note exists in the naturals table (cross-reference keyboard input to see if valid input) if current_note in note_lookup.naturals: # check if rest, then add the rest symbol from the lookup class if so if current_note == "R": append = LookupNote.get_rest(Variables.note_length) # if not a rest, append the note from the symbol generation in the lookup class else: append = LookupNote.get_note(current_note, Variables.octave, Variables.note_length, Variables.accidental, Variables.key_sig) # insert note self.insert(tkinter.END, append) else: return # play tone so the user knows what they just added Playback.play_tone(current_note, Variables.octave, Variables.accidental) # update the current measure length Variables.current_measure_length += 1.0 / float(Variables.note_length) # compare to see if the measure length was just finished, if so add a measure line if math.isclose(Variables.get_measure_length(), Variables.current_measure_length): Variables.current_measure_length = 0.0 self.insert(tkinter.END, "!=") # save the new text so it can be compared again if needed on next input event self.save_last_text() else: # note is not at the end of the score # get the current character at the insertion index split = insert_index.split(".") next_index = split[0] + "." + str(int(split[1]) + 1) character = self.get(insert_index, next_index) # if the note is one that can be replaced if LookupNote.replaceable(character): # parse the insertion index first_half = self.get(1.0, split[0] + "." + str(int(split[1]) - 1)) second_half = self.get(split[0] + "." + str(int(split[1]) + 2), "end-1c") # generate new note from the lookup note class using current settings new_note = LookupNote.get_note(note.upper(), Variables.octave, LookupNote.get_note_length(character), Variables.accidental, Variables.key_sig) # if there is an accidental symbol, remove it if 208 <= ord(first_half[-1]) <= 254: first_half = first_half[0:len(first_half) - 1] # delete the old text, insert the new generated text with the new symbol in the insertion index self.delete(1.0, tkinter.END) self.insert(tkinter.END, first_half + new_note + second_half) # play the tone so they know what they inputted Playback.play_tone(note.upper(), Variables.octave, Variables.accidental) # save the text for future comparisons self.save_last_text() else: # cancel the event if replacement is impossible self.cancel_event()