I have been working on one application where in I want to autocomplete things like brackets round curly square. I had done that using the insert method of tkinter with the tk.INSERT
constant as index like so:
def autocomplete(self, val) :
if val == '(' :
self.insert(tk.INSERT, ')')
elif val == '{' :
self.insert(tk.INSERT, '}')
elif val == '[' :
self.insert(tk.INSERT, ']')
elif val == '\'' :
self.insert(tk.INSERT, '\'')
self.mark_set('sentinel', str(float(self.index(tk.INSERT)) - 0.1))
elif val == '\"' :
self.insert(tk.INSERT, '\"')
self.mark_set('sentinel', str(float(self.index(tk.INSERT)) - 0.1))
elif val == ':' :
text = self.get(1.0, tk.INSERT).strip().replace(' ', '')
if text[(text.index(':') - 1) : text.index(':')] == ')' :
self.insert(tk.INSERT, '\n\t')
return
This is a function which is within a text widget where the init has the following bindings attached:
self.bind('(', lambda x : self.autocomplete('('))
self.bind('{', lambda x : self.autocomplete('{'))
self.bind('[', lambda x : self.autocomplete('['))
self.bind(':', lambda x : self.autocomplete(':'))
self.bind('\'', lambda x : self.autocomplete('\''))
self.bind('\"', lambda x : self.autocomplete('\"'))
And when I type any of these like lets take the e.g. of brackets, then it shows the output somewhat not right.
)(
Here is also a snapshot of the same:
You see I want the other bracket to appear at the end but it does not.