Skip to content Skip to sidebar Skip to footer

I'm Trying To Get The Text Widget Functions To Work Properly In Python Tkinter

I'm trying to take input text in the tkinter text widget and transfer it to another text widget object line by line. I've tried passing literals to the text.get(start index, end in

Solution 1:

There are two problems. First is that indexes aren't floating-point numbers and you shouldn't be doing floating point math on them. Indexes are strings of the form line.character.

For example, in floating-point numbers, 1.3 and 1.30 are identical. As indexes, "1.3" represents the third character on line 1, and "1.30" represents the thirtieth character on line one.

Additionally, you are neglecting to copy the newline at the end of each line. You can't insert on line 2 in the other window unless line 1 ends with a newline, you can't insert on line 3 if line two doesn't end in a newline, and so on.

I don't know what your intent is, so it's hard to recommend a solution. For example, do you really want to only copy the first 30 characters of a line, or is your goal to copy the whole line?

If you want only the first 30 characters of a line and copy it to a new line in the other window, you need to insert a newline when you copy it over. For example:

self.windowOut.insert("end", inputText+"\n")

If your goal is just to copy entire lines, you can use a modifier to copy the entire line plus the trailing newline. Or, copy just the entire line without the newline, and like above you can append a newline when copying it over.

Here's how to get the full line:

end = "{} lineend".format(self.a)

Here's how to get the full line plus the trailing newline:

end = "{} lineend+1c".format(self.a)

Post a Comment for "I'm Trying To Get The Text Widget Functions To Work Properly In Python Tkinter"