I have programmed an App with Tkinter on Python, that reads out a number of consecutive XML-protocols of a (literary) writing process and visualizes the same process in a Tkinter Text widget. The program is already working, a video version of the result can be watched here: https://www.youtube.com/watch?v=h3q8F8SEc68
In the video posted, I recorded a screen video and speeded up the video. Now, however, I want to be able to speed up the process on program-level. Goal is, as in the video, to synchronzie the whole process with a reading of the - naturally much shorter - product, but live, this time.
Somehow I can't seem to get the program to be running more quickly and am wondering, which is the limiting factor here. I have ruled out XML-parsing as the reason by separating parsing and visualization. In this first step the XML-files are rearranged into a list of dictionaries. An example:
chain_of_events = [...,
{'type': 'input',
'value': 'h',
'delay': 254,
'position': 648},
{'type': 'input',
'value': 'e',
'delay': 542,
'position': 649},
{'type': 'cursor',
'delay': 928,
'position': 648},
{'type': 'input',
'value': 'T',
'delay': 542,
'position': 648}, ...]
In the second step this list is processed to rearrange the writing-process. This is done mostly via a single function process()
, that calls itself repeatedly. It is not practical to post all of the (messy) code here, I will instead focus on the part, that does the actual writing:
def process():
global chain_of_events
if chain_of_events != []:
action = chain_of_events.pop(0)
delay = action["delay"]
if action["type"] == "cursor":
if txt.tag_ranges(tk.SEL):
txt.tag_remove(tk.SEL, tk.SEL_FIRST, tk.SEL_LAST) #remove existing selections
txt.mark_set("insert", "1.0 + %d chars" % (action["position"])) #set cursor position
elif action["type"] == "delete":
txt.delete("1.0 + %d chars" % (action["start"]), "1.0 + %d chars" % (action["end"])) #deletion
txt.mark_set("insert", "1.0 + %d chars" % (action["start"])) #set cursor position
elif action["type"] == "input":
txt.insert("insert", action["value"]) #input text
window.after(delay, process()) #rinse and repeat
# main program
window = tkinter.Tk()
txt = tkinter.Text(window)
process()
window.mainloop()