I have a textEdit field and I want to process some selected text within this field (but not the format of it).
So far, I connect the button with:
QtCore.QObject.connect(self.ui.mytext_button,QtCore.SIGNAL("clicked()"), self.mytext)
The method:
def mytext(s):
return s.upper()
But how do I tell Python that s is the selected text? I know that is something with selectionStart(), selectionEnd(). And how to change it to what mytext returns? I think is something with insertText(), but here I am also lost at the details.
Answering my own question. Posting here for fellow Python noobs:
Get the selected text:
cursor = self.ui.editor_window.textCursor()
textSelected = cursor.selectedText()
insert back the text into your editor.
self.ui.editor_window.append(s)
There are also alternatives to append(), for inserting the text into the original text.
So, to put a selected text into uppercase:
def mytext(self):
cursor = self.ui.editor_window.textCursor()
textSelected = cursor.selectedText()
s = textSelected.upper()
self.ui.editor_window.append(s)
Related
I'm new at Tkinter, and python. I've been experimenting with a notepad script I've made. I'm working on a find / replace command. But I've had no luck. Here is what I've tried so far:
def replace():
def replaceall():
findtext = str(find.get(1.0, END))
replacetext = str(replace.get(1.0, END))
alltext = str(text.get(1.0, END))
alltext1 = all.replace(findtext, replacetext)
text.delete(1.0, END)
text.insert('1.0', alltext1)
replacebox =Tk()
replacebox.geometry("230x150")
replacebox.title("Replace..")
find = Text(replacebox, height=2, width=20).pack()
replace = Text(replacebox, height=2, width=20).pack()
replaceallbutton = Button(replacebox, text="Replace..", command=replaceall)
replaceallbutton.pack()
(this is just the function I am defining for the replace command)
The 'text' variable is on the large canvas which contains the menu's and the main text widget.
Any help is appreciated
So far I've been creating this notepad in 2.7.8, so the Tkinter import is 'Tkinter.'
What I'm shooting for is having the first box have the text to find and the second box have the text to be replaced. Upon pressing the replace button, the function replaceall() should begin.
Are there any obvious mistakes in my function, or is it just deeply flawed? Any help is appreciated.
The most obvious mistake is that you are creating a second instance of Tk. If you need a popup window you should create an instance of Toplevel. You should always have exactly one instance of Tk running.
The second problem is related to the fact you are using a Text widget for the find and replace inputs. When you do a get with a second index of END, the string you get back will always have a newline whether the user entered one or not. If you want exactly and only what the user typed, use "end-1c" (end minus one character).
Finally, there's no reason to get all the text, replace the string, and then re-insert all the text. That will work only as long as you have no formatting or embedded widgets or images in the text widget. The text widget has a search command which can search for a pattern (either string or regular expression), and you can use the returned information to replace the found text with the replacement text.
I want to create automcomplete feature in a tkinter text widget. When the autocomplete finds a possible word, it deletes the user part-of-word, then insert the complete word:
#if some matched words are found
if self._hits != []:
#delete the part written by the user
self.text.delete("%s+1c" % Space1Index,INSERT)
#Inser the complete word
self.text.insert("%s+1c" % Space1Index,self._hits[self._hit_index])
Then I will tag the text added by the autocomplete to have a different look than the user input. For ex, if the user wrote te, autocomplete will write the complete word test. te will be with normal font, and st will be wrote in another color and waits for the user to confirm the selected word by the computer.
My question is, after inserting the word test and properly highlighting it, how can I move the INSERT position again after te?
I hope I could clarify my question enough, please let me know if more explanation is needed.
To move the insertion cursor, set the "insert" mark to wherever you want:
self.text.mark_set("insert", "%s+1c" % ...)
-or-
self.text.mark_set(INSERT, "%s+1c" % ...)
You can save the position of the insert mark before your autocompletion changes and reset the mark to the saved position after:
old_pos = self.text.index("insert")
# make autocompletion changes
self.text.mark_set("insert", old_pos)
I want to insert text to a textbuffer at the end...
Im using this: gtk.TextBuffer.insert_at_cursor
But if I clicking into the text there the new appears at the cursor...
How can i add text at the end?
Try using gtk.TextBuffer.insert() with gtk.TextBuffer.get_end_iter(). Example:
# text_buffer is a gtk.TextBuffer
end_iter = text_buffer.get_end_iter()
text_buffer.insert(end_iter, "The text to insert at the end")
NOTE: Once you insert into text_buffer, end_iter is invalidated so make sure to get a new reference to the end-iterator whenever you want to append to the end of the buffer.
How can I get the text under the cursor? So if I hover over it and the word was "hi" I could read it? I think I need to do something with QTextCursor.WordUnderCursor but I am not really sure what. Any help?
This is what I am trying to work with right now:
textCursor = text.cursorForPosition(event.pos());
textCursor.select(QTextCursor.WordUnderCursor);
text.setTextCursor(textCursor);
word = textCursor.selectedText();
I have it selecting the text right now just so I can see it.
Edit 2:
What I am really trying to do is display a tooltip over certain words in the text.
Unfortunately, I can't test this at the moment, so this is a best guess at what you need. This is based on some code I wrote that had a textfield that showed errors in a tooltip as you typed, but should work.
You've already got code to select the word under the hover over, you just need the tooltip in the right spot.
textCursor = text.cursorForPosition(event.pos())
textCursor.select(QTextCursor.WordUnderCursor)
text.setTextCursor(textCursor)
word = textCursor.selectedText()
if meetsSomeCondition(word):
toolTipText = toolTipFromWord(word)
# Put the hover over in an easy to read spot
pos = text.cursorRect(text.textCursor()).bottomRight()
# The pos could also be set to event.pos() if you want it directly under the mouse
pos = text.mapToGlobal(pos)
QtGui.QToolTip.showText(pos,toolTipText)
I've left meetsSomeCondition() and toolTipFromWord() up to you to fill in as you don't describe those, but they are pretty descriptive in what needs to go there.
Regarding your comment on doing it without selecting the word, the easiest way to do this is to cache the cursor before you select a new one and then set it back. You can do this by calling QTextEdit.textCursor() and then setting it like you did previously.
Like so:
oldCur = text.textCursor()
textCursor.select(QTextCursor.WordUnderCursor) # line from above
text.setTextCursor(textCursor) # line from above
word = textCursor.selectedText() # line from above
text.setTextCursor(oldCur)
# if condition as above
Every time textChanged() is emitted, the text of QTextBrowser is processed and then re-inserted in that QTextBrowser.
That causes trouble with the current Cursor.
How do I do that after typing something at | and re-inserting the text, the cursor is behind the newly inserted character (here: X) ?
Hello| World
Where it should be:
HelloX| World
Where it is:
|HelloX World
I need some help because I don't understand the according part of the QT documentation.
If you're inserting text after the cursor, as yor example suggests, you, you should use the text cursor's insertText method instead of replacing the whole content of the editor - should save you some trouble:
editor.textCursor().insertText('X')
Otherwise you should be able to restore the previous position like this:
old_position = editor.textCursor().position()
# ...
new_cursor = editor.textCursor()
new_cursor.setPosition(old_position)
editor.setTextCursor(cursor)