I'm trying to add notes to a given text that's not to be altered. It's pretty much like formatting in common text editors.
I have tried to get the current cursor position via .index('insert'), and then tag the character either forwards with tag_add(current cursor, current cursor '+1c'), or backwards with tag_add(current cursor + '-1c', current cursor). This results in tagging either the character right before or after the character just typed.
Are there any workarounds to live-tag the actually typed character?
import tkinter
main = tkinter.Tk()
def typing(event):
text.tag_configure('note', background='yellow')
text.tag_configure('note2', background='blue')
cur_cursor = text.index("insert")
text.tag_add('note', cur_cursor + '-1c', cur_cursor)
text.tag_add('note2', cur_cursor, cur_cursor + '+1c')
text = tkinter.Text(main)
text.grid()
text.bind('<Key>', typing)
for i in ['OX'*20 + '\n' for i in range(10)]:
text.insert('end', i)
main.mainloop()
Edit: Although Bryan's answer worked for me you might get problems with fast typing as described here: how-to-get-cursor-position
The simplest solution is to bind on <KeyRelease> rather than <Key>. The reason is that the text widget doesn't actually insert the character you typed until its own <Key> binding fires, and that binding always fires after any custom bindings you have on the widget.
Related
I want to move the insertion cursor(caret)(|) within the tkinter text widget, to a certain text widget indice.
USE CASE:
I am making an autocomplete program wherein if I type one single apostrophe(quotation mark), the other one automatically is inserted into the text widget, which I got working all fine. But after, the second apostrophe is generated I want to bring the insertion cursor(caret), in the middle of the two apostrophes rather than at the end. Like so -:
One apostrophe typed(inside the tkinter text widget) -:
'[insertion cursor(caret)(|)]
The other one is auto-inserted pushing the insertion cursor(caret) to the end(The code till this part has been figured out by me successfully.) -:
''[insertion cursor(caret)(|)]
The insertion cursor(caret) shifts between the two apostrophes(The objective of this question.) -:
'[insertion cursor(caret)(|)]'
NOTE: These operations are all taking place within a tkinter text widget.
You call the mark_set method, using "insert" as the name of the mark.
the_widget.mark_set("insert", "4.0") will set the insertion cursor at the start of the fourth line. You can use the_widget.mark_set("insert", "insert-1c") to move the cursor back one character.
Here is an example of one way for automatically inserting a closing parenthesis or bracket. In this example, the code inserts both the opening and closing character so it returns "break" to prevent the default behavior provided by the text widget.
import tkinter as tk
def autocomplete(widget, first, last):
# insert the two characters at the insertion point
widget.insert("insert", first + last)
# move insertion cursor back one character
widget.mark_set("insert", "insert-1c")
# prevent the text widget from inserting the character that
# triggered the event since we've already inserted it.
return "break"
root = tk.Tk()
text = tk.Text(root, wrap="word")
text.pack(fill="both", expand=True)
text.bind("(", lambda event: autocomplete(event.widget, "(", ")"))
text.bind("[", lambda event: autocomplete(event.widget, "[", "]"))
root.mainloop()
I am writing a program to assist with a trivial part of my job that can be automated. My purpose here is to:
Copy and paste a chunk of plain text into a Tkinter text widget
Use that pasted chunk of text as the value of a variable so that the variable can have certain characters pulled and returned down the line.
I have a functioning little bit of code. For example, here is my text widget and the lines of code I use to get and print its contents:
textBox = Text(root)
textBox.focus_set()
def get_input():
print textBox.get(1.0, 'end-1c')
Then I use a button that uses the command get_input. It works when it comes to printing the contents of the widget.
Now that I know how to properly call on the contents and 'get' them, I would like to learn how I can assign those contents (a string) to the value of a variable.
I think what you want is this. It will delete all the text and then insert a variable.
def set_input(value):
text.delete(1.0, "END")
text.insert("END", value)
It is Python 2.x only. Python 3 requires that "END" be END from the Tk namespace.
def set_value():
text.insert("end-1c", value)
I am trying to delete item from listbox. i am using python2.7. when i am using remove it shows error.the same is for delete also
import tkinter
window=Tk()
ncbox = Tkinter.Listbox(window, width=14, height=7,fg="blue",font=("Helvetica", 20))
ncbox.grid(row=2, column=2,columnspan=4,sticky=NW)
yscroll = Tkinter.Scrollbar(command=ncbox.yview, orient=Tkinter.VERTICAL)
yscroll.grid(row=2, column=4, sticky=Tkinter.N+Tkinter.S)
ncbox.configure(yscrollcommand=yscroll.set)
msg1='abc'
msg2='xyz'
gap=' '
ncbox.insert(Tkinter.END, msg1+gap+msg2)
ncbox.delete(msg1+gap+msg2)
if msg3+gap+msg4 in ncbox:
print 'found'
window.mainloop()
How i could delete data? when i want to search it says that in is not a command for listbox. how could i change the color of every inserted text?
The error is telling you exactly what the problem is: you are giving it a bad index. The documentation for the delete method says you have to give an index. The documentation for the listbox widget describes what is a valid index -- a number, and a few special strings.
You need to tell the delete method which item number you want to delete.
I am using an onscreen keyboard to type data for a tkinter based gui.
I was able to use the entry field to enter, edit data through the onscreen keyboard such as getting current cursor position and length of the string.
temp = self.entry_label.get()
length_string=len(temp)
cursor_position = self.entry_label.index(INSERT)
But I wish to do the same for a Text widget. I could get the text for Text widget using get() method and its length but cannot get the current mouse cursor position.
temp=self.new_text.get(1.0, END)
cursor_position = self.new_text.index(INSERT)
Acutally it works and i am able to add character to that poisition , but after adding character cursor goes back to origional position , i.e last character
maybe This works? Else maybe text_widget.index(Tkinter.INSERT) is what should work.
Although the other answer is correct and worked I believe tk.Current is the meta answer.
"tk.INSERT"
as provided by this reference.
https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/text-index.html
This question already has an answer here:
Why Text cursor coordinates are not updated correctly?
(1 answer)
Closed 7 years ago.
I write a simple program with tkinter Text, and bind arrow down key with a function but the CURRENT and INSERT cursor is not correct when I press the down key.
Firstly, CURRENT sometimes is not updated, and sometimes is updated with wrong index
Secondly, INSERT is always updated, however its index is the last position, for example, if current index is line 1 column 1, then I press Down key, the printed result is still 1.1(line 1 column 1), but my cursor has already come to line 2.
Anyone has any experience on that? Thanks in advance!
def tipKeyDown(event):
pos=text.index(CURRENT)
print(pos)
pos=text.index(INSERT)
print(pos)
text = Text(textFrm, relief=SOLID)
text.bind('<Button-1>', tipButton1)
text.bind('<Down>', tipKeyDown)
You can use KeyRelease which is raised after the text change.
text.bind('<KeyRelease-Down>', tipKeyDown)
BTW, CURRENT is corresponds to the character closest to the mouse pointer. (not related to insertion cursor)
This has to do with the order that tkinter processes events. The short answer is, custom bindings on widgets are processed before the default bindings, and it's the default bindings that cause the text to be inserted or deleted, the index changed, etc.
See more here: Basic query regarding bindtags in tkinter and How to bind self events in Tkinter Text widget after it will binded by Text widget? and Why Text cursor coordinates are not updated correctly?