我正在尝试让文本小部件函数在 python tkinter 中正常工作 [英] I'm trying to get the Text widget functions to work properly in python tkinter

查看:63
本文介绍了我正在尝试让文本小部件函数在 python tkinter 中正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 tkinter 文本小部件中获取输入文本,并将其逐行传输到另一个文本小部件对象.

I'm trying to take input text in the tkinter text widget and transfer it to another text widget object line by line.

我尝试将文字传递给 text.get(start index, end index) 和 text.insert(index, stringToInsert)

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()

我需要它复制一整行并在单击编译下一行?"时一次将其传输到第二个文本窗口.按钮.到目前为止,我已经捕获了一行代码,然后没有正确粘贴它.它会粘贴到前一个字符串的末尾.然后当我输入一行完整的文本和第二行文本时,它粘贴不完整.所以我们应该得到(1.0, 1.30)并插入(1.0,字符串)然后得到(2.0, 2.30)插入(2.0,字符串)得到(3.0,3.30)插入(3.0,字符串)等等......但它忽略了对参数的文字更改,并且如前所述没有正确粘贴整行文本......

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...

推荐答案

有两个问题.首先是索引不是浮点数,您不应该对它们进行浮点数学运算.索引是 line.character 形式的字符串.

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.

例如,在浮点数中,1.3 和 1.30 是相同的.作为索引,1.3"代表第一行的第三个字符,1.30"代表第一行的第三十个字符.

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.

此外,您忽略了在每行末尾复制换行符.除非第 1 行以换行符结尾,否则您不能在另一个窗口的第 2 行插入;如果第 2 行不以换行符结尾,则不能在第 3 行插入,依此类推.

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.

我不知道您的意图是什么,因此很难推荐解决方案.例如,您真的只想复制一行的前 30 个字符,还是您的目标是复制整行?

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?

如果只想要一行的前30个字符,复制到另一个窗口的新行,复制时需要插入换行符.例如:

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)

这篇关于我正在尝试让文本小部件函数在 python tkinter 中正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆