Python Gtk TextView insert text at end - python

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.

Related

Hi there. I am creating a program in tkinter. How to get the coordinates of the cursor position when entering text into the Text component

I am creating a program in tkinter. How to get the coordinates of the cursor position when entering text into the Text component
The index of the cursor is referenced by the string "insert". You can convert that to the form of line.character by calling the index command:
the_cursor = the_text_widget.index("insert")

How to get EPC without printing a label?

We are coding a Zebra ZT410 printer to print RFID labels with information stored in a SQL Database using Python. We are trying to print an abridged version of the EPC on each label, however we cannot get the EPC without the printer printing a blank label.
re = "^XA^RMN^RS8^FN0^RFR,H^FS^HV0^XZ"
q = re.encode("ASCII")
s.sendall(q)
data = s.recv(BUFFER_SIZE)
The Code above returns the EPC just like we need it to, however the printer prints a blank label every time the code is run. We need a way to do this without the printer spitting out a blank label every time. Is this possible? If not, is there a way to make the printer go back one label so we can reuse the blank ones ?

Tkinter Find and Replace Function for Notepad

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.

SetTextCursor in PyQt4

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)

Select text of textEdit object with QTextCursor, QTextEdit

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)

Categories

Resources