I'm trying to get the Text widget functions to work properly in python tkinter

北城余情 提交于 2019-12-25 01:42:51

问题


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 index) and text.insert(index, stringToInsert)

from tkinter import *
import re 

class TextCompiler:

    def __init__(self, master):

        self.a = 1.0

        self.frame = Frame(master)
        self.frame.pack()

        self.topLabel = Label(master, text = "Enter text to be compiled: ", bg = "green", fg = "black")
        self.topLabel.pack(side = TOP)

        self.windowIn = Text(master, state=NORMAL, height=20, width=30)
        self.windowIn.pack(side = LEFT)
        self.windowOut = Text(master, height=20, width=30)
        self.windowOut.pack(side = RIGHT)

        self.printButton = Button(self.frame, text="Compile next line?", command = lambda: self.transferPrint(self.a))
        self.printButton.pack(side = BOTTOM)

        self.quitButton = Button(self.frame, text="Quit", command=lambda: self.quitStuff(master))
        self.quitButton.pack(side = BOTTOM)

    def transferPrint(self, a):
            b = self.a + 0.30
            endOfLine = "{0:.2f}".format(b)
            inputText = self.windowIn.get(self.a, endOfLine)
            self.windowOut.insert(self.a, inputText)
            self.a = self.a + 1.0

    def quitStuff(self, master):
        self.frame.quit()
        master.destroy()

root = Tk()
TextCompiler(root)
root.mainloop()

I need it to copy one full line and transfer it to second text window at a time at the click of "Compile next line?" button. So far I had it capturing one line of code and then not pasting it properly. It would paste to the end of the previous string. Then when I put in one FULL line of text and a second line of text it pasted incompletely. So we should get(1.0, 1.30) and insert(1.0, string) then get(2.0, 2.30) insert(2.0, string) get(3.0, 3.30) insert(3.0, string) and so on.. but its ignoring literal changes to the parameters and as said before not pasting full lines of texts properly...


回答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)


来源:https://stackoverflow.com/questions/58020950/im-trying-to-get-the-text-widget-functions-to-work-properly-in-python-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!